Skip to content

Commit 89524d0

Browse files
committedDec 26, 2020
Auto merge of #79520 - ssomers:btree_cleanup_1, r=Mark-Simulacrum
BTreeMap: clean up access to MaybeUninit arrays Stop exposing and using immutable access to `MaybeUninit` slices when we need and have exclusive access to the tree. r? `@Mark-Simulacrum`
2 parents 30a4273 + 0d2548a commit 89524d0

File tree

1 file changed

+17
-64
lines changed
  • library/alloc/src/collections/btree

1 file changed

+17
-64
lines changed
 

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

+17-64
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,6 @@ impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
295295
}
296296
}
297297

298-
impl<'a, K, V> NodeRef<marker::Immut<'a>, K, V, marker::Internal> {
299-
/// Exposes the data of an internal node in an immutable tree.
300-
fn as_internal(this: &Self) -> &'a InternalNode<K, V> {
301-
let ptr = Self::as_internal_ptr(this);
302-
// SAFETY: there can be no mutable references into this tree borrowed as `Immut`.
303-
unsafe { &*ptr }
304-
}
305-
}
306-
307298
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
308299
/// Borrows exclusive access to the data of an internal node.
309300
fn as_internal_mut(&mut self) -> &mut InternalNode<K, V> {
@@ -368,17 +359,6 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
368359
}
369360
}
370361

371-
impl<'a, K, V> NodeRef<marker::Immut<'a>, K, V, marker::Internal> {
372-
/// Exposes the contents of one of the edges in the node.
373-
///
374-
/// # Safety
375-
/// The node has more than `idx` initialized elements.
376-
unsafe fn edge_at(self, idx: usize) -> &'a BoxedNode<K, V> {
377-
debug_assert!(idx <= self.len());
378-
unsafe { Self::as_internal(&self).edges.get_unchecked(idx).assume_init_ref() }
379-
}
380-
}
381-
382362
impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
383363
/// Finds the parent of the current node. Returns `Ok(handle)` if the current
384364
/// node actually has a parent, where `handle` points to the edge of the parent
@@ -550,31 +530,6 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
550530
}
551531
}
552532

553-
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
554-
/// Exposes the entire key storage area in the node,
555-
/// regardless of the node's current length,
556-
/// having exclusive access to the entire node.
557-
unsafe fn key_area(self) -> &'a [MaybeUninit<K>] {
558-
self.into_leaf().keys.as_slice()
559-
}
560-
561-
/// Exposes the entire value storage area in the node,
562-
/// regardless of the node's current length,
563-
/// having exclusive access to the entire node.
564-
unsafe fn val_area(self) -> &'a [MaybeUninit<V>] {
565-
self.into_leaf().vals.as_slice()
566-
}
567-
}
568-
569-
impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::Internal> {
570-
/// Exposes the entire storage area for edge contents in the node,
571-
/// regardless of the node's current length,
572-
/// having exclusive access to the entire node.
573-
unsafe fn edge_area(self) -> &'a [MaybeUninit<BoxedNode<K, V>>] {
574-
Self::as_internal(&self).edges.as_slice()
575-
}
576-
}
577-
578533
impl<'a, K, V, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
579534
/// # Safety
580535
/// - The node has more than `idx` initialized elements.
@@ -707,12 +662,12 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
707662
let idx = self.len() - 1;
708663

709664
unsafe {
710-
let key = ptr::read(self.reborrow().key_at(idx));
711-
let val = ptr::read(self.reborrow().val_at(idx));
665+
let key = self.key_area_mut_at(idx).assume_init_read();
666+
let val = self.val_area_mut_at(idx).assume_init_read();
712667
let edge = match self.reborrow_mut().force() {
713668
ForceResult::Leaf(_) => None,
714-
ForceResult::Internal(internal) => {
715-
let node = ptr::read(internal.reborrow().edge_at(idx + 1));
669+
ForceResult::Internal(mut internal) => {
670+
let node = internal.edge_area_mut_at(idx + 1).assume_init_read();
716671
let mut edge = Root { node, height: internal.height - 1, _marker: PhantomData };
717672
// Currently, clearing the parent link is superfluous, because we will
718673
// insert the node elsewhere and set its parent link again.
@@ -1172,16 +1127,16 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
11721127
let new_len = self.node.len() - self.idx - 1;
11731128
new_node.len = new_len as u16;
11741129
unsafe {
1175-
let k = ptr::read(self.node.reborrow().key_at(self.idx));
1176-
let v = ptr::read(self.node.reborrow().val_at(self.idx));
1130+
let k = self.node.key_area_mut_at(self.idx).assume_init_read();
1131+
let v = self.node.val_area_mut_at(self.idx).assume_init_read();
11771132

11781133
ptr::copy_nonoverlapping(
1179-
self.node.reborrow().key_area().as_ptr().add(self.idx + 1),
1134+
self.node.key_area_mut_at(self.idx + 1..).as_ptr(),
11801135
new_node.keys.as_mut_ptr(),
11811136
new_len,
11821137
);
11831138
ptr::copy_nonoverlapping(
1184-
self.node.reborrow().val_area().as_ptr().add(self.idx + 1),
1139+
self.node.val_area_mut_at(self.idx + 1..).as_ptr(),
11851140
new_node.vals.as_mut_ptr(),
11861141
new_len,
11871142
);
@@ -1240,7 +1195,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
12401195
let kv = self.split_leaf_data(&mut new_node.data);
12411196
let new_len = usize::from(new_node.data.len);
12421197
ptr::copy_nonoverlapping(
1243-
self.node.reborrow().edge_area().as_ptr().add(self.idx + 1),
1198+
self.node.edge_area_mut_at(self.idx + 1..).as_ptr(),
12441199
new_node.edges.as_mut_ptr(),
12451200
new_len + 1,
12461201
);
@@ -1352,7 +1307,7 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
13521307
let old_parent_len = parent_node.len();
13531308
let mut left_node = self.left_child;
13541309
let old_left_len = left_node.len();
1355-
let right_node = self.right_child;
1310+
let mut right_node = self.right_child;
13561311
let right_len = right_node.len();
13571312
let new_left_len = old_left_len + 1 + right_len;
13581313

@@ -1370,7 +1325,7 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
13701325
slice_remove(parent_node.key_area_mut_at(..old_parent_len), parent_idx);
13711326
left_node.key_area_mut_at(old_left_len).write(parent_key);
13721327
ptr::copy_nonoverlapping(
1373-
right_node.reborrow().key_area().as_ptr(),
1328+
right_node.key_area_mut_at(..).as_ptr(),
13741329
left_node.key_area_mut_at(old_left_len + 1..).as_mut_ptr(),
13751330
right_len,
13761331
);
@@ -1379,7 +1334,7 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
13791334
slice_remove(parent_node.val_area_mut_at(..old_parent_len), parent_idx);
13801335
left_node.val_area_mut_at(old_left_len).write(parent_val);
13811336
ptr::copy_nonoverlapping(
1382-
right_node.reborrow().val_area().as_ptr(),
1337+
right_node.val_area_mut_at(..).as_ptr(),
13831338
left_node.val_area_mut_at(old_left_len + 1..).as_mut_ptr(),
13841339
right_len,
13851340
);
@@ -1392,9 +1347,9 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
13921347
// SAFETY: the height of the nodes being merged is one below the height
13931348
// of the node of this edge, thus above zero, so they are internal.
13941349
let mut left_node = left_node.reborrow_mut().cast_to_internal_unchecked();
1395-
let right_node = right_node.cast_to_internal_unchecked();
1350+
let mut right_node = right_node.cast_to_internal_unchecked();
13961351
ptr::copy_nonoverlapping(
1397-
right_node.reborrow().edge_area().as_ptr(),
1352+
right_node.edge_area_mut_at(..).as_ptr(),
13981353
left_node.edge_area_mut_at(old_left_len + 1..).as_mut_ptr(),
13991354
right_len + 1,
14001355
);
@@ -1503,7 +1458,6 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
15031458
match (left_node.reborrow_mut().force(), right_node.reborrow_mut().force()) {
15041459
(ForceResult::Internal(left), ForceResult::Internal(mut right)) => {
15051460
// Make room for stolen edges.
1506-
let left = left.reborrow();
15071461
let right_edges = right.edge_area_mut_at(..).as_mut_ptr();
15081462
ptr::copy(right_edges, right_edges.add(count), old_right_len + 1);
15091463
right.correct_childrens_parent_links(count..new_right_len + 1);
@@ -1561,7 +1515,7 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
15611515
match (left_node.reborrow_mut().force(), right_node.reborrow_mut().force()) {
15621516
(ForceResult::Internal(left), ForceResult::Internal(mut right)) => {
15631517
// Steal edges.
1564-
move_edges(right.reborrow(), 0, left, old_left_len + 1, count);
1518+
move_edges(right.reborrow_mut(), 0, left, old_left_len + 1, count);
15651519

15661520
// Fill gap where stolen edges used to be.
15671521
let right_edges = right.edge_area_mut_at(..).as_mut_ptr();
@@ -1590,14 +1544,14 @@ unsafe fn move_kv<K, V>(
15901544

15911545
// Source and destination must have the same height.
15921546
unsafe fn move_edges<'a, K: 'a, V: 'a>(
1593-
source: NodeRef<marker::Immut<'a>, K, V, marker::Internal>,
1547+
mut source: NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
15941548
source_offset: usize,
15951549
mut dest: NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
15961550
dest_offset: usize,
15971551
count: usize,
15981552
) {
15991553
unsafe {
1600-
let source_ptr = source.edge_area().as_ptr();
1554+
let source_ptr = source.edge_area_mut_at(..).as_ptr();
16011555
let dest_ptr = dest.edge_area_mut_at(dest_offset..).as_mut_ptr();
16021556
ptr::copy_nonoverlapping(source_ptr.add(source_offset), dest_ptr, count);
16031557
dest.correct_childrens_parent_links(dest_offset..dest_offset + count);
@@ -1699,7 +1653,6 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, ma
16991653

17001654
match (left_node.force(), right_node.force()) {
17011655
(ForceResult::Internal(left), ForceResult::Internal(right)) => {
1702-
let left = left.reborrow();
17031656
move_edges(left, new_left_len + 1, right, 1, new_right_len);
17041657
}
17051658
(ForceResult::Leaf(_), ForceResult::Leaf(_)) => {}

0 commit comments

Comments
 (0)