From 26dee92b535d0a204531b6a04ee761f06c402d7e Mon Sep 17 00:00:00 2001 From: Tim Vilgot Mikael Fredenberg <26655508+vilgotf@users.noreply.github.com> Date: Thu, 30 Apr 2026 11:36:55 +0200 Subject: [PATCH] chore: use functional slice building (#8097) Noticed some redundant procedural code. --- tokio/src/runtime/scheduler/multi_thread/queue.rs | 8 ++------ tokio/src/runtime/time/wheel/mod.rs | 7 ++----- tokio/src/runtime/time_alt/wheel/mod.rs | 7 ++----- tokio/src/sync/broadcast.rs | 12 +++++------- tokio/src/util/sharded_list.rs | 7 ++----- 5 files changed, 13 insertions(+), 28 deletions(-) diff --git a/tokio/src/runtime/scheduler/multi_thread/queue.rs b/tokio/src/runtime/scheduler/multi_thread/queue.rs index 7836183a4..c1327e149 100644 --- a/tokio/src/runtime/scheduler/multi_thread/queue.rs +++ b/tokio/src/runtime/scheduler/multi_thread/queue.rs @@ -90,17 +90,13 @@ fn make_fixed_size(buffer: Box<[T]>) -> Box<[T; LOCAL_QUEUE_CAPACITY]> { /// Create a new local run-queue pub(crate) fn local() -> (Steal, Local) { - 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 { diff --git a/tokio/src/runtime/time/wheel/mod.rs b/tokio/src/runtime/time/wheel/mod.rs index a58954c52..53ea95958 100644 --- a/tokio/src/runtime/time/wheel/mod.rs +++ b/tokio/src/runtime/time/wheel/mod.rs @@ -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::>(); Wheel { elapsed: 0, - levels: levels.into_boxed_slice().try_into().unwrap(), + levels: levels.try_into().unwrap(), pending: EntryList::new(), } } diff --git a/tokio/src/runtime/time_alt/wheel/mod.rs b/tokio/src/runtime/time_alt/wheel/mod.rs index a17ed5b85..541eeba61 100644 --- a/tokio/src/runtime/time_alt/wheel/mod.rs +++ b/tokio/src/runtime/time_alt/wheel/mod.rs @@ -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::>(); Wheel { elapsed: 0, - levels: levels.into_boxed_slice().try_into().unwrap(), + levels: levels.try_into().unwrap(), } } diff --git a/tokio/src/sync/broadcast.rs b/tokio/src/sync/broadcast.rs index 3ab9a2ca4..49e879568 100644 --- a/tokio/src/sync/broadcast.rs +++ b/tokio/src/sync/broadcast.rs @@ -550,18 +550,16 @@ impl Sender { // 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, diff --git a/tokio/src/util/sharded_list.rs b/tokio/src/util/sharded_list.rs index 5b7d6b97a..4fccab693 100644 --- a/tokio/src/util/sharded_list.rs +++ b/tokio/src/util/sharded_list.rs @@ -38,12 +38,9 @@ impl ShardedList { 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::::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,