Skip to content

Commit 6e2c1e1

Browse files
authored
db: use ByteView in KV StateCache (#2141)
1 parent f1d688b commit 6e2c1e1

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

silkworm/db/kv/api/base_transaction.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ Task<Bytes> BaseTransaction::get_one_impl_with_cache(const std::string& table, B
5555
if (table == db::table::kPlainStateName) {
5656
std::shared_ptr<StateView> view = state_cache_->get_view(*this);
5757
if (view != nullptr) {
58-
// TODO(canepat) remove key copy changing DatabaseReader interface
59-
const auto value = co_await view->get(silkworm::Bytes{key.data(), key.size()});
58+
const auto value = co_await view->get(key);
6059
co_return value ? *value : silkworm::Bytes{};
6160
}
6261
} else if (table == db::table::kCodeName) {
6362
std::shared_ptr<StateView> view = state_cache_->get_view(*this);
6463
if (view != nullptr) {
65-
// TODO(canepat) remove key copy changing DatabaseReader interface
66-
const auto value = co_await view->get_code(silkworm::Bytes{key.data(), key.size()});
64+
const auto value = co_await view->get_code(key);
6765
co_return value ? *value : silkworm::Bytes{};
6866
}
6967
}

silkworm/db/kv/api/state_cache.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ namespace silkworm::db::kv::api {
3232

3333
CoherentStateView::CoherentStateView(Transaction& txn, CoherentStateCache* cache) : txn_(txn), cache_(cache) {}
3434

35-
Task<std::optional<Bytes>> CoherentStateView::get(const Bytes& key) {
35+
Task<std::optional<Bytes>> CoherentStateView::get(ByteView key) {
3636
co_return co_await cache_->get(key, txn_);
3737
}
3838

39-
Task<std::optional<Bytes>> CoherentStateView::get_code(const Bytes& key) {
39+
Task<std::optional<Bytes>> CoherentStateView::get_code(ByteView key) {
4040
co_return co_await cache_->get_code(key, txn_);
4141
}
4242

@@ -156,7 +156,7 @@ void CoherentStateCache::process_storage_change(CoherentStateRoot* root, StateVi
156156
}
157157
}
158158

159-
bool CoherentStateCache::add(const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id) {
159+
bool CoherentStateCache::add(KeyValue&& kv, CoherentStateRoot* root, StateViewId view_id) {
160160
auto [it, inserted] = root->cache.insert(kv);
161161
SILK_DEBUG << "Data cache kv.key=" << to_hex(kv.key) << " inserted=" << inserted << " view=" << view_id;
162162
std::optional<KeyValue> replaced;
@@ -173,7 +173,7 @@ bool CoherentStateCache::add(const KeyValue& kv, CoherentStateRoot* root, StateV
173173
state_evictions_.remove(*replaced);
174174
SILK_DEBUG << "Data evictions removed replaced.key=" << to_hex(replaced->key);
175175
}
176-
state_evictions_.push_front(kv);
176+
state_evictions_.push_front(std::move(kv));
177177

178178
// Remove the longest unused key-value pair when size exceeded
179179
if (state_evictions_.size() > config_.max_state_keys) {
@@ -186,7 +186,7 @@ bool CoherentStateCache::add(const KeyValue& kv, CoherentStateRoot* root, StateV
186186
return inserted;
187187
}
188188

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) {
190190
auto [it, inserted] = root->code_cache.insert(kv);
191191
SILK_DEBUG << "Code cache kv.key=" << to_hex(kv.key) << " inserted=" << inserted << " view=" << view_id;
192192
std::optional<KeyValue> replaced;
@@ -203,7 +203,7 @@ bool CoherentStateCache::add_code(const KeyValue& kv, CoherentStateRoot* root, S
203203
code_evictions_.remove(*replaced);
204204
SILK_DEBUG << "Code evictions removed replaced.key=" << to_hex(replaced->key);
205205
}
206-
code_evictions_.push_front(kv);
206+
code_evictions_.push_front(std::move(kv));
207207

208208
// Remove the longest unused key-value pair when size exceeded
209209
if (code_evictions_.size() > config_.max_code_keys) {
@@ -216,7 +216,7 @@ bool CoherentStateCache::add_code(const KeyValue& kv, CoherentStateRoot* root, S
216216
return inserted;
217217
}
218218

219-
Task<std::optional<Bytes>> CoherentStateCache::get(const Bytes& key, Transaction& tx) {
219+
Task<std::optional<Bytes>> CoherentStateCache::get(ByteView key, Transaction& tx) {
220220
std::shared_lock read_lock{rw_mutex_};
221221

222222
const auto view_id = tx.view_id();
@@ -225,7 +225,7 @@ Task<std::optional<Bytes>> CoherentStateCache::get(const Bytes& key, Transaction
225225
co_return std::nullopt;
226226
}
227227

228-
KeyValue kv{key};
228+
KeyValue kv{Bytes{key}};
229229
auto& cache = root_it->second->cache;
230230
const auto kv_it = cache.find(kv);
231231
if (kv_it != cache.end()) {
@@ -252,12 +252,13 @@ Task<std::optional<Bytes>> CoherentStateCache::get(const Bytes& key, Transaction
252252
read_lock.unlock();
253253
std::unique_lock write_lock{rw_mutex_};
254254

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);
256257

257258
co_return value;
258259
}
259260

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) {
261262
std::shared_lock read_lock{rw_mutex_};
262263

263264
const auto view_id = tx.view_id();
@@ -266,7 +267,7 @@ Task<std::optional<Bytes>> CoherentStateCache::get_code(const Bytes& key, Transa
266267
co_return std::nullopt;
267268
}
268269

269-
KeyValue kv{key};
270+
KeyValue kv{Bytes{key}};
270271
auto& code_cache = root_it->second->code_cache;
271272
const auto kv_it = code_cache.find(kv);
272273
if (kv_it != code_cache.end()) {
@@ -293,7 +294,8 @@ Task<std::optional<Bytes>> CoherentStateCache::get_code(const Bytes& key, Transa
293294
read_lock.unlock();
294295
std::unique_lock write_lock{rw_mutex_};
295296

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);
297299

298300
co_return value;
299301
}

silkworm/db/kv/api/state_cache.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class StateView {
4040
public:
4141
virtual ~StateView() = default;
4242

43-
virtual Task<std::optional<Bytes>> get(const Bytes& key) = 0;
43+
virtual Task<std::optional<Bytes>> get(ByteView key) = 0;
4444

45-
virtual Task<std::optional<Bytes>> get_code(const Bytes& key) = 0;
45+
virtual Task<std::optional<Bytes>> get_code(ByteView key) = 0;
4646
};
4747

4848
class StateCache {
@@ -95,9 +95,9 @@ class CoherentStateView : public StateView {
9595
CoherentStateView(const CoherentStateView&) = delete;
9696
CoherentStateView& operator=(const CoherentStateView&) = delete;
9797

98-
Task<std::optional<Bytes>> get(const Bytes& key) override;
98+
Task<std::optional<Bytes>> get(ByteView key) override;
9999

100-
Task<std::optional<Bytes>> get_code(const Bytes& key) override;
100+
Task<std::optional<Bytes>> get_code(ByteView key) override;
101101

102102
private:
103103
Transaction& txn_;
@@ -134,10 +134,10 @@ class CoherentStateCache : public StateCache {
134134
void process_code_change(CoherentStateRoot* root, StateViewId view_id, const remote::AccountChange& change);
135135
void process_delete_change(CoherentStateRoot* root, StateViewId view_id, const remote::AccountChange& change);
136136
void process_storage_change(CoherentStateRoot* root, StateViewId view_id, const remote::AccountChange& change);
137-
bool add(const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id);
138-
bool add_code(const KeyValue& kv, CoherentStateRoot* root, StateViewId view_id);
139-
Task<std::optional<Bytes>> get(const Bytes& key, Transaction& txn);
140-
Task<std::optional<Bytes>> get_code(const Bytes& key, Transaction& txn);
137+
bool add(KeyValue&& kv, CoherentStateRoot* root, StateViewId view_id);
138+
bool add_code(KeyValue&& kv, CoherentStateRoot* root, StateViewId view_id);
139+
Task<std::optional<Bytes>> get(ByteView key, Transaction& txn);
140+
Task<std::optional<Bytes>> get_code(ByteView key, Transaction& txn);
141141
CoherentStateRoot* get_root(StateViewId view_id);
142142
CoherentStateRoot* advance_root(StateViewId view_id);
143143
void evict_roots(StateViewId next_view_id);

0 commit comments

Comments
 (0)