Skip to content

Commit 702758d

Browse files
Drop NodeHeader type from BTree code
We no longer have a separate header because the shared root is gone; all code can work solely with leafs now.
1 parent 76f9c96 commit 702758d

File tree

1 file changed

+5
-41
lines changed
  • src/liballoc/collections/btree

1 file changed

+5
-41
lines changed

src/liballoc/collections/btree/node.rs

+5-41
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,7 @@ const B: usize = 6;
4444
pub const MIN_LEN: usize = B - 1;
4545
pub const CAPACITY: usize = 2 * B - 1;
4646

47-
/// The underlying representation of leaf nodes. Note that it is often unsafe to actually store
48-
/// these, since only the first `len` keys and values are assumed to be initialized. As such,
49-
/// these should always be put behind pointers, and specifically behind `BoxedNode` in the owned
50-
/// case.
51-
///
52-
/// We have a separate type for the header and rely on it matching the prefix of `LeafNode`, in
53-
/// order to statically allocate a single dummy node to avoid allocations. This struct is
54-
/// `repr(C)` to prevent them from being reordered. `LeafNode` does not just contain a
55-
/// `NodeHeader` because we do not want unnecessary padding between `len` and the keys.
56-
/// Crucially, `NodeHeader` can be safely transmuted to different K and V. (This is exploited
57-
/// by `as_header`.)
58-
#[repr(C)]
59-
struct NodeHeader<K, V> {
60-
/// We use `*const` as opposed to `*mut` so as to be covariant in `K` and `V`.
61-
/// This either points to an actual node or is null.
62-
parent: *const InternalNode<K, V>,
63-
64-
/// This node's index into the parent node's `edges` array.
65-
/// `*node.parent.edges[node.parent_idx]` should be the same thing as `node`.
66-
/// This is only guaranteed to be initialized when `parent` is non-null.
67-
parent_idx: MaybeUninit<u16>,
68-
69-
/// The number of keys and values this node stores.
70-
///
71-
/// This next to `parent_idx` to encourage the compiler to join `len` and
72-
/// `parent_idx` into the same 32-bit word, reducing space overhead.
73-
len: u16,
74-
}
47+
/// The underlying representation of leaf nodes.
7548
#[repr(C)]
7649
struct LeafNode<K, V> {
7750
/// We use `*const` as opposed to `*mut` so as to be covariant in `K` and `V`.
@@ -141,10 +114,7 @@ impl<K, V> InternalNode<K, V> {
141114
/// A managed, non-null pointer to a node. This is either an owned pointer to
142115
/// `LeafNode<K, V>` or an owned pointer to `InternalNode<K, V>`.
143116
///
144-
/// All of these types have a `NodeHeader<K, V>` prefix, meaning that they have at
145-
/// least the same size as `NodeHeader<K, V>` and store the same kinds of data at the same
146-
/// offsets; and they have a pointer alignment at least as large as `NodeHeader<K, V>`'s.
147-
/// However, `BoxedNode` contains no information as to which of the three types
117+
/// However, `BoxedNode` contains no information as to which of the two types
148118
/// of nodes it actually contains, and, partially due to this lack of information,
149119
/// has no destructor.
150120
struct BoxedNode<K, V> {
@@ -279,8 +249,6 @@ impl<K, V> Root<K, V> {
279249
/// `Leaf`, the `NodeRef` points to a leaf node, when this is `Internal` the
280250
/// `NodeRef` points to an internal node, and when this is `LeafOrInternal` the
281251
/// `NodeRef` could be pointing to either type of node.
282-
///
283-
/// Turning this into a `NodeHeader` reference is always safe.
284252
pub struct NodeRef<BorrowType, K, V, Type> {
285253
/// The number of levels below the node.
286254
height: usize,
@@ -322,7 +290,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
322290
/// Note that, despite being safe, calling this function can have the side effect
323291
/// of invalidating mutable references that unsafe code has created.
324292
pub fn len(&self) -> usize {
325-
self.as_header().len as usize
293+
self.as_leaf().len as usize
326294
}
327295

328296
/// Returns the height of this node in the whole tree. Zero height denotes the
@@ -353,10 +321,6 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
353321
unsafe { self.node.as_ref() }
354322
}
355323

356-
fn as_header(&self) -> &NodeHeader<K, V> {
357-
unsafe { &*(self.node.as_ptr() as *const NodeHeader<K, V>) }
358-
}
359-
360324
/// Borrows a view into the keys stored in the node.
361325
pub fn keys(&self) -> &[K] {
362326
self.reborrow().into_key_slice()
@@ -377,7 +341,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
377341
pub fn ascend(
378342
self,
379343
) -> Result<Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::Edge>, Self> {
380-
let parent_as_leaf = self.as_header().parent as *const LeafNode<K, V>;
344+
let parent_as_leaf = self.as_leaf().parent as *const LeafNode<K, V>;
381345
if let Some(non_zero) = NonNull::new(parent_as_leaf as *mut _) {
382346
Ok(Handle {
383347
node: NodeRef {
@@ -386,7 +350,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
386350
root: self.root,
387351
_marker: PhantomData,
388352
},
389-
idx: unsafe { usize::from(*self.as_header().parent_idx.as_ptr()) },
353+
idx: unsafe { usize::from(*self.as_leaf().parent_idx.as_ptr()) },
390354
_marker: PhantomData,
391355
})
392356
} else {

0 commit comments

Comments
 (0)