Skip to content

Commit 55fa8af

Browse files
committed
BTreeMap: various tweaks
1 parent 3965524 commit 55fa8af

File tree

1 file changed

+50
-61
lines changed
  • library/alloc/src/collections/btree

1 file changed

+50
-61
lines changed

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

+50-61
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ const KV_IDX_CENTER: usize = B - 1;
4747
const EDGE_IDX_LEFT_OF_CENTER: usize = B - 1;
4848
const EDGE_IDX_RIGHT_OF_CENTER: usize = B;
4949

50-
/// The underlying representation of leaf nodes.
51-
#[repr(C)]
50+
/// The underlying representation of leaf nodes and part of the representation of internal nodes.
5251
struct LeafNode<K, V> {
5352
/// We want to be covariant in `K` and `V`.
5453
parent: Option<NonNull<InternalNode<K, V>>>,
@@ -59,9 +58,6 @@ struct LeafNode<K, V> {
5958
parent_idx: MaybeUninit<u16>,
6059

6160
/// The number of keys and values this node stores.
62-
///
63-
/// This next to `parent_idx` to encourage the compiler to join `len` and
64-
/// `parent_idx` into the same 32-bit word, reducing space overhead.
6561
len: u16,
6662

6763
/// The arrays storing the actual data of the node. Only the first `len` elements of each
@@ -92,7 +88,9 @@ impl<K, V> LeafNode<K, V> {
9288
/// node, allowing code to act on leaf and internal nodes generically without having to even check
9389
/// which of the two a pointer is pointing at. This property is enabled by the use of `repr(C)`.
9490
#[repr(C)]
91+
// gdb_providers.py uses this type name for introspection.
9592
struct InternalNode<K, V> {
93+
// gdb_providers.py uses this field name for introspection.
9694
data: LeafNode<K, V>,
9795

9896
/// The pointers to the children of this node. `len + 1` of these are considered
@@ -183,9 +181,9 @@ impl<K, V> Root<K, V> {
183181
NodeRef { height: self.height, node: self.node.as_ptr(), _marker: PhantomData }
184182
}
185183

186-
/// Adds a new internal node with a single edge, pointing to the previous root, and make that
187-
/// new node the root. This increases the height by 1 and is the opposite of
188-
/// `pop_internal_level`.
184+
/// Adds a new internal node with a single edge pointing to the previous root node,
185+
/// make that new node the root node, and return it. This increases the height by 1
186+
/// and is the opposite of `pop_internal_level`.
189187
pub fn push_internal_level(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::Internal> {
190188
let mut new_node = Box::new(unsafe { InternalNode::new() });
191189
new_node.edges[0].write(unsafe { BoxedNode::from_ptr(self.node.as_ptr()) });
@@ -322,7 +320,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
322320
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
323321
}
324322

325-
/// Exposes the leaf "portion" of any leaf or internal node.
323+
/// Exposes the leaf portion of any leaf or internal node.
326324
/// If the node is a leaf, this function simply opens up its data.
327325
/// If the node is an internal node, so not a leaf, it does have all the data a leaf has
328326
/// (header, keys and values), and this function exposes that.
@@ -472,7 +470,7 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
472470
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
473471
}
474472

475-
/// Exposes the leaf "portion" of any leaf or internal node for writing.
473+
/// Exposes the leaf portion of any leaf or internal node for writing.
476474
/// If the node is a leaf, this function simply opens up its data.
477475
/// If the node is an internal node, so not a leaf, it does have all the data a leaf has
478476
/// (header, keys and values), and this function exposes that.
@@ -498,18 +496,38 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
498496
unsafe { self.reborrow_mut().into_val_mut_at(idx) }
499497
}
500498

501-
fn keys_mut(&mut self) -> &mut [K] {
499+
fn keys_mut(&mut self) -> &mut [K]
500+
where
501+
K: 'a,
502+
V: 'a,
503+
{
502504
// SAFETY: the caller will not be able to call further methods on self
503505
// until the key slice reference is dropped, as we have unique access
504506
// for the lifetime of the borrow.
505-
unsafe { self.reborrow_mut().into_key_slice_mut() }
507+
// SAFETY: The keys of a node must always be initialized up to length.
508+
unsafe {
509+
slice::from_raw_parts_mut(
510+
MaybeUninit::slice_as_mut_ptr(&mut self.as_leaf_mut().keys),
511+
self.len(),
512+
)
513+
}
506514
}
507515

508-
fn vals_mut(&mut self) -> &mut [V] {
516+
fn vals_mut(&mut self) -> &mut [V]
517+
where
518+
K: 'a,
519+
V: 'a,
520+
{
509521
// SAFETY: the caller will not be able to call further methods on self
510522
// until the value slice reference is dropped, as we have unique access
511523
// for the lifetime of the borrow.
512-
unsafe { self.reborrow_mut().into_val_slice_mut() }
524+
// SAFETY: The values of a node must always be initialized up to length.
525+
unsafe {
526+
slice::from_raw_parts_mut(
527+
MaybeUninit::slice_as_mut_ptr(&mut self.as_leaf_mut().vals),
528+
self.len(),
529+
)
530+
}
513531
}
514532
}
515533

@@ -539,26 +557,6 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
539557
}
540558

541559
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
542-
fn into_key_slice_mut(mut self) -> &'a mut [K] {
543-
// SAFETY: The keys of a node must always be initialized up to length.
544-
unsafe {
545-
slice::from_raw_parts_mut(
546-
MaybeUninit::slice_as_mut_ptr(&mut self.as_leaf_mut().keys),
547-
self.len(),
548-
)
549-
}
550-
}
551-
552-
fn into_val_slice_mut(mut self) -> &'a mut [V] {
553-
// SAFETY: The values of a node must always be initialized up to length.
554-
unsafe {
555-
slice::from_raw_parts_mut(
556-
MaybeUninit::slice_as_mut_ptr(&mut self.as_leaf_mut().vals),
557-
self.len(),
558-
)
559-
}
560-
}
561-
562560
/// # Safety
563561
/// The node has more than `idx` initialized elements.
564562
unsafe fn into_key_mut_at(mut self, idx: usize) -> &'a mut K {
@@ -610,8 +608,8 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
610608
}
611609

612610
/// Adds a key/value pair to the beginning of the node.
613-
pub fn push_front(&mut self, key: K, val: V) {
614-
assert!(self.len() < CAPACITY);
611+
fn push_front(&mut self, key: K, val: V) {
612+
debug_assert!(self.len() < CAPACITY);
615613

616614
unsafe {
617615
slice_insert(self.keys_mut(), 0, key);
@@ -683,10 +681,9 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
683681
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
684682
/// Removes a key/value pair from the end of this node and returns the pair.
685683
/// If this is an internal node, also removes the edge that was to the right
686-
/// of that pair and returns the orphaned node that this edge owned with its
687-
/// parent erased.
688-
pub fn pop(&mut self) -> (K, V, Option<Root<K, V>>) {
689-
assert!(self.len() > 0);
684+
/// of that pair and returns the orphaned node that this edge owned.
685+
fn pop(&mut self) -> (K, V, Option<Root<K, V>>) {
686+
debug_assert!(self.len() > 0);
690687

691688
let idx = self.len() - 1;
692689

@@ -708,10 +705,11 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
708705
}
709706
}
710707

711-
/// Removes a key/value pair from the beginning of this node. If this is an internal node,
712-
/// also removes the edge that was to the left of that pair.
713-
pub fn pop_front(&mut self) -> (K, V, Option<Root<K, V>>) {
714-
assert!(self.len() > 0);
708+
/// Removes a key/value pair from the beginning of this node and returns the pair.
709+
/// If this is an internal node, also removes the edge that was to the left
710+
/// of that pair and returns the orphaned node that this edge owned.
711+
fn pop_front(&mut self) -> (K, V, Option<Root<K, V>>) {
712+
debug_assert!(self.len() > 0);
715713

716714
let old_len = self.len();
717715

@@ -913,7 +911,6 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
913911
/// this edge. This method assumes that there is enough space in the node for the new
914912
/// pair to fit.
915913
fn leafy_insert_fit(&mut self, key: K, val: V) {
916-
// Necessary for correctness, but in a private module
917914
debug_assert!(self.node.len() < CAPACITY);
918915

919916
unsafe {
@@ -951,18 +948,18 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
951948
let (middle_kv_idx, insertion) = splitpoint(self.idx);
952949
let middle = unsafe { Handle::new_kv(self.node, middle_kv_idx) };
953950
let (mut left, k, v, mut right) = middle.split();
954-
let val_ptr = match insertion {
951+
let mut insertion_edge = match insertion {
955952
InsertionPlace::Left(insert_idx) => unsafe {
956-
Handle::new_edge(left.reborrow_mut(), insert_idx).insert_fit(key, val)
953+
Handle::new_edge(left.reborrow_mut(), insert_idx)
957954
},
958955
InsertionPlace::Right(insert_idx) => unsafe {
959956
Handle::new_edge(
960957
right.node_as_mut().cast_unchecked::<marker::Leaf>(),
961958
insert_idx,
962959
)
963-
.insert_fit(key, val)
964960
},
965961
};
962+
let val_ptr = insertion_edge.insert_fit(key, val);
966963
(InsertResult::Split(SplitResult { left: left.forget_type(), k, v, right }), val_ptr)
967964
}
968965
}
@@ -985,8 +982,6 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
985982
/// between this edge and the key/value pair to the right of this edge. This method assumes
986983
/// that there is enough space in the node for the new pair to fit.
987984
fn insert_fit(&mut self, key: K, val: V, edge: Root<K, V>) {
988-
// Necessary for correctness, but in an internal module
989-
debug_assert!(self.node.len() < CAPACITY);
990985
debug_assert!(edge.height == self.node.height - 1);
991986

992987
unsafe {
@@ -1136,12 +1131,12 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
11361131

11371132
ptr::copy_nonoverlapping(
11381133
self.node.key_at(self.idx + 1),
1139-
new_node.keys.as_mut_ptr() as *mut K,
1134+
MaybeUninit::slice_as_mut_ptr(&mut new_node.keys),
11401135
new_len,
11411136
);
11421137
ptr::copy_nonoverlapping(
11431138
self.node.val_at(self.idx + 1),
1144-
new_node.vals.as_mut_ptr() as *mut V,
1139+
MaybeUninit::slice_as_mut_ptr(&mut new_node.vals),
11451140
new_len,
11461141
);
11471142

@@ -1376,9 +1371,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
13761371
move_edges(left, new_left_len + 1, right, 0, count);
13771372
}
13781373
(ForceResult::Leaf(_), ForceResult::Leaf(_)) => {}
1379-
_ => {
1380-
unreachable!();
1381-
}
1374+
_ => unreachable!(),
13821375
}
13831376
}
13841377
}
@@ -1433,9 +1426,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
14331426
right.correct_childrens_parent_links(0..=new_right_len);
14341427
}
14351428
(ForceResult::Leaf(_), ForceResult::Leaf(_)) => {}
1436-
_ => {
1437-
unreachable!();
1438-
}
1429+
_ => unreachable!(),
14391430
}
14401431
}
14411432
}
@@ -1568,9 +1559,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, ma
15681559
move_edges(left, left_new_len + 1, right, 1, right_new_len);
15691560
}
15701561
(ForceResult::Leaf(_), ForceResult::Leaf(_)) => {}
1571-
_ => {
1572-
unreachable!();
1573-
}
1562+
_ => unreachable!(),
15741563
}
15751564
}
15761565
}

0 commit comments

Comments
 (0)