From 011f7f4b470463cf7141f2da944316a599639a07 Mon Sep 17 00:00:00 2001 From: Tim Vilgot Mikael Fredenberg <26655508+vilgotf@users.noreply.github.com> Date: Sun, 9 Aug 2026 17:42:42 +0200 Subject: [PATCH] time: simplify wheel constants (#8335) --- tokio/src/runtime/time/wheel/level.rs | 8 ++++---- tokio/src/runtime/time/wheel/mod.rs | 15 +++++++-------- tokio/src/runtime/time_alt/wheel/level.rs | 8 ++++---- tokio/src/runtime/time_alt/wheel/mod.rs | 15 +++++++-------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/tokio/src/runtime/time/wheel/level.rs b/tokio/src/runtime/time/wheel/level.rs index 27b288911..4c0db573b 100644 --- a/tokio/src/runtime/time/wheel/level.rs +++ b/tokio/src/runtime/time/wheel/level.rs @@ -36,7 +36,7 @@ pub(crate) struct Expiration { /// Level multiplier. /// /// Being a power of 2 is very important. -const LEVEL_MULT: usize = 64; +const LEVEL_MULT: usize = 1 << super::BITS_PER_LEVEL; impl Level { pub(crate) fn new(level: usize) -> Level { @@ -161,16 +161,16 @@ fn occupied_bit(slot: usize) -> u64 { } fn slot_range(level: usize) -> u64 { - LEVEL_MULT.pow(level as u32) as u64 + 1 << (super::BITS_PER_LEVEL * level) } fn level_range(level: usize) -> u64 { - LEVEL_MULT as u64 * slot_range(level) + 1 << (super::BITS_PER_LEVEL * (level + 1)) } /// Converts a duration (milliseconds) and a level to a slot position. fn slot_for(duration: u64, level: usize) -> usize { - ((duration >> (level * 6)) % LEVEL_MULT as u64) as usize + ((duration >> (level * super::BITS_PER_LEVEL)) % LEVEL_MULT as u64) as usize } #[cfg(all(test, not(loom)))] diff --git a/tokio/src/runtime/time/wheel/mod.rs b/tokio/src/runtime/time/wheel/mod.rs index ad1173619..38640a65d 100644 --- a/tokio/src/runtime/time/wheel/mod.rs +++ b/tokio/src/runtime/time/wheel/mod.rs @@ -44,8 +44,10 @@ pub(crate) struct Wheel { /// precision of 1 millisecond. const NUM_LEVELS: usize = 6; +const BITS_PER_LEVEL: usize = 6; + /// The maximum duration of a `Sleep`. -pub(super) const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1; +const MAX_DURATION: u64 = 1 << (BITS_PER_LEVEL * NUM_LEVELS); impl Wheel { /// Creates a new timing wheel. @@ -272,21 +274,18 @@ impl Wheel { } fn level_for(elapsed: u64, when: u64) -> usize { - const SLOT_MASK: u64 = (1 << 6) - 1; + const SLOT_MASK: u64 = (1 << BITS_PER_LEVEL) - 1; // Mask in the trailing bits ignored by the level calculation in order to cap // the possible leading zeros - let mut masked = elapsed ^ when | SLOT_MASK; + let masked = elapsed ^ when | SLOT_MASK; if masked >= MAX_DURATION { // Fudge the timer into the top level - masked = MAX_DURATION - 1; + return NUM_LEVELS - 1; } - let leading_zeros = masked.leading_zeros() as usize; - let significant = 63 - leading_zeros; - - significant / NUM_LEVELS + masked.ilog2() as usize / BITS_PER_LEVEL } #[cfg(all(test, not(loom)))] diff --git a/tokio/src/runtime/time_alt/wheel/level.rs b/tokio/src/runtime/time_alt/wheel/level.rs index 6d2d57a07..507998f80 100644 --- a/tokio/src/runtime/time_alt/wheel/level.rs +++ b/tokio/src/runtime/time_alt/wheel/level.rs @@ -37,7 +37,7 @@ pub(crate) struct Expiration { /// Level multiplier. /// /// Being a power of 2 is very important. -const LEVEL_MULT: usize = 64; +const LEVEL_MULT: usize = 1 << super::BITS_PER_LEVEL; impl Level { pub(crate) fn new(level: usize) -> Level { @@ -164,16 +164,16 @@ fn occupied_bit(slot: usize) -> u64 { } fn slot_range(level: usize) -> u64 { - LEVEL_MULT.pow(level as u32) as u64 + 1 << (super::BITS_PER_LEVEL * level) } fn level_range(level: usize) -> u64 { - LEVEL_MULT as u64 * slot_range(level) + 1 << (super::BITS_PER_LEVEL * (level + 1)) } /// Converts a duration (milliseconds) and a level to a slot position. fn slot_for(duration: u64, level: usize) -> usize { - ((duration >> (level * 6)) % LEVEL_MULT as u64) as usize + ((duration >> (level * super::BITS_PER_LEVEL)) % LEVEL_MULT as u64) as usize } #[cfg(all(test, not(loom)))] diff --git a/tokio/src/runtime/time_alt/wheel/mod.rs b/tokio/src/runtime/time_alt/wheel/mod.rs index b8fcbc801..1bb67d816 100644 --- a/tokio/src/runtime/time_alt/wheel/mod.rs +++ b/tokio/src/runtime/time_alt/wheel/mod.rs @@ -35,8 +35,10 @@ pub(crate) struct Wheel { /// precision of 1 millisecond. const NUM_LEVELS: usize = 6; +const BITS_PER_LEVEL: usize = 6; + /// The maximum duration of a `Sleep`. -pub(super) const MAX_DURATION: u64 = (1 << (6 * NUM_LEVELS)) - 1; +const MAX_DURATION: u64 = 1 << (BITS_PER_LEVEL * NUM_LEVELS); impl Wheel { /// Creates a new timing wheel. @@ -216,21 +218,18 @@ impl Wheel { } fn level_for(elapsed: u64, when: u64) -> usize { - const SLOT_MASK: u64 = (1 << 6) - 1; + const SLOT_MASK: u64 = (1 << BITS_PER_LEVEL) - 1; // Mask in the trailing bits ignored by the level calculation in order to cap // the possible leading zeros - let mut masked = elapsed ^ when | SLOT_MASK; + let masked = elapsed ^ when | SLOT_MASK; if masked >= MAX_DURATION { // Fudge the timer into the top level - masked = MAX_DURATION - 1; + return NUM_LEVELS - 1; } - let leading_zeros = masked.leading_zeros() as usize; - let significant = 63 - leading_zeros; - - significant / NUM_LEVELS + masked.ilog2() as usize / BITS_PER_LEVEL } #[cfg(all(test, not(loom)))]