You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This allows you to lookup an entry by searching the map, then modify the value and insert it back into the map without having to search again.
The API is similar to entry on the std lib BTreeMap, but does not work with mutable references, so you need to get the owned value, update it, then insert it back in the entry.
For example in the std lib you would write this
match map.entry(1) {
Occupied(mut e) => {
let value = e.get_mut();
*value += 1;
}
Vacant(e) => {
e.insert(1);
}
}
Or using the shorthand syntax
*map.entry(1).or_default() += 1;
Whereas using this new API for StableBTreeMap you would write it as this (the shorthand syntax is not available)
match map.entry(1) {
Occupied(e) => {
let value = e.get();
e.insert(value + 1);
}
Vacant(e) => {
e.insert(1);
}
}
I've added some benchmarks which compare using get then insert vs using entry for 10k u32 values, from the results you can see that in this scenario using entry is roughly 37% faster
| status | name | calls | ins | ins Δ% | HI | HI Δ% | SMI | SMI Δ% |
|--------|---------------------------------|-------|---------|---------|----|--------|-----|---------|
| new | btreemap_get_and_incr | | 597.30M | | 0 | | 0 | |
| new | btreemap_get_and_incr_via_entry | | 374.19M | | 0 | | 0 | |
canbench 🏋 (dir: ./benchmarks/btreemap) ba5170b 2026-03-27 11:59:10 UTC
❌ ./benchmarks/btreemap/canbench_results.yml is not up to date
If the performance change is expected, run canbench --persist [--csv] to update the benchmark results.
📦 canbench_results_btreemap.csv available in artifacts
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
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.
This allows you to lookup an entry by searching the map, then modify the value and insert it back into the map without having to search again.
The API is similar to
entryon the std libBTreeMap, but does not work with mutable references, so you need to get the owned value, update it, then insert it back in the entry.For example in the std lib you would write this
Or using the shorthand syntax
Whereas using this new API for
StableBTreeMapyou would write it as this (the shorthand syntax is not available)I've added some benchmarks which compare using
gettheninsertvs usingentryfor 10k u32 values, from the results you can see that in this scenario usingentryis roughly 37% faster