Prevent needless key/value copies in database methods #163
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.
Signed-off-by: Victor Porof victor.porof@gmail.com
We're using
std::collections::HashMap
'sEntry
API to store keys/values in our safe mode database implementation. This is more ergonomic than using an imperative API, and more performant in the ideal case.However, the real world isn't ideal, and we end up needlessly reallocating keys and values when interacting with the database. For example, using the
Entry
API to insert a key/value pair into the database results in a copy of the key when the value was already added. Similarly (in an even worse case scenario), deleting all values matching a key copies the key, and attempts clearing an already emptyBTreeSet
representing our values.The
Entry
API is long known for having poor performance in those situations, with many attempts to fix it. We could opt into using the imperative API instead, but that just makes us less performant in other situations. Luckily, we now have a newRawEntry
API, added in rust-lang/rust#54043 giving us the tools required to deal with this problem elegantly.The
RawEntry
API is available in nightlies at the moment; however, we can use it in stable viastd::collections::HashMap
's backing store rust-lang/hashbrown.