Skip to content

Commit 1102565

Browse files
committed
Auto merge of #54924 - RalfJung:use-maybe-uninit2, r=<try>
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 b1a137d + faa733d commit 1102565

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
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-20
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

@@ -73,7 +73,7 @@ struct LeafNode<K, V> {
7373
/// This node's index into the parent node's `edges` array.
7474
/// `*node.parent.edges[node.parent_idx]` should be the same thing as `node`.
7575
/// This is only guaranteed to be initialized when `parent` is nonnull.
76-
parent_idx: u16,
76+
parent_idx: MaybeUninit<u16>,
7777

7878
/// The number of keys and values this node stores.
7979
///
@@ -83,8 +83,8 @@ struct LeafNode<K, V> {
8383

8484
/// The arrays storing the actual data of the node. Only the first `len` elements of each
8585
/// array are initialized and valid.
86-
keys: [K; CAPACITY],
87-
vals: [V; CAPACITY],
86+
keys: MaybeUninit<[K; CAPACITY]>,
87+
vals: MaybeUninit<[V; CAPACITY]>,
8888
}
8989

9090
impl<K, V> LeafNode<K, V> {
@@ -94,10 +94,10 @@ impl<K, V> LeafNode<K, V> {
9494
LeafNode {
9595
// As a general policy, we leave fields uninitialized if they can be, as this should
9696
// be both slightly faster and easier to track in Valgrind.
97-
keys: mem::uninitialized(),
98-
vals: mem::uninitialized(),
97+
keys: MaybeUninit::uninitialized(),
98+
vals: MaybeUninit::uninitialized(),
9999
parent: ptr::null(),
100-
parent_idx: mem::uninitialized(),
100+
parent_idx: MaybeUninit::uninitialized(),
101101
len: 0
102102
}
103103
}
@@ -115,10 +115,10 @@ unsafe impl Sync for LeafNode<(), ()> {}
115115
// ever take a pointer past the first key.
116116
static EMPTY_ROOT_NODE: LeafNode<(), ()> = LeafNode {
117117
parent: ptr::null(),
118-
parent_idx: 0,
118+
parent_idx: MaybeUninit::uninitialized(),
119119
len: 0,
120-
keys: [(); CAPACITY],
121-
vals: [(); CAPACITY],
120+
keys: MaybeUninit::uninitialized(),
121+
vals: MaybeUninit::uninitialized(),
122122
};
123123

124124
/// The underlying representation of internal nodes. As with `LeafNode`s, these should be hidden
@@ -430,7 +430,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
430430
root: self.root,
431431
_marker: PhantomData
432432
},
433-
idx: self.as_leaf().parent_idx as usize,
433+
idx: unsafe { usize::from(*self.as_leaf().parent_idx.get_ref()) },
434434
_marker: PhantomData
435435
})
436436
} else {
@@ -567,7 +567,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
567567
// the node, which is allowed by LLVM.
568568
unsafe {
569569
slice::from_raw_parts(
570-
self.as_leaf().keys.as_ptr(),
570+
self.as_leaf().keys.as_ptr() as *const K,
571571
self.len()
572572
)
573573
}
@@ -578,7 +578,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
578578
debug_assert!(!self.is_shared_root());
579579
unsafe {
580580
slice::from_raw_parts(
581-
self.as_leaf().vals.as_ptr(),
581+
self.as_leaf().vals.as_ptr() as *const V,
582582
self.len()
583583
)
584584
}
@@ -605,7 +605,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
605605
} else {
606606
unsafe {
607607
slice::from_raw_parts_mut(
608-
&mut self.as_leaf_mut().keys as *mut [K] as *mut K,
608+
self.as_leaf_mut().keys.get_mut() as *mut [K] as *mut K,
609609
self.len()
610610
)
611611
}
@@ -616,7 +616,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
616616
debug_assert!(!self.is_shared_root());
617617
unsafe {
618618
slice::from_raw_parts_mut(
619-
&mut self.as_leaf_mut().vals as *mut [V] as *mut V,
619+
self.as_leaf_mut().vals.get_mut() as *mut [V] as *mut V,
620620
self.len()
621621
)
622622
}
@@ -1013,7 +1013,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
10131013
let ptr = self.node.as_internal_mut() as *mut _;
10141014
let mut child = self.descend();
10151015
child.as_leaf_mut().parent = ptr;
1016-
child.as_leaf_mut().parent_idx = idx;
1016+
child.as_leaf_mut().parent_idx.set(idx);
10171017
}
10181018

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

11531153
ptr::copy_nonoverlapping(
11541154
self.node.keys().as_ptr().add(self.idx + 1),
1155-
new_node.keys.as_mut_ptr(),
1155+
new_node.keys.as_mut_ptr() as *mut K,
11561156
new_len
11571157
);
11581158
ptr::copy_nonoverlapping(
11591159
self.node.vals().as_ptr().add(self.idx + 1),
1160-
new_node.vals.as_mut_ptr(),
1160+
new_node.vals.as_mut_ptr() as *mut V,
11611161
new_len
11621162
);
11631163

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

12111211
ptr::copy_nonoverlapping(
12121212
self.node.keys().as_ptr().add(self.idx + 1),
1213-
new_node.data.keys.as_mut_ptr(),
1213+
new_node.data.keys.as_mut_ptr() as *mut K,
12141214
new_len
12151215
);
12161216
ptr::copy_nonoverlapping(
12171217
self.node.vals().as_ptr().add(self.idx + 1),
1218-
new_node.data.vals.as_mut_ptr(),
1218+
new_node.data.vals.as_mut_ptr() as *mut V,
12191219
new_len
12201220
);
12211221
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)