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