Pass the prefix by reference in storage key prefix iteration - #2100
Open
oliv3rdrt wants to merge 1 commit into
Open
Pass the prefix by reference in storage key prefix iteration#2100oliv3rdrt wants to merge 1 commit into
oliv3rdrt wants to merge 1 commit into
Conversation
The prefix and key passed to prefix_iter and greater_equal_iter are only read, but they were taken by value, so callers had to hand over an owned Vec and in a few places clone it first. Change the ReadOps methods, and the corresponding helpers in the database module, to take a slice instead, and update the backends and their callers. This removes the clones in the locking adaptor, which cloned the prefix twice per call, and lets callers that only have a slice avoid allocating. The returned iterator borrows from the transaction only, so the precise capturing syntax is used to keep it from borrowing the prefix as well.
oliv3rdrt
requested review from
ImplOfAnImpl,
anyxem and
erubboli
as code owners
July 28, 2026 07:27
Author
|
@erubboli can I be assigned this? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The prefix and key passed to
prefix_iterandgreater_equal_iterare only read, but they were taken by value, so callers had to hand over an ownedVeceven when they already had a slice. In the locking adaptor this meant cloning the prefix twice on every call.This changes the
ReadOpsmethods, and the matching helpers in the database module, to take&[u8], and updates the backends (in-memory, lmdb, sqlite, failing, and the locking adaptor) and their callers.What it removes in practice:
prefix.clone()calls and thekey.clone()in the locking adaptorVec::new()andData::new()allocations at call sites that only needed an empty prefixclone()calls in the backend test suiteOne thing worth a look during review: since the crate is on edition 2024, the returned
impl Iteratorwould otherwise capture the prefix lifetime as well as the transaction's. I used the precise capturing syntax (use<..>) on the trait methods and the impls so the iterator only borrows from the transaction, which is what the previous signature effectively guaranteed. If you would rather see it done another way I am happy to change it.Checked with
cargo check --workspace --all-targets,cargo fmt, and the storage tests (storage,storage-core,storage-inmemory,storage-lmdb,storage-sqlite) pluschainstate-storageandutxo, all passing. Note that a few of the lmdb property tests fail intermittently when run in parallel both with and without this change, which looks like the flakiness tracked in #762, they pass with--test-threads=1.Addresses #417.