@@ -47,8 +47,8 @@ const KV_IDX_CENTER: usize = B - 1;
47
47
const EDGE_IDX_LEFT_OF_CENTER : usize = B - 1 ;
48
48
const EDGE_IDX_RIGHT_OF_CENTER : usize = B ;
49
49
50
- /// The underlying representation of leaf nodes and part of the representation of internal nodes .
51
- struct LeafNode < K , V > {
50
+ /// Header of any node, defined separately to optimize initialization .
51
+ struct Head < K , V > {
52
52
/// We want to be covariant in `K` and `V`.
53
53
parent : Option < NonNull < InternalNode < K , V > > > ,
54
54
@@ -59,6 +59,11 @@ struct LeafNode<K, V> {
59
59
60
60
/// The number of keys and values this node stores.
61
61
len : u16 ,
62
+ }
63
+
64
+ /// The underlying representation of leaf nodes and part of the representation of internal nodes.
65
+ struct LeafNode < K , V > {
66
+ head : Head < K , V > ,
62
67
63
68
/// The arrays storing the actual data of the node. Only the first `len` elements of each
64
69
/// array are initialized and valid.
@@ -72,15 +77,16 @@ impl<K, V> LeafNode<K, V> {
72
77
// As a general policy, we leave fields uninitialized if they can be, as this should
73
78
// be both slightly faster and easier to track in Valgrind.
74
79
unsafe {
75
- // parent_idx, keys, and vals are all MaybeUninit
76
- ptr:: addr_of_mut!( ( * this) . parent) . write ( None ) ;
77
- ptr:: addr_of_mut!( ( * this) . len) . write ( 0 ) ;
80
+ ptr:: addr_of_mut!( ( * this) . head) . write ( Head {
81
+ parent : None ,
82
+ parent_idx : MaybeUninit :: uninit ( ) ,
83
+ len : 0 ,
84
+ } ) ;
78
85
}
79
86
}
80
87
81
- /// Creates a new boxed `LeafNode`. Unsafe because all nodes should really be hidden behind
82
- /// `BoxedNode`, preventing accidental dropping of uninitialized keys and values.
83
- unsafe fn new ( ) -> Box < Self > {
88
+ /// Creates a new boxed `LeafNode`.
89
+ fn new ( ) -> Box < Self > {
84
90
unsafe {
85
91
let mut leaf = Box :: new_uninit ( ) ;
86
92
LeafNode :: init ( leaf. as_mut_ptr ( ) ) ;
@@ -108,15 +114,14 @@ struct InternalNode<K, V> {
108
114
impl < K , V > InternalNode < K , V > {
109
115
/// Creates a new boxed `InternalNode`.
110
116
///
111
- /// This is unsafe for two reasons. First, it returns an owned `InternalNode` in a box, risking
112
- /// dropping of uninitialized fields. Second, an invariant of internal nodes is that `len + 1`
113
- /// edges are initialized and valid, meaning that even when the node is empty (having a
114
- /// `len` of 0), there must be one initialized and valid edge. This function does not set up
117
+ /// # Safety
118
+ /// An invariant of internal nodes is it has at least one
119
+ /// initialized and valid edge. This function does not set up
115
120
/// such an edge.
116
121
unsafe fn new ( ) -> Box < Self > {
117
122
unsafe {
118
123
let mut node = Box :: < Self > :: new_uninit ( ) ;
119
- // We only need to initialize the data; the edges are MaybeUninit.
124
+ // We only need to initialize the leaf data; the edges are MaybeUninit.
120
125
LeafNode :: init ( ptr:: addr_of_mut!( ( * node. as_mut_ptr( ) ) . data) ) ;
121
126
node. assume_init ( )
122
127
}
@@ -145,7 +150,7 @@ impl<K, V> Root<K, V> {
145
150
146
151
impl < K , V > NodeRef < marker:: Owned , K , V , marker:: Leaf > {
147
152
fn new_leaf ( ) -> Self {
148
- Self :: from_new_leaf ( unsafe { LeafNode :: new ( ) } )
153
+ Self :: from_new_leaf ( LeafNode :: new ( ) )
149
154
}
150
155
151
156
fn from_new_leaf ( leaf : Box < LeafNode < K , V > > ) -> Self {
@@ -341,7 +346,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
341
346
pub fn len ( & self ) -> usize {
342
347
// Crucially, we only access the `len` field here. If BorrowType is marker::ValMut,
343
348
// there might be outstanding mutable references to values that we must not invalidate.
344
- unsafe { usize:: from ( ( * Self :: as_leaf_ptr ( self ) ) . len ) }
349
+ unsafe { usize:: from ( ( * Self :: as_leaf_ptr ( self ) ) . head . len ) }
345
350
}
346
351
347
352
/// Returns the number of levels that the node and leaves are apart. Zero
@@ -386,11 +391,11 @@ impl<BorrowType: marker::BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type>
386
391
// We need to use raw pointers to nodes because, if BorrowType is marker::ValMut,
387
392
// there might be outstanding mutable references to values that we must not invalidate.
388
393
let leaf_ptr: * const _ = Self :: as_leaf_ptr ( & self ) ;
389
- unsafe { ( * leaf_ptr) . parent }
394
+ unsafe { ( * leaf_ptr) . head . parent }
390
395
. as_ref ( )
391
396
. map ( |parent| Handle {
392
397
node : NodeRef :: from_internal ( * parent, self . height + 1 ) ,
393
- idx : unsafe { usize:: from ( ( * leaf_ptr) . parent_idx . assume_init ( ) ) } ,
398
+ idx : unsafe { usize:: from ( ( * leaf_ptr) . head . parent_idx . assume_init ( ) ) } ,
394
399
_marker : PhantomData ,
395
400
} )
396
401
. ok_or ( self )
@@ -431,9 +436,8 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
431
436
/// Borrows a view into the keys stored in the node.
432
437
pub fn keys ( & self ) -> & [ K ] {
433
438
let leaf = self . into_leaf ( ) ;
434
- unsafe {
435
- MaybeUninit :: slice_assume_init_ref ( leaf. keys . get_unchecked ( ..usize:: from ( leaf. len ) ) )
436
- }
439
+ let len = usize:: from ( leaf. head . len ) ;
440
+ unsafe { MaybeUninit :: slice_assume_init_ref ( leaf. keys . get_unchecked ( ..len) ) }
437
441
}
438
442
}
439
443
@@ -571,17 +575,17 @@ impl<'a, K, V, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
571
575
impl < ' a , K : ' a , V : ' a , Type > NodeRef < marker:: Mut < ' a > , K , V , Type > {
572
576
/// Borrows exclusive access to the length of the node.
573
577
pub fn len_mut ( & mut self ) -> & mut u16 {
574
- & mut self . as_leaf_mut ( ) . len
578
+ & mut self . as_leaf_mut ( ) . head . len
575
579
}
576
580
}
577
581
578
582
impl < ' a , K : ' a , V : ' a > NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > {
579
583
/// Sets the node's link to its parent edge,
580
584
/// without invalidating other references to the node.
581
585
fn set_parent_link ( & mut self , parent : NonNull < InternalNode < K , V > > , parent_idx : usize ) {
582
- let leaf = Self :: as_leaf_ptr ( self ) ;
583
- unsafe { ( * leaf ) . parent = Some ( parent) } ;
584
- unsafe { ( * leaf ) . parent_idx . write ( parent_idx as u16 ) } ;
586
+ let head = unsafe { & mut ( * Self :: as_leaf_ptr ( self ) ) . head } ;
587
+ head . parent = Some ( parent) ;
588
+ head . parent_idx . write ( parent_idx as u16 ) ;
585
589
}
586
590
}
587
591
@@ -590,7 +594,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
590
594
fn clear_parent_link ( & mut self ) {
591
595
let mut root_node = self . borrow_mut ( ) ;
592
596
let leaf = root_node. as_leaf_mut ( ) ;
593
- leaf. parent = None ;
597
+ leaf. head . parent = None ;
594
598
}
595
599
}
596
600
@@ -1057,7 +1061,7 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
1057
1061
debug_assert ! ( self . idx < self . node. len( ) ) ;
1058
1062
let old_len = self . node . len ( ) ;
1059
1063
let new_len = old_len - self . idx - 1 ;
1060
- new_node. len = new_len as u16 ;
1064
+ new_node. head . len = new_len as u16 ;
1061
1065
unsafe {
1062
1066
let k = self . node . key_area_mut ( self . idx ) . assume_init_read ( ) ;
1063
1067
let v = self . node . val_area_mut ( self . idx ) . assume_init_read ( ) ;
@@ -1086,14 +1090,12 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
1086
1090
/// - All the key-value pairs to the right of this handle are put into a newly
1087
1091
/// allocated node.
1088
1092
pub fn split ( mut self ) -> SplitResult < ' a , K , V , marker:: Leaf > {
1089
- unsafe {
1090
- let mut new_node = LeafNode :: new ( ) ;
1093
+ let mut new_node = LeafNode :: new ( ) ;
1091
1094
1092
- let kv = self . split_leaf_data ( & mut new_node) ;
1095
+ let kv = self . split_leaf_data ( & mut new_node) ;
1093
1096
1094
- let right = NodeRef :: from_new_leaf ( new_node) ;
1095
- SplitResult { left : self . node , kv, right }
1096
- }
1097
+ let right = NodeRef :: from_new_leaf ( new_node) ;
1098
+ SplitResult { left : self . node , kv, right }
1097
1099
}
1098
1100
1099
1101
/// Removes the key-value pair pointed to by this handle and returns it, along with the edge
@@ -1124,7 +1126,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1124
1126
unsafe {
1125
1127
let mut new_node = InternalNode :: new ( ) ;
1126
1128
let kv = self . split_leaf_data ( & mut new_node. data ) ;
1127
- let new_len = usize:: from ( new_node. data . len ) ;
1129
+ let new_len = usize:: from ( new_node. data . head . len ) ;
1128
1130
move_to_slice (
1129
1131
self . node . edge_area_mut ( self . idx + 1 ..old_len + 1 ) ,
1130
1132
& mut new_node. edges [ ..new_len + 1 ] ,
0 commit comments