time: simplify wheel constants (#8335)

This commit is contained in:
Tim Vilgot Mikael Fredenberg
2026-08-09 17:42:42 +02:00
committed by GitHub
parent b6ed00435d
commit 011f7f4b47
4 changed files with 22 additions and 24 deletions
+4 -4
View File
@@ -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)))]
+7 -8
View File
@@ -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)))]
+4 -4
View File
@@ -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)))]
+7 -8
View File
@@ -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)))]