mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-07 00:00:09 +02:00
chore: use functional slice building (#8097)
Noticed some redundant procedural code.
This commit is contained in:
@@ -90,17 +90,13 @@ fn make_fixed_size<T>(buffer: Box<[T]>) -> Box<[T; LOCAL_QUEUE_CAPACITY]> {
|
||||
|
||||
/// Create a new local run-queue
|
||||
pub(crate) fn local<T: 'static>() -> (Steal<T>, Local<T>) {
|
||||
let mut buffer = Vec::with_capacity(LOCAL_QUEUE_CAPACITY);
|
||||
|
||||
for _ in 0..LOCAL_QUEUE_CAPACITY {
|
||||
buffer.push(UnsafeCell::new(MaybeUninit::uninit()));
|
||||
}
|
||||
let buffer = std::iter::repeat_with(|| UnsafeCell::new(MaybeUninit::uninit()));
|
||||
|
||||
let inner = Arc::new(Inner {
|
||||
head: AtomicUnsignedLong::new(0),
|
||||
tail: AtomicUnsignedShort::new(0),
|
||||
lifo: task::AtomicNotified::empty(),
|
||||
buffer: make_fixed_size(buffer.into_boxed_slice()),
|
||||
buffer: make_fixed_size(buffer.take(LOCAL_QUEUE_CAPACITY).collect()),
|
||||
});
|
||||
|
||||
let local = Local {
|
||||
|
||||
@@ -50,13 +50,10 @@ pub(super) const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1;
|
||||
impl Wheel {
|
||||
/// Creates a new timing wheel.
|
||||
pub(crate) fn new() -> Wheel {
|
||||
let mut levels = Vec::with_capacity(NUM_LEVELS);
|
||||
for i in 0..NUM_LEVELS {
|
||||
levels.push(Level::new(i));
|
||||
}
|
||||
let levels = (0..NUM_LEVELS).map(Level::new).collect::<Box<_>>();
|
||||
Wheel {
|
||||
elapsed: 0,
|
||||
levels: levels.into_boxed_slice().try_into().unwrap(),
|
||||
levels: levels.try_into().unwrap(),
|
||||
pending: EntryList::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,13 +39,10 @@ pub(super) const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1;
|
||||
impl Wheel {
|
||||
/// Creates a new timing wheel.
|
||||
pub(crate) fn new() -> Wheel {
|
||||
let mut levels = Vec::with_capacity(NUM_LEVELS);
|
||||
for i in 0..NUM_LEVELS {
|
||||
levels.push(Level::new(i));
|
||||
}
|
||||
let levels = (0..NUM_LEVELS).map(Level::new).collect::<Box<_>>();
|
||||
Wheel {
|
||||
elapsed: 0,
|
||||
levels: levels.into_boxed_slice().try_into().unwrap(),
|
||||
levels: levels.try_into().unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -550,18 +550,16 @@ impl<T> Sender<T> {
|
||||
// Round to a power of two
|
||||
capacity = capacity.next_power_of_two();
|
||||
|
||||
let mut buffer = Vec::with_capacity(capacity);
|
||||
|
||||
for i in 0..capacity {
|
||||
buffer.push(Mutex::new(Slot {
|
||||
let buffer = (0..capacity).map(|i| {
|
||||
Mutex::new(Slot {
|
||||
rem: AtomicUsize::new(0),
|
||||
pos: (i as u64).wrapping_sub(capacity as u64),
|
||||
val: None,
|
||||
}));
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let shared = Arc::new(Shared {
|
||||
buffer: buffer.into_boxed_slice(),
|
||||
buffer: buffer.collect(),
|
||||
mask: capacity - 1,
|
||||
tail: Mutex::new(Tail {
|
||||
pos: 0,
|
||||
|
||||
@@ -38,12 +38,9 @@ impl<L, T> ShardedList<L, T> {
|
||||
assert!(sharded_size.is_power_of_two());
|
||||
|
||||
let shard_mask = sharded_size - 1;
|
||||
let mut lists = Vec::with_capacity(sharded_size);
|
||||
for _ in 0..sharded_size {
|
||||
lists.push(Mutex::new(LinkedList::<L, T>::new()))
|
||||
}
|
||||
let lists = std::iter::repeat_with(|| Mutex::new(LinkedList::new()));
|
||||
Self {
|
||||
lists: lists.into_boxed_slice(),
|
||||
lists: lists.take(sharded_size).collect(),
|
||||
added: MetricAtomicU64::new(0),
|
||||
count: MetricAtomicUsize::new(0),
|
||||
shard_mask,
|
||||
|
||||
Reference in New Issue
Block a user