Skip to content

perf: add batch read of BTreeMap node children addresses#364

Merged
maksymar merged 17 commits intomainfrom
maksym/save_v2
Jul 8, 2025
Merged

perf: add batch read of BTreeMap node children addresses#364
maksymar merged 17 commits intomainfrom
maksym/save_v2

Conversation

@maksymar
Copy link
Copy Markdown
Contributor

@maksymar maksymar commented Jul 7, 2025

This PR introduces batch reading of BTreeMap node children addresses to improve performance.

  • compare: 2 of 18 improved, none regressed, median 0.00%, best -7.10%
  • btreeset: 20 of 100 improved, no significant regressions, worst +0.09%, median -0.71%, best -5.87%
  • btreemap: 97 of 285 improved, 1 regressed, worst +3.11%, median -1.45%, best -8.91%

compare

  instructions:
    status:   Improvements detected 🟢
    counts:   [total 18 | regressed 0 | improved 2 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 -1 | min -5.17B]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 -0.00% | min -7.10%]

btreeset

  instructions:
    status:   Improvements detected 🟢
    counts:   [total 100 | regressed 0 | improved 20 | new 0 | unchanged 80]
    change:   [max +113.94K | p75 -20.40K | median -32.72K | p25 -171.95K | min -32.04M]
    change %: [max +0.09% | p75 -0.29% | median -0.71% | p25 -1.41% | min -5.87%]

btreemap

  instructions:
    status:   Regressions and improvements 🔴🟢
    counts:   [total 285 | regressed 1 | improved 97 | new 0 | unchanged 187]
    change:   [max +29.84M | p75 -7.47M | median -9.62M | p25 -16.38M | min -29.91M]
    change %: [max +3.11% | p75 -0.67% | median -1.45% | p25 -2.35% | min -8.91%]

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 7, 2025

canbench 🏋 (dir: ./benchmarks/compare) 9e04765 2025-07-08 09:20:09 UTC

./benchmarks/compare/canbench_results.yml is up to date
📦 canbench_results_compare.csv available in artifacts

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

Summary:
  instructions:
    status:   Improvements detected 🟢
    counts:   [total 18 | regressed 0 | improved 2 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 -1 | min -5.17B]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 -0.00% | min -7.10%]

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

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

Only significant changes:
| status | name                     | calls |    ins |  ins Δ% | HI |  HI Δ% |   SMI |  SMI Δ% |
|--------|--------------------------|-------|--------|---------|----|--------|-------|---------|
|   -    | write_chunks_btreemap_1m |       | 83.66B |  -5.82% |  0 |  0.00% | 3.07K |   0.00% |
|   -    | read_chunks_btreemap_1m  |       | 40.94B |  -7.10% |  0 |  0.00% |     0 |   0.00% |

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

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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 7, 2025

canbench 🏋 (dir: ./benchmarks/vec) 9e04765 2025-07-08 09:19:18 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 Jul 7, 2025

canbench 🏋 (dir: ./benchmarks/memory_manager) 9e04765 2025-07-08 09:19:13 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 Jul 7, 2025

canbench 🏋 (dir: ./benchmarks/btreeset) 9e04765 2025-07-08 09:19:35 UTC

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

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

Summary:
  instructions:
    status:   Improvements detected 🟢
    counts:   [total 100 | regressed 0 | improved 20 | new 0 | unchanged 80]
    change:   [max +113.94K | p75 -20.40K | median -32.72K | p25 -171.95K | min -32.04M]
    change %: [max +0.09% | p75 -0.29% | median -0.71% | p25 -1.41% | min -5.87%]

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

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

Only significant changes:
| status | name                       | calls |     ins |  ins Δ% | HI |  HI Δ% | SMI |  SMI Δ% |
|--------|----------------------------|-------|---------|---------|----|--------|-----|---------|
|   -    | btreeset_range_u32         |       |   7.22M |  -2.15% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_range_u64         |       |   7.26M |  -2.18% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_insert_blob_64    |       | 980.71M |  -2.73% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_is_subset_blob_64 |       |  57.97K |  -2.76% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_remove_blob_64    |       | 987.47M |  -2.92% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_insert_blob_32    |       | 815.13M |  -3.45% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_remove_blob_32    |       | 802.37M |  -3.79% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_insert_blob_16    |       | 716.22M |  -3.88% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_insert_blob_8     |       | 694.55M |  -3.90% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_remove_blob_16    |       | 705.65M |  -4.32% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_remove_blob_8     |       | 683.86M |  -4.38% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_is_subset_blob_16 |       |  53.97K |  -4.46% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_insert_u64        |       | 561.94M |  -4.63% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_is_subset_blob_8  |       |  52.52K |  -4.73% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_insert_u32        |       | 540.59M |  -5.11% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_remove_u64        |       | 553.32M |  -5.30% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_is_subset_blob_32 |       |  45.72K |  -5.40% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_is_subset_u64     |       |  43.86K |  -5.61% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_remove_u32        |       | 528.39M |  -5.72% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreeset_is_subset_u32     |       |  43.31K |  -5.87% |  0 |  0.00% |   0 |   0.00% |

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

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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 7, 2025

canbench 🏋 (dir: ./benchmarks/btreemap) 9e04765 2025-07-08 09:21:09 UTC

./benchmarks/btreemap/canbench_results.yml is up to date
📦 canbench_results_btreemap.csv available in artifacts

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

Summary:
  instructions:
    status:   Regressions and improvements 🔴🟢
    counts:   [total 285 | regressed 1 | improved 97 | new 0 | unchanged 187]
    change:   [max +29.84M | p75 -7.47M | median -9.62M | p25 -16.38M | min -29.91M]
    change %: [max +3.11% | p75 -0.67% | median -1.45% | p25 -2.35% | min -8.91%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 285 | regressed 0 | improved 0 | new 0 | unchanged 285]
    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 285 | regressed 0 | improved 0 | new 0 | unchanged 285]
    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_range_count_1k_10kib             |       |   2.48M |  +3.11% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_vec8_u64                     |       | 379.65M |  -2.01% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_contains_vec8_u64                |       | 374.10M |  -2.04% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_first_blob_32_128            |       | 860.90M |  -2.08% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_last_blob_32_128             |       | 826.50M |  -2.12% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_vec_32_256                   |       | 447.15M |  -2.14% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_get_vec512_u64       |       |   1.24B |  -2.19% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_insert_blob_4_128                |       | 406.39M |  -2.19% |  0 |  0.00% |  13 |   0.00% |
|   -    | btreemap_v2_pop_first_blob_32_4              |       | 779.42M |  -2.20% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_last_vec_32_16               |       | 903.59M |  -2.20% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_contains_vec_32_256              |       | 433.05M |  -2.21% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_vec_32_16                    |       | 373.71M |  -2.21% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_first_blob_32_8              |       | 797.44M |  -2.22% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_last_vec_32_4                |       | 899.65M |  -2.22% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_last_vec_32_8                |       | 903.14M |  -2.23% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_last_blob_4_128              |       | 358.12M |  -2.23% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_contains_vec_32_16               |       | 367.80M |  -2.25% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_first_blob_32_16             |       | 795.73M |  -2.25% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_blob_128_128                 |       | 855.27M |  -2.25% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_first_blob_32_32             |       | 809.20M |  -2.26% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_first_blob_4_128             |       | 371.73M |  -2.26% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_contains_vec512_u64  |       |   1.20B |  -2.28% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_first_blob_32_64             |       | 818.02M |  -2.30% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_first_blob_8_128             |       | 605.42M |  -2.33% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_blob_32_1024                 |       | 350.51M |  -2.34% |  0 |  0.00% |   0 |   0.00% |
|  ...   | ... 48 rows omitted ...                      |       |         |         |    |        |     |         |
|   -    | btreemap_v2_get_blob_32_16                   |       | 330.65M |  -3.19% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_last_u64_u64                 |       | 658.13M |  -3.28% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_blob_32_128                  |       | 341.65M |  -3.34% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_last_u64_vec8                |       | 648.83M |  -3.36% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_pop_last_u64_blob8               |       | 646.45M |  -3.38% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_insert_u64_blob512   |       | 606.23M |  -3.65% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_contains_u64_blob8               |       | 219.63M |  -3.67% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_contains_u64_vec8                |       | 219.63M |  -3.67% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_blob_4_128                   |       | 254.91M |  -3.68% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_contains_blob_4_128              |       | 243.88M |  -3.69% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_remove_u64_u64       |       | 746.31M |  -3.70% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_blob_32_64                   |       | 336.81M |  -3.95% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_insert_u64_u64       |       | 519.46M |  -4.17% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_contains_u64_u64                 |       | 223.28M |  -4.21% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_u64_blob8                    |       | 226.45M |  -4.44% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_vec_32_4                     |       | 358.35M |  -4.55% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_u64_vec8                     |       | 226.94M |  -4.55% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_contains_vec_32_4                |       | 352.56M |  -4.62% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_get_u64_u64                      |       | 230.53M |  -5.57% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_contains_u64_vec512  |       | 367.04M |  -6.61% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_get_u64_vec512       |       | 388.83M |  -7.14% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_get_u64_blob512      |       | 295.41M |  -8.14% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_contains_u64_u64     |       | 283.52M |  -8.41% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_contains_u64_blob512 |       | 278.86M |  -8.53% |  0 |  0.00% |   0 |   0.00% |
|   -    | btreemap_v2_mem_manager_get_u64_u64          |       | 291.72M |  -8.91% |  0 |  0.00% |   0 |   0.00% |

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

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

@maksymar maksymar changed the title perf: read node children in one go perf: read/write BTreeMap node's children in batch Jul 7, 2025
@maksymar maksymar requested a review from Copilot July 7, 2025 14:56

This comment was marked as outdated.

@maksymar maksymar changed the title perf: read/write BTreeMap node's children in batch perf: add batch read of BTreeMap node children addresses Jul 7, 2025
@maksymar maksymar requested a review from Copilot July 7, 2025 15:10

This comment was marked as outdated.

@maksymar maksymar requested a review from Copilot July 7, 2025 15:15

This comment was marked as outdated.

@maksymar maksymar marked this pull request as ready for review July 7, 2025 15:25
@maksymar maksymar requested a review from a team as a code owner July 7, 2025 15:25
Comment thread src/btreemap/node/v2.rs
Comment thread src/lib.rs Outdated

This comment was marked as outdated.

@maksymar maksymar requested a review from Copilot July 8, 2025 07:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Improves BTreeMap performance by introducing a batch-read helper and using it when loading internal node children.

  • Adds read_address_vec in src/lib.rs for efficient bulk reading of Address values.
  • Refactors child-loading in src/btreemap/node/v2.rs to choose between individual reads and batch reads based on a threshold.
  • Updates imports in src/btreemap/node.rs and refreshes benchmark results.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/lib.rs New read_address_vec function for batched Address reads.
src/btreemap/node/v2.rs Switched internal-node child-loading to use read_address_vec.
src/btreemap/node.rs Imported read_address_vec.
benchmarks/compare/canbench_results.yml Updated instruction counts after performance changes.
benchmarks/btreeset/canbench_results.yml Updated instruction counts after performance changes.
benchmarks/btreemap/canbench_results.yml Updated instruction counts after performance changes.
Comments suppressed due to low confidence (1)

src/lib.rs:134

  • This new utility function read_address_vec ingests raw bytes into addresses; consider adding a unit test for read_address_vec to validate correct behavior for different count values and boundary cases.
    read_to_vec(m, addr, &mut buf, byte_len);

Comment thread src/lib.rs
Comment thread src/btreemap/node/v2.rs
@maksymar maksymar enabled auto-merge (squash) July 8, 2025 09:11
@maksymar maksymar merged commit 2074af9 into main Jul 8, 2025
15 checks passed
@maksymar maksymar deleted the maksym/save_v2 branch July 8, 2025 09:23
maksymar added a commit that referenced this pull request Jul 8, 2025
This PR introduces batch writing of BTreeMap node children addresses to
improve performance.

This PR follows the idea from
#364 to batch
individual writes.
Although there are no significant changes overall, the majority of cases
show slight improvements (p25–p75: approx -0.07%), with: worst +0.27%,
median 0.00%, best -1.28%.

btreemap
```
  instructions:
    status:   No significant changes 👍
    counts:   [total 285 | regressed 0 | improved 0 | new 0 | unchanged 285]
    change:   [max +1.43M | p75 0 | median 0 | p25 -637.40K | min -10.48M]
    change %: [max +0.27% | p75 0.00% | median 0.00% | p25 -0.07% | min -1.28%]
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants