Skip to content

Commit 567557f

Browse files
committed
Auto merge of #54924 - RalfJung:use-maybe-uninit2, r=cramertj
Use MaybeUninit in liballoc All code by @japaric. This is a re-submission of a part of #53508 that hopefully does not regress performance.
2 parents 77af314 + e4434be commit 567557f

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

src/etc/gdb_rust_pretty_printing.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,11 @@ def to_string(self):
322322
def children(self):
323323
(length, data_ptr) = \
324324
rustpp.extract_length_and_ptr_from_std_btreeset(self.__val)
325-
val = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3)
326-
gdb_ptr = val.get_wrapped_value()
325+
leaf_node = GdbValue(data_ptr.get_wrapped_value().dereference())
326+
maybe_uninit_keys = leaf_node.get_child_at_index(3)
327+
manually_drop_keys = maybe_uninit_keys.get_child_at_index(1)
328+
keys = manually_drop_keys.get_child_at_index(0)
329+
gdb_ptr = keys.get_wrapped_value()
327330
for index in xrange(length):
328331
yield (str(index), gdb_ptr[index])
329332

@@ -345,9 +348,14 @@ def to_string(self):
345348
def children(self):
346349
(length, data_ptr) = \
347350
rustpp.extract_length_and_ptr_from_std_btreemap(self.__val)
348-
keys = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(3)
351+
leaf_node = GdbValue(data_ptr.get_wrapped_value().dereference())
352+
maybe_uninit_keys = leaf_node.get_child_at_index(3)
353+
manually_drop_keys = maybe_uninit_keys.get_child_at_index(1)
354+
keys = manually_drop_keys.get_child_at_index(0)
349355
keys_ptr = keys.get_wrapped_value()
350-
vals = GdbValue(data_ptr.get_wrapped_value().dereference()).get_child_at_index(4)
356+
maybe_uninit_vals = leaf_node.get_child_at_index(4)
357+
manually_drop_vals = maybe_uninit_vals.get_child_at_index(1)
358+
vals = manually_drop_vals.get_child_at_index(0)
351359
vals_ptr = vals.get_wrapped_value()
352360
for index in xrange(length):
353361
yield (str(index), keys_ptr[index])

src/liballoc/collections/btree/node.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
// This implies that even an empty internal node has at least one edge.
4343

4444
use core::marker::PhantomData;
45-
use core::mem;
45+
use core::mem::{self, MaybeUninit};
4646
use core::ptr::{self, Unique, NonNull};
4747
use core::slice;
4848

@@ -58,9 +58,6 @@ pub const CAPACITY: usize = 2 * B - 1;
5858
/// these should always be put behind pointers, and specifically behind `BoxedNode` in the owned
5959
/// case.
6060
///
61-
/// See also rust-lang/rfcs#197, which would make this structure significantly more safe by
62-
/// avoiding accidentally dropping unused and uninitialized keys and values.
63-
///
6461
/// We put the metadata first so that its position is the same for every `K` and `V`, in order
6562
/// to statically allocate a single dummy node to avoid allocations. This struct is `repr(C)` to
6663
/// prevent them from being reordered.
@@ -73,7 +70,7 @@ struct LeafNode<K, V> {
7370
/// This node's index into the parent node's `edges` array.
7471
/// `*node.parent.edges[node.parent_idx]` should be the same thing as `node`.
7572
/// This is only guaranteed to be initialized when `parent` is nonnull.
76-
parent_idx: u16,
73+
parent_idx: MaybeUninit<u16>,
7774

7875
/// The number of keys and values this node stores.
7976
///
@@ -83,8 +80,8 @@ struct LeafNode<K, V> {
8380

8481
/// The arrays storing the actual data of the node. Only the first `len` elements of each
8582
/// array are initialized and valid.
86-
keys: [K; CAPACITY],
87-
vals: [V; CAPACITY],
83+
keys: MaybeUninit<[K; CAPACITY]>,
84+
vals: MaybeUninit<[V; CAPACITY]>,
8885
}
8986

9087
impl<K, V> LeafNode<K, V> {
@@ -94,10 +91,10 @@ impl<K, V> LeafNode<K, V> {
9491
LeafNode {
9592
// As a general policy, we leave fields uninitialized if they can be, as this should
9693
// be both slightly faster and easier to track in Valgrind.
97-
keys: mem::uninitialized(),
98-
vals: mem::uninitialized(),
94+
keys: MaybeUninit::uninitialized(),
95+
vals: MaybeUninit::uninitialized(),
9996
parent: ptr::null(),
100-
parent_idx: mem::uninitialized(),
97+
parent_idx: MaybeUninit::uninitialized(),
10198
len: 0
10299
}
103100
}
@@ -115,10 +112,10 @@ unsafe impl Sync for LeafNode<(), ()> {}
115112
// ever take a pointer past the first key.
116113
static EMPTY_ROOT_NODE: LeafNode<(), ()> = LeafNode {
117114
parent: ptr::null(),
118-
parent_idx: 0,
115+
parent_idx: MaybeUninit::uninitialized(),
119116
len: 0,
120-
keys: [(); CAPACITY],
121-
vals: [(); CAPACITY],
117+
keys: MaybeUninit::uninitialized(),
118+
vals: MaybeUninit::uninitialized(),
122119
};
123120

124121
/// The underlying representation of internal nodes. As with `LeafNode`s, these should be hidden
@@ -430,7 +427,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
430427
root: self.root,
431428
_marker: PhantomData
432429
},
433-
idx: self.as_leaf().parent_idx as usize,
430+
idx: unsafe { usize::from(*self.as_leaf().parent_idx.get_ref()) },
434431
_marker: PhantomData
435432
})
436433
} else {
@@ -567,7 +564,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
567564
// the node, which is allowed by LLVM.
568565
unsafe {
569566
slice::from_raw_parts(
570-
self.as_leaf().keys.as_ptr(),
567+
self.as_leaf().keys.as_ptr() as *const K,
571568
self.len()
572569
)
573570
}
@@ -578,7 +575,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
578575
debug_assert!(!self.is_shared_root());
579576
unsafe {
580577
slice::from_raw_parts(
581-
self.as_leaf().vals.as_ptr(),
578+
self.as_leaf().vals.as_ptr() as *const V,
582579
self.len()
583580
)
584581
}
@@ -605,7 +602,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
605602
} else {
606603
unsafe {
607604
slice::from_raw_parts_mut(
608-
&mut self.as_leaf_mut().keys as *mut [K] as *mut K,
605+
self.as_leaf_mut().keys.get_mut() as *mut [K] as *mut K,
609606
self.len()
610607
)
611608
}
@@ -616,7 +613,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
616613
debug_assert!(!self.is_shared_root());
617614
unsafe {
618615
slice::from_raw_parts_mut(
619-
&mut self.as_leaf_mut().vals as *mut [V] as *mut V,
616+
self.as_leaf_mut().vals.get_mut() as *mut [V] as *mut V,
620617
self.len()
621618
)
622619
}
@@ -1013,7 +1010,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
10131010
let ptr = self.node.as_internal_mut() as *mut _;
10141011
let mut child = self.descend();
10151012
child.as_leaf_mut().parent = ptr;
1016-
child.as_leaf_mut().parent_idx = idx;
1013+
child.as_leaf_mut().parent_idx.set(idx);
10171014
}
10181015

10191016
/// Unsafely asserts to the compiler some static information about whether the underlying
@@ -1152,12 +1149,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
11521149

11531150
ptr::copy_nonoverlapping(
11541151
self.node.keys().as_ptr().add(self.idx + 1),
1155-
new_node.keys.as_mut_ptr(),
1152+
new_node.keys.as_mut_ptr() as *mut K,
11561153
new_len
11571154
);
11581155
ptr::copy_nonoverlapping(
11591156
self.node.vals().as_ptr().add(self.idx + 1),
1160-
new_node.vals.as_mut_ptr(),
1157+
new_node.vals.as_mut_ptr() as *mut V,
11611158
new_len
11621159
);
11631160

@@ -1210,12 +1207,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12101207

12111208
ptr::copy_nonoverlapping(
12121209
self.node.keys().as_ptr().add(self.idx + 1),
1213-
new_node.data.keys.as_mut_ptr(),
1210+
new_node.data.keys.as_mut_ptr() as *mut K,
12141211
new_len
12151212
);
12161213
ptr::copy_nonoverlapping(
12171214
self.node.vals().as_ptr().add(self.idx + 1),
1218-
new_node.data.vals.as_mut_ptr(),
1215+
new_node.data.vals.as_mut_ptr() as *mut V,
12191216
new_len
12201217
);
12211218
ptr::copy_nonoverlapping(

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
#![feature(rustc_const_unstable)]
120120
#![feature(const_vec_new)]
121121
#![feature(slice_partition_dedup)]
122+
#![feature(maybe_uninit)]
122123

123124
// Allow testing this library
124125

0 commit comments

Comments
 (0)