perf: faster node traversal and entry extraction in BTreeMap#363
perf: faster node traversal and entry extraction in BTreeMap#363
Conversation
There was a problem hiding this comment.
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_entrytoextract_entry_atand changed its signature to take&mut self. - Replaced
mem::take+nthwithswap_removefor O(1) removal. - Updated traversal logic in
btreemap.rsto 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);
|
|
|
|
|
Improve performance of node-tree traversal and entry extraction in
BTreeMap.mem::take+nthwithswap_removefor efficient O(1) removalselfto&mut selfinextract_entry_atto avoid unnecessary movesinto_entrytoextract_entry_atto match Rust naming conventions (into_*typically consumesself)btreemap
Google sheets CSV report.