Skip to content

Commit 687783d

Browse files
committed
refactor: optimize arena access in leaf split insertion
- Pull get_leaf_mut(leaf_id) and linked list update above conditional logic - Eliminate redundant arena lookup by updating leaf.next first - Maintain same functionality with cleaner code structure - All tests pass, no clippy warnings
1 parent 2a566af commit 687783d

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

rust/src/insert_operations.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ impl<K: Ord + Clone, V: Clone> BPlusTreeMap<K, V> {
5757

5858
// Node is full, need to split
5959
// Don't insert first. That causes the Vecs to overflow.
60-
// Split the full node - inline the split logic
6160

6261
// Calculate split point for better balance while ensuring both sides have at least min_keys
6362
let min_keys = leaf.capacity / 2; // min_keys() inlined
@@ -88,18 +87,19 @@ impl<K: Ord + Clone, V: Clone> BPlusTreeMap<K, V> {
8887
leaf_next, // Right node takes over the next pointer
8988
);
9089

91-
// Update the linked list and insert into the correct node with single get_leaf_mut call
90+
// Update the linked list first
91+
if let Some(leaf) = self.get_leaf_mut(leaf_id) {
92+
leaf.next = new_right_id;
93+
}
94+
95+
// Then insert into the correct node
9296
if index <= leaf_keys_len {
93-
// Insert into the original (left) leaf - combine linked list update with insertion
97+
// Insert into the original (left) leaf
9498
if let Some(leaf) = self.get_leaf_mut(leaf_id) {
95-
leaf.next = new_right_id; // Update linked list
96-
leaf.insert_at_index(index, key, value); // Insert key-value
99+
leaf.insert_at_index(index, key, value);
97100
}
98101
} else {
99-
// Update linked list in original leaf, then insert into the new (right) leaf
100-
if let Some(leaf) = self.get_leaf_mut(leaf_id) {
101-
leaf.next = new_right_id; // Update linked list
102-
}
102+
// Insert into the new (right) leaf
103103
if let Some(new_right) = self.get_leaf_mut(new_right_id) {
104104
new_right.insert_at_index(index - leaf_keys_len, key, value);
105105
}

0 commit comments

Comments
 (0)