Skip to content

Commit 4e33a54

Browse files
Remove shared root code and assertions from BTree nodes
1 parent 25e856b commit 4e33a54

File tree

1 file changed

+2
-51
lines changed
  • src/liballoc/collections/btree

1 file changed

+2
-51
lines changed

src/liballoc/collections/btree/node.rs

+2-51
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,6 @@ impl<K, V> LeafNode<K, V> {
111111
}
112112
}
113113

114-
impl<K, V> NodeHeader<K, V> {
115-
fn is_shared_root(&self) -> bool {
116-
ptr::eq(self, &EMPTY_ROOT_NODE as *const _ as *const _)
117-
}
118-
}
119-
120-
// We need to implement Sync here in order to make a static instance.
121-
unsafe impl Sync for NodeHeader<(), ()> {}
122-
123-
// An empty node used as a placeholder for the root node, to avoid allocations.
124-
// We use just a header in order to save space, since no operation on an empty tree will
125-
// ever take a pointer past the first key.
126-
static EMPTY_ROOT_NODE: NodeHeader<(), ()> =
127-
NodeHeader { parent: ptr::null(), parent_idx: MaybeUninit::uninit(), len: 0 };
128-
129114
/// The underlying representation of internal nodes. As with `LeafNode`s, these should be hidden
130115
/// behind `BoxedNode`s to prevent dropping uninitialized keys and values. Any pointer to an
131116
/// `InternalNode` can be directly casted to a pointer to the underlying `LeafNode` portion of the
@@ -154,8 +139,8 @@ impl<K, V> InternalNode<K, V> {
154139
}
155140

156141
/// A managed, non-null pointer to a node. This is either an owned pointer to
157-
/// `LeafNode<K, V>`, an owned pointer to `InternalNode<K, V>`, or a (not owned)
158-
/// pointer to `NodeHeader<(), ()` (more specifically, the pointer to EMPTY_ROOT_NODE).
142+
/// `LeafNode<K, V>` or an owned pointer to `InternalNode<K, V>`.
143+
///
159144
/// All of these types have a `NodeHeader<K, V>` prefix, meaning that they have at
160145
/// least the same size as `NodeHeader<K, V>` and store the same kinds of data at the same
161146
/// offsets; and they have a pointer alignment at least as large as `NodeHeader<K, V>`'s.
@@ -196,20 +181,6 @@ unsafe impl<K: Sync, V: Sync> Sync for Root<K, V> {}
196181
unsafe impl<K: Send, V: Send> Send for Root<K, V> {}
197182

198183
impl<K, V> Root<K, V> {
199-
/// Whether the instance of `Root` wraps a shared, empty root node. If not,
200-
/// the entire tree is uniquely owned by the owner of the `Root` instance.
201-
pub fn is_shared_root(&self) -> bool {
202-
self.as_ref().is_shared_root()
203-
}
204-
205-
/// Returns a shared tree, wrapping a shared root node that is eternally empty.
206-
pub fn shared_empty_root() -> Self {
207-
Root {
208-
node: unsafe { BoxedNode::from_ptr(NonNull::from(&EMPTY_ROOT_NODE).cast()) },
209-
height: 0,
210-
}
211-
}
212-
213184
/// Returns a new owned tree, with its own root node that is initially empty.
214185
pub fn new_leaf() -> Self {
215186
Root { node: BoxedNode::from_leaf(Box::new(unsafe { LeafNode::new() })), height: 0 }
@@ -245,7 +216,6 @@ impl<K, V> Root<K, V> {
245216
/// Adds a new internal node with a single edge, pointing to the previous root, and make that
246217
/// new node the root. This increases the height by 1 and is the opposite of `pop_level`.
247218
pub fn push_level(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::Internal> {
248-
debug_assert!(!self.is_shared_root());
249219
let mut new_node = Box::new(unsafe { InternalNode::new() });
250220
new_node.edges[0].write(unsafe { BoxedNode::from_ptr(self.node.as_ptr()) });
251221

@@ -381,19 +351,13 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
381351
/// Unsafe because the node must not be the shared root. For more information,
382352
/// see the `NodeRef` comments.
383353
unsafe fn as_leaf(&self) -> &LeafNode<K, V> {
384-
debug_assert!(!self.is_shared_root());
385354
self.node.as_ref()
386355
}
387356

388357
fn as_header(&self) -> &NodeHeader<K, V> {
389358
unsafe { &*(self.node.as_ptr() as *const NodeHeader<K, V>) }
390359
}
391360

392-
/// Returns whether the node is the shared, empty root.
393-
pub fn is_shared_root(&self) -> bool {
394-
self.as_header().is_shared_root()
395-
}
396-
397361
/// Borrows a view into the keys stored in the node.
398362
/// Unsafe because the caller must ensure that the node is not the shared root.
399363
pub unsafe fn keys(&self) -> &[K] {
@@ -464,7 +428,6 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
464428
pub unsafe fn deallocate_and_ascend(
465429
self,
466430
) -> Option<Handle<NodeRef<marker::Owned, K, V, marker::Internal>, marker::Edge>> {
467-
assert!(!self.is_shared_root());
468431
let height = self.height;
469432
let node = self.node;
470433
let ret = self.ascend().ok();
@@ -527,14 +490,12 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
527490
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
528491
/// Unsafe because the caller must ensure that the node is not the shared root.
529492
unsafe fn into_key_slice(self) -> &'a [K] {
530-
debug_assert!(!self.is_shared_root());
531493
// We cannot be the shared root, so `as_leaf` is okay.
532494
slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().keys), self.len())
533495
}
534496

535497
/// Unsafe because the caller must ensure that the node is not the shared root.
536498
unsafe fn into_val_slice(self) -> &'a [V] {
537-
debug_assert!(!self.is_shared_root());
538499
// We cannot be the shared root, so `as_leaf` is okay.
539500
slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().vals), self.len())
540501
}
@@ -555,7 +516,6 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
555516

556517
/// Unsafe because the caller must ensure that the node is not the shared root.
557518
unsafe fn into_key_slice_mut(mut self) -> &'a mut [K] {
558-
debug_assert!(!self.is_shared_root());
559519
// We cannot be the shared root, so `as_leaf_mut` is okay.
560520
slice::from_raw_parts_mut(
561521
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
@@ -565,7 +525,6 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
565525

566526
/// Unsafe because the caller must ensure that the node is not the shared root.
567527
unsafe fn into_val_slice_mut(mut self) -> &'a mut [V] {
568-
debug_assert!(!self.is_shared_root());
569528
slice::from_raw_parts_mut(
570529
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).vals),
571530
self.len(),
@@ -574,7 +533,6 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
574533

575534
/// Unsafe because the caller must ensure that the node is not the shared root.
576535
unsafe fn into_slices_mut(mut self) -> (&'a mut [K], &'a mut [V]) {
577-
debug_assert!(!self.is_shared_root());
578536
// We cannot use the getters here, because calling the second one
579537
// invalidates the reference returned by the first.
580538
// More precisely, it is the call to `len` that is the culprit,
@@ -592,7 +550,6 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
592550
/// Adds a key/value pair the end of the node.
593551
pub fn push(&mut self, key: K, val: V) {
594552
assert!(self.len() < CAPACITY);
595-
debug_assert!(!self.is_shared_root());
596553

597554
let idx = self.len();
598555

@@ -607,7 +564,6 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
607564
/// Adds a key/value pair to the beginning of the node.
608565
pub fn push_front(&mut self, key: K, val: V) {
609566
assert!(self.len() < CAPACITY);
610-
debug_assert!(!self.is_shared_root());
611567

612568
unsafe {
613569
slice_insert(self.keys_mut(), 0, key);
@@ -624,7 +580,6 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
624580
pub fn push(&mut self, key: K, val: V, edge: Root<K, V>) {
625581
assert!(edge.height == self.height - 1);
626582
assert!(self.len() < CAPACITY);
627-
debug_assert!(!self.is_shared_root());
628583

629584
let idx = self.len();
630585

@@ -658,7 +613,6 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
658613
pub fn push_front(&mut self, key: K, val: V, edge: Root<K, V>) {
659614
assert!(edge.height == self.height - 1);
660615
assert!(self.len() < CAPACITY);
661-
debug_assert!(!self.is_shared_root());
662616

663617
unsafe {
664618
slice_insert(self.keys_mut(), 0, key);
@@ -904,7 +858,6 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
904858
fn insert_fit(&mut self, key: K, val: V) -> *mut V {
905859
// Necessary for correctness, but in a private module
906860
debug_assert!(self.node.len() < CAPACITY);
907-
debug_assert!(!self.node.is_shared_root());
908861

909862
unsafe {
910863
slice_insert(self.node.keys_mut(), self.idx, key);
@@ -1081,7 +1034,6 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
10811034
/// - All the key/value pairs to the right of this handle are put into a newly
10821035
/// allocated node.
10831036
pub fn split(mut self) -> (NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, K, V, Root<K, V>) {
1084-
assert!(!self.node.is_shared_root());
10851037
unsafe {
10861038
let mut new_node = Box::new(LeafNode::new());
10871039

@@ -1113,7 +1065,6 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
11131065
pub fn remove(
11141066
mut self,
11151067
) -> (Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>, K, V) {
1116-
assert!(!self.node.is_shared_root());
11171068
unsafe {
11181069
let k = slice_remove(self.node.keys_mut(), self.idx);
11191070
let v = slice_remove(self.node.vals_mut(), self.idx);

0 commit comments

Comments
 (0)