Skip to content

feat: implement entry for BTreeMap similar to std lib#420

Open
hpeebles wants to merge 7 commits intodfinity:mainfrom
hpeebles:entry
Open

feat: implement entry for BTreeMap similar to std lib#420
hpeebles wants to merge 7 commits intodfinity:mainfrom
hpeebles:entry

Conversation

@hpeebles
Copy link
Copy Markdown
Contributor

@hpeebles hpeebles commented Mar 26, 2026

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 |         |

@hpeebles hpeebles requested a review from a team as a code owner March 26, 2026 17:53
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 27, 2026

canbench 🏋 (dir: ./benchmarks/nns) ba5170b 2026-03-27 11:57:27 UTC

./benchmarks/nns/canbench_results.yml is up to date
📦 canbench_results_nns.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median -98 | p25 -439.05K | min -6.98M]
    change %: [max 0.00% | p75 0.00% | median -0.04% | p25 -0.16% | min -0.29%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 27, 2026

canbench 🏋 (dir: ./benchmarks/io_chunks) ba5170b 2026-03-27 11:58:02 UTC

./benchmarks/io_chunks/canbench_results.yml is up to date
📦 canbench_results_io_chunks.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 18 | regressed 0 | improved 0 | new 0 | unchanged 18]
    change:   [max +827.11M | p75 0 | median 0 | p25 0 | min 0]
    change %: [max +0.99% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 18 | regressed 0 | improved 0 | new 0 | unchanged 18]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 18 | regressed 0 | improved 0 | new 0 | unchanged 18]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 27, 2026

canbench 🏋 (dir: ./benchmarks/vec) ba5170b 2026-03-27 11:57:13 UTC

./benchmarks/vec/canbench_results.yml is up to date
📦 canbench_results_vec.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 27, 2026

canbench 🏋 (dir: ./benchmarks/memory_manager) ba5170b 2026-03-27 11:57:12 UTC

./benchmarks/memory_manager/canbench_results.yml is up to date
📦 canbench_results_memory-manager.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 3 | regressed 0 | improved 0 | new 0 | unchanged 3]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 3 | regressed 0 | improved 0 | new 0 | unchanged 3]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 3 | regressed 0 | improved 0 | new 0 | unchanged 3]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 27, 2026

canbench 🏋 (dir: ./benchmarks/btreeset) ba5170b 2026-03-27 11:57:38 UTC

./benchmarks/btreeset/canbench_results.yml is up to date
📦 canbench_results_btreeset.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 100 | regressed 0 | improved 0 | new 0 | unchanged 100]
    change:   [max +31.11M | p75 +15 | median 0 | p25 0 | min -102]
    change %: [max +0.89% | p75 0.00% | median 0.00% | p25 0.00% | min -0.19%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 100 | regressed 0 | improved 0 | new 0 | unchanged 100]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 100 | regressed 0 | improved 0 | new 0 | unchanged 100]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 27, 2026

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

---------------------------------------------------

Summary:
  instructions:
    status:   Regressions and new benchmarks 🔴➕
    counts:   [total 305 | regressed 15 | improved 0 | new 2 | unchanged 288]
    change:   [max +32.66M | p75 +874.04K | median +164.93K | p25 0 | min -41.21M]
    change %: [max +3.48% | p75 +0.10% | median +0.02% | p25 0.00% | min -0.51%]

  heap_increase:
    status:   New benchmarks added ➕
    counts:   [total 305 | regressed 0 | improved 0 | new 2 | unchanged 303]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   New benchmarks added ➕
    counts:   [total 305 | regressed 0 | improved 0 | new 2 | unchanged 303]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------

Only significant changes:
| status | name                                      | calls |     ins |  ins Δ% | HI |  HI Δ% | SMI |  SMI Δ% |
|--------|-------------------------------------------|-------|---------|---------|----|--------|-----|---------|
|   +    | btreemap_v2_insert_vec_32_4               |       | 683.34M |  +3.48% |  0 |  0.00% |  20 |   0.00% |
|   +    | btreemap_v2_insert_vec_32_16              |       | 688.97M |  +3.42% |  0 |  0.00% |  20 |   0.00% |
|   +    | btreemap_v2_insert_vec_32_0               |       | 642.31M |  +3.29% |  0 |  0.00% |  20 |   0.00% |
|   +    | btreemap_v2_insert_vec_32_32              |       | 683.30M |  +3.27% |  0 |  0.00% |  20 |   0.00% |
|   +    | btreemap_v2_insert_vec_32_8               |       | 681.41M |  +3.27% |  0 |  0.00% |  20 |   0.00% |
|   +    | btreemap_v2_insert_vec_4_128              |       | 621.09M |  +2.77% |  0 |  0.00% |  16 |   0.00% |
|   +    | btreemap_v2_insert_vec_128_128            |       |   1.04B |  +2.75% |  0 |  0.00% |  51 |   0.00% |
|   +    | btreemap_v2_mem_manager_insert_u64_vec512 |       | 856.91M |  +2.73% |  0 |  0.00% |   0 |   0.00% |
|   +    | btreemap_v2_insert_vec_32_64              |       | 710.41M |  +2.71% |  0 |  0.00% |  24 |   0.00% |
|   +    | btreemap_v2_insert_vec_8_128              |       | 683.31M |  +2.55% |  0 |  0.00% |  23 |   0.00% |
|   +    | btreemap_v2_insert_vec8_u64               |       | 609.17M |  +2.54% |  0 |  0.00% |  16 |   0.00% |
|   +    | btreemap_v2_insert_u64_vec8               |       | 419.80M |  +2.30% |  0 |  0.00% |  21 |   0.00% |
|   +    | btreemap_v2_insert_vec_32_256             |       | 887.85M |  +2.14% |  0 |  0.00% |  54 |   0.00% |
|   +    | btreemap_v2_insert_vec_32_512             |       | 994.43M |  +2.13% |  0 |  0.00% |  91 |   0.00% |
|   +    | btreemap_v2_insert_vec_32_128             |       | 772.02M |  +2.11% |  0 |  0.00% |  33 |   0.00% |
|  new   | btreemap_get_and_incr                     |       | 596.45M |         |  0 |        |   0 |         |
|  new   | btreemap_get_and_incr_via_entry           |       | 373.80M |         |  0 |        |   0 |         |

ins = instructions, HI = heap_increase, SMI = stable_memory_increase, Δ% = percent change

---------------------------------------------------
CSV results saved to canbench_results.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant