Skip to content

Commit 62b3624

Browse files
authored
Rollup merge of #71523 - Mark-Simulacrum:alloc-inline-dup, r=Amanieu
Take a single root node in range_search The unsafe code can be justified within range_search, as it makes sure to not overlap the returned references, but from the callers perspective it's an entirely safe algorithm and there's no need for the caller to know about the duplication. cc @ssomers r? @Amanieu
2 parents 4762e22 + e6cf6a7 commit 62b3624

File tree

1 file changed

+7
-10
lines changed
  • src/liballoc/collections/btree

1 file changed

+7
-10
lines changed

src/liballoc/collections/btree/map.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10341034
R: RangeBounds<T>,
10351035
{
10361036
if let Some(root) = &self.root {
1037-
let root1 = root.as_ref();
1038-
let root2 = root.as_ref();
1039-
let (f, b) = range_search(root1, root2, range);
1037+
let (f, b) = range_search(root.as_ref(), range);
10401038

10411039
Range { front: Some(f), back: Some(b) }
10421040
} else {
@@ -1082,9 +1080,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
10821080
R: RangeBounds<T>,
10831081
{
10841082
if let Some(root) = &mut self.root {
1085-
let root1 = root.as_mut();
1086-
let root2 = unsafe { ptr::read(&root1) };
1087-
let (f, b) = range_search(root1, root2, range);
1083+
let (f, b) = range_search(root.as_mut(), range);
10881084

10891085
RangeMut { front: Some(f), back: Some(b), _marker: PhantomData }
10901086
} else {
@@ -2043,8 +2039,7 @@ where
20432039
}
20442040

20452041
fn range_search<BorrowType, K, V, Q: ?Sized, R: RangeBounds<Q>>(
2046-
root1: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
2047-
root2: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
2042+
root: NodeRef<BorrowType, K, V, marker::LeafOrInternal>,
20482043
range: R,
20492044
) -> (
20502045
Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>,
@@ -2064,8 +2059,10 @@ where
20642059
_ => {}
20652060
};
20662061

2067-
let mut min_node = root1;
2068-
let mut max_node = root2;
2062+
// We duplicate the root NodeRef here -- we will never access it in a way
2063+
// that overlaps references obtained from the root.
2064+
let mut min_node = unsafe { ptr::read(&root) };
2065+
let mut max_node = root;
20692066
let mut min_found = false;
20702067
let mut max_found = false;
20712068

0 commit comments

Comments
 (0)