Skip to content

Commit f965120

Browse files
committed
Auto merge of #77244 - ssomers:btree_love_the_leaf_edge, r=Mark-Simulacrum
BTreeMap: more refactoring around edges Continuation of #77005. r? `@Mark-Simulacrum`
2 parents 31530e5 + 76c466a commit f965120

File tree

1 file changed

+39
-52
lines changed
  • library/alloc/src/collections/btree

1 file changed

+39
-52
lines changed

library/alloc/src/collections/btree/node.rs

+39-52
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ impl<K, V> BoxedNode<K, V> {
127127
BoxedNode { ptr: Unique::from(&mut Box::leak(node).data) }
128128
}
129129

130-
unsafe fn from_ptr(ptr: NonNull<LeafNode<K, V>>) -> Self {
131-
BoxedNode { ptr: unsafe { Unique::new_unchecked(ptr.as_ptr()) } }
132-
}
133-
134130
fn as_ptr(&self) -> NonNull<LeafNode<K, V>> {
135131
NonNull::from(self.ptr)
136132
}
@@ -198,7 +194,7 @@ impl<K, V> Root<K, V> {
198194
/// and is the opposite of `pop_internal_level`.
199195
pub fn push_internal_level(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::Internal> {
200196
let mut new_node = Box::new(unsafe { InternalNode::new() });
201-
new_node.edges[0].write(unsafe { BoxedNode::from_ptr(self.node.as_ptr()) });
197+
new_node.edges[0].write(unsafe { ptr::read(&mut self.node) });
202198

203199
self.node = BoxedNode::from_internal(new_node);
204200
self.height += 1;
@@ -224,8 +220,8 @@ impl<K, V> Root<K, V> {
224220

225221
let top = self.node.ptr;
226222

227-
let internal_node = unsafe { self.internal_node_as_mut() };
228-
self.node = unsafe { BoxedNode::from_ptr(internal_node.first_edge().descend().node) };
223+
let mut internal_node = unsafe { self.internal_node_as_mut() };
224+
self.node = unsafe { internal_node.as_internal_mut().edges[0].assume_init_read() };
229225
self.height -= 1;
230226
self.node_as_mut().as_leaf_mut().parent = None;
231227

@@ -615,7 +611,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
615611

616612
/// Adds a key/value pair to the beginning of the node.
617613
fn push_front(&mut self, key: K, val: V) {
618-
debug_assert!(self.len() < CAPACITY);
614+
assert!(self.len() < CAPACITY);
619615

620616
unsafe {
621617
slice_insert(self.keys_mut(), 0, key);
@@ -668,14 +664,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
668664
unsafe {
669665
slice_insert(self.keys_mut(), 0, key);
670666
slice_insert(self.vals_mut(), 0, val);
671-
slice_insert(
672-
slice::from_raw_parts_mut(
673-
MaybeUninit::slice_as_mut_ptr(&mut self.as_internal_mut().edges),
674-
self.len() + 1,
675-
),
676-
0,
677-
edge.node,
678-
);
667+
slice_insert(self.edges_mut(), 0, edge.node);
679668
}
680669

681670
self.as_leaf_mut().len += 1;
@@ -925,33 +914,22 @@ fn splitpoint(edge_idx: usize) -> (usize, InsertionPlace) {
925914
}
926915
}
927916

928-
impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker::Edge> {
929-
/// Helps implementations of `insert_fit` for a particular `NodeType`,
930-
/// by taking care of leaf data.
917+
impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge> {
931918
/// Inserts a new key/value pair between the key/value pairs to the right and left of
932919
/// this edge. This method assumes that there is enough space in the node for the new
933920
/// pair to fit.
934-
fn leafy_insert_fit(&mut self, key: K, val: V) {
921+
///
922+
/// The returned pointer points to the inserted value.
923+
fn insert_fit(&mut self, key: K, val: V) -> *mut V {
935924
debug_assert!(self.node.len() < CAPACITY);
936925

937926
unsafe {
938927
slice_insert(self.node.keys_mut(), self.idx, key);
939928
slice_insert(self.node.vals_mut(), self.idx, val);
940-
941929
self.node.as_leaf_mut().len += 1;
942-
}
943-
}
944-
}
945930

946-
impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge> {
947-
/// Inserts a new key/value pair between the key/value pairs to the right and left of
948-
/// this edge. This method assumes that there is enough space in the node for the new
949-
/// pair to fit.
950-
///
951-
/// The returned pointer points to the inserted value.
952-
fn insert_fit(&mut self, key: K, val: V) -> *mut V {
953-
self.leafy_insert_fit(key, val);
954-
unsafe { self.node.val_mut_at(self.idx) }
931+
self.node.val_mut_at(self.idx)
932+
}
955933
}
956934
}
957935

@@ -1000,11 +978,14 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1000978
/// between this edge and the key/value pair to the right of this edge. This method assumes
1001979
/// that there is enough space in the node for the new pair to fit.
1002980
fn insert_fit(&mut self, key: K, val: V, edge: Root<K, V>) {
981+
debug_assert!(self.node.len() < CAPACITY);
1003982
debug_assert!(edge.height == self.node.height - 1);
1004983

1005984
unsafe {
985+
slice_insert(self.node.keys_mut(), self.idx, key);
986+
slice_insert(self.node.vals_mut(), self.idx, val);
1006987
slice_insert(self.node.edges_mut(), self.idx + 1, edge.node);
1007-
self.leafy_insert_fit(key, val);
988+
self.node.as_leaf_mut().len += 1;
1008989

1009990
self.node.correct_childrens_parent_links((self.idx + 1)..=self.node.len());
1010991
}
@@ -1135,15 +1116,21 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
11351116
}
11361117

11371118
impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker::KV> {
1119+
/// Helps implementations of `split` for a particular `NodeType`,
1120+
/// by calculating the length of the new node.
1121+
fn split_new_node_len(&self) -> usize {
1122+
debug_assert!(self.idx < self.node.len());
1123+
self.node.len() - self.idx - 1
1124+
}
1125+
11381126
/// Helps implementations of `split` for a particular `NodeType`,
11391127
/// by taking care of leaf data.
1140-
fn leafy_split(&mut self, new_node: &mut LeafNode<K, V>) -> (K, V, usize) {
1128+
fn split_leaf_data(&mut self, new_node: &mut LeafNode<K, V>) -> (K, V) {
1129+
let new_len = self.split_new_node_len();
11411130
unsafe {
11421131
let k = ptr::read(self.node.key_at(self.idx));
11431132
let v = ptr::read(self.node.val_at(self.idx));
11441133

1145-
let new_len = self.node.len() - self.idx - 1;
1146-
11471134
ptr::copy_nonoverlapping(
11481135
self.node.key_at(self.idx + 1),
11491136
MaybeUninit::slice_as_mut_ptr(&mut new_node.keys),
@@ -1157,15 +1144,15 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
11571144

11581145
self.node.as_leaf_mut().len = self.idx as u16;
11591146
new_node.len = new_len as u16;
1160-
(k, v, new_len)
1147+
(k, v)
11611148
}
11621149
}
11631150
}
11641151

11651152
impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV> {
11661153
/// Splits the underlying node into three parts:
11671154
///
1168-
/// - The node is truncated to only contain the key/value pairs to the right of
1155+
/// - The node is truncated to only contain the key/value pairs to the left of
11691156
/// this handle.
11701157
/// - The key and value pointed to by this handle are extracted.
11711158
/// - All the key/value pairs to the right of this handle are put into a newly
@@ -1174,9 +1161,10 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
11741161
unsafe {
11751162
let mut new_node = Box::new(LeafNode::new());
11761163

1177-
let (k, v, _) = self.leafy_split(&mut new_node);
1164+
let (k, v) = self.split_leaf_data(&mut new_node);
11781165

1179-
(self.node, k, v, Root { node: BoxedNode::from_leaf(new_node), height: 0 })
1166+
let right = Root { node: BoxedNode::from_leaf(new_node), height: 0 };
1167+
(self.node, k, v, right)
11801168
}
11811169
}
11821170

@@ -1210,29 +1198,28 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
12101198
/// Splits the underlying node into three parts:
12111199
///
12121200
/// - The node is truncated to only contain the edges and key/value pairs to the
1213-
/// right of this handle.
1201+
/// left of this handle.
12141202
/// - The key and value pointed to by this handle are extracted.
12151203
/// - All the edges and key/value pairs to the right of this handle are put into
12161204
/// a newly allocated node.
12171205
pub fn split(mut self) -> (NodeRef<marker::Mut<'a>, K, V, marker::Internal>, K, V, Root<K, V>) {
12181206
unsafe {
12191207
let mut new_node = Box::new(InternalNode::new());
1220-
1221-
let (k, v, new_len) = self.leafy_split(&mut new_node.data);
1222-
let height = self.node.height;
1223-
let old_node = &*self.node.as_internal_ptr();
1224-
1208+
// Move edges out before reducing length:
1209+
let new_len = self.split_new_node_len();
12251210
ptr::copy_nonoverlapping(
1226-
old_node.edges.as_ptr().add(self.idx + 1),
1227-
new_node.edges.as_mut_ptr(),
1211+
self.node.edge_at(self.idx + 1),
1212+
MaybeUninit::slice_as_mut_ptr(&mut new_node.edges),
12281213
new_len + 1,
12291214
);
1215+
let (k, v) = self.split_leaf_data(&mut new_node.data);
12301216

1231-
let mut new_root = Root { node: BoxedNode::from_internal(new_node), height };
1217+
let height = self.node.height;
1218+
let mut right = Root { node: BoxedNode::from_internal(new_node), height };
12321219

1233-
new_root.internal_node_as_mut().correct_childrens_parent_links(0..=new_len);
1220+
right.internal_node_as_mut().correct_childrens_parent_links(0..=new_len);
12341221

1235-
(self.node, k, v, new_root)
1222+
(self.node, k, v, right)
12361223
}
12371224
}
12381225

0 commit comments

Comments
 (0)