CCF
Loading...
Searching...
No Matches
interpreter_cache.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6#include "ccf/pal/locking.h"
7#include "ds/lru.h"
8
9namespace ccf::js
10{
12 {
13 protected:
14 // Locks access to all internal fields
18
20
21 std::shared_ptr<js::core::Context> make_interpreter(js::TxAccess access)
22 {
23 if (interpreter_factory != nullptr)
24 {
25 return interpreter_factory(access);
26 }
27
28 return std::make_shared<js::core::Context>(access);
29 }
30
31 public:
32 InterpreterCache(size_t max_cache_size) : lru(max_cache_size) {}
33
34 std::shared_ptr<js::core::Context> get_interpreter(
35 js::TxAccess access,
36 const std::optional<ccf::endpoints::InterpreterReusePolicy>&
37 interpreter_reuse,
38 size_t freshness_marker) override
39 {
40 if (access != js::TxAccess::APP_RW && access != js::TxAccess::APP_RO)
41 {
42 throw std::logic_error(
43 "JS interpreter reuse lru is only supported for APP "
44 "interpreters");
45 }
46
47 std::lock_guard<ccf::pal::Mutex> guard(lock);
48
49 if (cache_build_marker != freshness_marker)
50 {
52 "Clearing interpreter lru at {} - rebuilding at {}",
54 freshness_marker);
55 lru.clear();
56 cache_build_marker = freshness_marker;
57 }
58
59 if (interpreter_reuse.has_value())
60 {
61 switch (interpreter_reuse->kind)
62 {
64 {
65 auto key = interpreter_reuse->key;
66 if (access == js::TxAccess::APP_RW)
67 {
68 key += " (rw)";
69 }
70 else if (access == js::TxAccess::APP_RO)
71 {
72 key += " (ro)";
73 }
74 auto it = lru.find(key);
75 if (it == lru.end())
76 {
77 it = lru.insert(key, make_interpreter(access));
79 "Constructed cached JS interpreter at key {}. Cache now "
80 "contains {} interpreters",
81 key,
82 lru.size());
83 }
84 else
85 {
87 "Returning interpreter previously in cache, with key {}", key);
88 lru.promote(it);
89 }
90
91 return it->second;
92 }
93 }
94 }
95
96 // Return a fresh interpreter, not stored in the cache
97 LOG_TRACE_FMT("Returning freshly constructed interpreter");
98 return make_interpreter(access);
99 }
100
101 void set_max_cached_interpreters(size_t max) override
102 {
103 std::lock_guard<ccf::pal::Mutex> guard(lock);
104 lru.set_max_size(max);
105 }
106
108 {
110 }
111 };
112}
Definition lru.h:20
Definition interpreter_cache_interface.h:23
Definition interpreter_cache.h:12
size_t cache_build_marker
Definition interpreter_cache.h:17
void set_max_cached_interpreters(size_t max) override
Definition interpreter_cache.h:101
LRU< std::string, std::shared_ptr< js::core::Context > > lru
Definition interpreter_cache.h:16
ccf::pal::Mutex lock
Definition interpreter_cache.h:15
InterpreterFactory interpreter_factory
Definition interpreter_cache.h:19
std::shared_ptr< js::core::Context > make_interpreter(js::TxAccess access)
Definition interpreter_cache.h:21
std::shared_ptr< js::core::Context > get_interpreter(js::TxAccess access, const std::optional< ccf::endpoints::InterpreterReusePolicy > &interpreter_reuse, size_t freshness_marker) override
Definition interpreter_cache.h:34
InterpreterCache(size_t max_cache_size)
Definition interpreter_cache.h:32
void set_interpreter_factory(const InterpreterFactory &ip) override
Definition interpreter_cache.h:107
#define LOG_INFO_FMT
Definition logger.h:362
#define LOG_TRACE_FMT
Definition logger.h:356
Definition bundle.h:12
std::function< std::shared_ptr< js::core::Context >(js::TxAccess)> InterpreterFactory
Definition interpreter_cache_interface.h:20
TxAccess
Definition tx_access.h:10
std::mutex Mutex
Definition locking.h:12
@ KeyBased
Definition endpoint.h:147