Skip to content

Commit 78db333

Browse files
authored
Rollup merge of rust-lang#67725 - ssomers:into_key_slice_mut, r=RalfJung
Simplify into_key_slice_mut Remove a rare and tiny but superfluous run-time check from into_key_slice_mut. In rust-lang#67459, I wrote that "`get_mut` [...] does visit `into_key_slice_mut`" and that was wrong. No function that operates on a map that (still) has a shared root ever dives into `into_key_slice_mut`. So it's more clear to remove the (previously existing, and always incomplete) code it has for dealing with shared roots, as well as a petty performance improvement for those using exotically aligned key types. ~~Also, some testing of the `range` function initially added to rust-lang#67686 but hardly related.~~ r? @RalfJung
2 parents 02ef0dc + 9b92bf8 commit 78db333

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/liballoc/collections/btree/node.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
397397

398398
/// Borrows a view into the values stored in the node.
399399
/// The caller must ensure that the node is not the shared root.
400+
/// This function is not public, so doesn't have to support shared roots like `keys` does.
400401
fn vals(&self) -> &[V] {
401402
self.reborrow().into_val_slice()
402403
}
@@ -514,6 +515,7 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
514515
}
515516

516517
/// The caller must ensure that the node is not the shared root.
518+
/// This function is not public, so doesn't have to support shared roots like `keys` does.
517519
fn keys_mut(&mut self) -> &mut [K] {
518520
unsafe { self.reborrow_mut().into_key_slice_mut() }
519521
}
@@ -589,20 +591,15 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
589591
unsafe { &mut *(self.root as *mut Root<K, V>) }
590592
}
591593

594+
/// The caller must ensure that the node is not the shared root.
592595
fn into_key_slice_mut(mut self) -> &'a mut [K] {
593-
// Same as for `into_key_slice` above, we try to avoid a run-time check.
594-
if (mem::align_of::<NodeHeader<K, V, K>>() > mem::align_of::<NodeHeader<K, V>>()
595-
|| mem::size_of::<NodeHeader<K, V, K>>() != mem::size_of::<NodeHeader<K, V>>())
596-
&& self.is_shared_root()
597-
{
598-
&mut []
599-
} else {
600-
unsafe {
601-
slice::from_raw_parts_mut(
602-
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
603-
self.len(),
604-
)
605-
}
596+
debug_assert!(!self.is_shared_root());
597+
// We cannot be the shared root, so `as_leaf_mut` is okay.
598+
unsafe {
599+
slice::from_raw_parts_mut(
600+
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
601+
self.len(),
602+
)
606603
}
607604
}
608605

src/liballoc/collections/btree/search.rs

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ where
4646
}
4747
}
4848

49+
/// Returns the index in the node at which the key (or an equivalent) exists
50+
/// or could exist, and whether it exists in the node itself. If it doesn't
51+
/// exist in the node itself, it may exist in the subtree with that index
52+
/// (if the node has subtrees). If the key doesn't exist in node or subtree,
53+
/// the returned index is the position or subtree to insert at.
4954
pub fn search_linear<BorrowType, K, V, Type, Q: ?Sized>(
5055
node: &NodeRef<BorrowType, K, V, Type>,
5156
key: &Q,
@@ -54,6 +59,12 @@ where
5459
Q: Ord,
5560
K: Borrow<Q>,
5661
{
62+
// This function is defined over all borrow types (immutable, mutable, owned),
63+
// and may be called on the shared root in each case.
64+
// Crucially, we use `keys()` here, i.e., we work with immutable data.
65+
// `keys_mut()` does not support the shared root, so we cannot use it.
66+
// Using `keys()` is fine here even if BorrowType is mutable, as all we return
67+
// is an index -- not a reference.
5768
for (i, k) in node.keys().iter().enumerate() {
5869
match key.cmp(k.borrow()) {
5970
Ordering::Greater => {}

0 commit comments

Comments
 (0)