@@ -32,11 +32,11 @@ namespace silkworm::db::kv::api {
32
32
33
33
CoherentStateView::CoherentStateView (Transaction& txn, CoherentStateCache* cache) : txn_(txn), cache_(cache) {}
34
34
35
- Task<std::optional<Bytes>> CoherentStateView::get (const Bytes& key) {
35
+ Task<std::optional<Bytes>> CoherentStateView::get (ByteView key) {
36
36
co_return co_await cache_->get (key, txn_);
37
37
}
38
38
39
- Task<std::optional<Bytes>> CoherentStateView::get_code (const Bytes& key) {
39
+ Task<std::optional<Bytes>> CoherentStateView::get_code (ByteView key) {
40
40
co_return co_await cache_->get_code (key, txn_);
41
41
}
42
42
@@ -156,7 +156,7 @@ void CoherentStateCache::process_storage_change(CoherentStateRoot* root, StateVi
156
156
}
157
157
}
158
158
159
- bool CoherentStateCache::add (const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id) {
159
+ bool CoherentStateCache::add (KeyValue& & kv, CoherentStateRoot* root, StateViewId view_id) {
160
160
auto [it, inserted] = root->cache .insert (kv);
161
161
SILK_DEBUG << " Data cache kv.key=" << to_hex (kv.key ) << " inserted=" << inserted << " view=" << view_id;
162
162
std::optional<KeyValue> replaced;
@@ -173,7 +173,7 @@ bool CoherentStateCache::add(const KeyValue& kv, CoherentStateRoot* root, StateV
173
173
state_evictions_.remove (*replaced);
174
174
SILK_DEBUG << " Data evictions removed replaced.key=" << to_hex (replaced->key );
175
175
}
176
- state_evictions_.push_front (kv );
176
+ state_evictions_.push_front (std::move (kv) );
177
177
178
178
// Remove the longest unused key-value pair when size exceeded
179
179
if (state_evictions_.size () > config_.max_state_keys ) {
@@ -186,7 +186,7 @@ bool CoherentStateCache::add(const KeyValue& kv, CoherentStateRoot* root, StateV
186
186
return inserted;
187
187
}
188
188
189
- bool CoherentStateCache::add_code (const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id) {
189
+ bool CoherentStateCache::add_code (KeyValue& & kv, CoherentStateRoot* root, StateViewId view_id) {
190
190
auto [it, inserted] = root->code_cache .insert (kv);
191
191
SILK_DEBUG << " Code cache kv.key=" << to_hex (kv.key ) << " inserted=" << inserted << " view=" << view_id;
192
192
std::optional<KeyValue> replaced;
@@ -203,7 +203,7 @@ bool CoherentStateCache::add_code(const KeyValue& kv, CoherentStateRoot* root, S
203
203
code_evictions_.remove (*replaced);
204
204
SILK_DEBUG << " Code evictions removed replaced.key=" << to_hex (replaced->key );
205
205
}
206
- code_evictions_.push_front (kv );
206
+ code_evictions_.push_front (std::move (kv) );
207
207
208
208
// Remove the longest unused key-value pair when size exceeded
209
209
if (code_evictions_.size () > config_.max_code_keys ) {
@@ -216,7 +216,7 @@ bool CoherentStateCache::add_code(const KeyValue& kv, CoherentStateRoot* root, S
216
216
return inserted;
217
217
}
218
218
219
- Task<std::optional<Bytes>> CoherentStateCache::get (const Bytes& key, Transaction& tx) {
219
+ Task<std::optional<Bytes>> CoherentStateCache::get (ByteView key, Transaction& tx) {
220
220
std::shared_lock read_lock{rw_mutex_};
221
221
222
222
const auto view_id = tx.view_id ();
@@ -225,7 +225,7 @@ Task<std::optional<Bytes>> CoherentStateCache::get(const Bytes& key, Transaction
225
225
co_return std::nullopt;
226
226
}
227
227
228
- KeyValue kv{key};
228
+ KeyValue kv{Bytes{ key} };
229
229
auto & cache = root_it->second ->cache ;
230
230
const auto kv_it = cache.find (kv);
231
231
if (kv_it != cache.end ()) {
@@ -252,12 +252,13 @@ Task<std::optional<Bytes>> CoherentStateCache::get(const Bytes& key, Transaction
252
252
read_lock.unlock ();
253
253
std::unique_lock write_lock{rw_mutex_};
254
254
255
- add ({key, value}, root_it->second .get (), view_id);
255
+ kv.value = value;
256
+ add (std::move (kv), root_it->second .get (), view_id);
256
257
257
258
co_return value;
258
259
}
259
260
260
- Task<std::optional<Bytes>> CoherentStateCache::get_code (const Bytes& key, Transaction& tx) {
261
+ Task<std::optional<Bytes>> CoherentStateCache::get_code (ByteView key, Transaction& tx) {
261
262
std::shared_lock read_lock{rw_mutex_};
262
263
263
264
const auto view_id = tx.view_id ();
@@ -266,7 +267,7 @@ Task<std::optional<Bytes>> CoherentStateCache::get_code(const Bytes& key, Transa
266
267
co_return std::nullopt;
267
268
}
268
269
269
- KeyValue kv{key};
270
+ KeyValue kv{Bytes{ key} };
270
271
auto & code_cache = root_it->second ->code_cache ;
271
272
const auto kv_it = code_cache.find (kv);
272
273
if (kv_it != code_cache.end ()) {
@@ -293,7 +294,8 @@ Task<std::optional<Bytes>> CoherentStateCache::get_code(const Bytes& key, Transa
293
294
read_lock.unlock ();
294
295
std::unique_lock write_lock{rw_mutex_};
295
296
296
- add_code ({key, value}, root_it->second .get (), view_id);
297
+ kv.value = value;
298
+ add_code (std::move (kv), root_it->second .get (), view_id);
297
299
298
300
co_return value;
299
301
}
0 commit comments