Skip to content

Commit 3bb9eec

Browse files
committed
Auto merge of #89069 - bjorn3:optimize_sharded_new, r=Mark-Simulacrum
Use <[T; N]>::map in Sharded instead of SmallVec and unsafe code This results in a lot less assembly
2 parents db1fb85 + 7b50fd5 commit 3bb9eec

File tree

1 file changed

+1
-19
lines changed

1 file changed

+1
-19
lines changed

compiler/rustc_data_structures/src/sharded.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::fx::{FxHashMap, FxHasher};
22
use crate::sync::{Lock, LockGuard};
3-
use smallvec::SmallVec;
43
use std::borrow::Borrow;
54
use std::collections::hash_map::RawEntryMut;
65
use std::hash::{Hash, Hasher};
@@ -37,24 +36,7 @@ impl<T: Default> Default for Sharded<T> {
3736
impl<T> Sharded<T> {
3837
#[inline]
3938
pub fn new(mut value: impl FnMut() -> T) -> Self {
40-
// Create a vector of the values we want
41-
let mut values: SmallVec<[_; SHARDS]> =
42-
(0..SHARDS).map(|_| CacheAligned(Lock::new(value()))).collect();
43-
44-
// Create an uninitialized array
45-
let mut shards: mem::MaybeUninit<[CacheAligned<Lock<T>>; SHARDS]> =
46-
mem::MaybeUninit::uninit();
47-
48-
unsafe {
49-
// Copy the values into our array
50-
let first = shards.as_mut_ptr() as *mut CacheAligned<Lock<T>>;
51-
values.as_ptr().copy_to_nonoverlapping(first, SHARDS);
52-
53-
// Ignore the content of the vector
54-
values.set_len(0);
55-
56-
Sharded { shards: shards.assume_init() }
57-
}
39+
Sharded { shards: [(); SHARDS].map(|()| CacheAligned(Lock::new(value()))) }
5840
}
5941

6042
/// The shard is selected by hashing `val` with `FxHasher`.

0 commit comments

Comments
 (0)