Skip to content

perf: faster node traversal and entry extraction in BTreeMap#363

Merged
maksymar merged 1 commit intomainfrom
maksym/extract_entry_at
Jul 7, 2025
Merged

perf: faster node traversal and entry extraction in BTreeMap#363
maksymar merged 1 commit intomainfrom
maksym/extract_entry_at

Conversation

@maksymar
Copy link
Copy Markdown
Contributor

@maksymar maksymar commented Jul 7, 2025

Improve performance of node-tree traversal and entry extraction in BTreeMap.

  • Replaced mem::take + nth with swap_remove for efficient O(1) removal
  • Changed self to &mut self in extract_entry_at to avoid unnecessary moves
  • Renamed into_entry to extract_entry_at to match Rust naming conventions (into_* typically consumes self)

btreemap

  instructions:
    status:   Improvements detected 🟢
    counts:   [total 285 | regressed 0 | improved 1 | new 0 | unchanged 284]
    change:   [max +43.69M | p75 0 | median -542.00K | p25 -1.48M | min -13.09M]
    change %: [max +1.35% | p75 0.00% | median -0.04% | p25 -0.20% | min -2.37%]

Google sheets CSV report.

@maksymar maksymar requested a review from Copilot July 7, 2025 12:44
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

This PR refactors entry extraction for better performance by renaming into_entry and using in-place removal, and updates traversal logic and benchmarks accordingly.

  • Renamed into_entry to extract_entry_at and changed its signature to take &mut self.
  • Replaced mem::take + nth with swap_remove for O(1) removal.
  • Updated traversal logic in btreemap.rs to pass mutable nodes and call the new method.
  • Benchmarks YAML updated with new instruction counts.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/btreemap/node.rs Renamed and refactored entry-extraction method
src/btreemap.rs Updated traversal and extraction call sites
benchmarks/*.yml Adjusted benchmark results after refactor
Comments suppressed due to low confidence (3)

src/btreemap/node.rs:321

  • [nitpick] The doc comment is outdated: extract_entry_at no longer consumes the node. Update it to reflect that it mutably extracts without consuming.
    /// Returns the entry at the specified index while consuming this node.

src/btreemap/node.rs:322

  • [nitpick] Consider adding unit tests for extract_entry_at to verify correct removal behavior and that B-tree invariants hold after extraction.
    pub fn extract_entry_at<M: Memory>(&mut self, idx: usize, memory: &M) -> Entry<K> {

src/btreemap/node.rs:323

  • Using swap_remove here can break the sorted order of entries, violating B-tree invariants. Consider using Vec::remove(idx) to preserve ordering even if it is O(n).
        let (key, value) = self.entries.swap_remove(idx);

@maksymar maksymar changed the title perf: improve move perf: faster node traversal and entry extraction Jul 7, 2025
@maksymar maksymar changed the title perf: faster node traversal and entry extraction perf: faster node traversal and entry extraction in BTreeMap Jul 7, 2025
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 7, 2025

canbench 🏋 (dir: ./benchmarks/btreeset) 82edf8b 2025-07-07 12:51:06 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 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 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 Jul 7, 2025

canbench 🏋 (dir: ./benchmarks/btreemap) 82edf8b 2025-07-07 12:52:49 UTC

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

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

Summary:
  instructions:
    status:   Improvements detected 🟢
    counts:   [total 285 | regressed 0 | improved 1 | new 0 | unchanged 284]
    change:   [max +43.69M | p75 0 | median -542.00K | p25 -1.48M | min -13.09M]
    change %: [max +1.35% | p75 0.00% | median -0.04% | p25 -0.20% | min -2.37%]

  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_get_vec_32_1024 |       | 539.74M |  -2.37% |  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/compare) 82edf8b 2025-07-07 12:51:53 UTC

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

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

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 18 | regressed 0 | improved 0 | new 0 | unchanged 18]
    change:   [max +119.98M | p75 0 | median 0 | p25 0 | min -69.94K]
    change %: [max +0.27% | p75 0.00% | median 0.00% | p25 0.00% | min -0.01%]

  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 Jul 7, 2025

canbench 🏋 (dir: ./benchmarks/memory_manager) 82edf8b 2025-07-07 12:50:57 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/vec) 82edf8b 2025-07-07 12:51:05 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

Comment thread src/btreemap/node.rs
@maksymar maksymar marked this pull request as ready for review July 7, 2025 13:01
@maksymar maksymar requested a review from a team as a code owner July 7, 2025 13:01
@maksymar maksymar merged commit 829e9d0 into main Jul 7, 2025
23 checks passed
@maksymar maksymar deleted the maksym/extract_entry_at branch July 7, 2025 13:55
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.

3 participants