/// Wheel for a single level in the timer. This wheel contains 64 slots. pub(crate) struct Level<T> {
level: usize,
/// Bit field tracking which slots currently contain entries. /// /// Using a bit field to track slots that contain entries allows avoiding a /// scan to find entries. This field is updated when entries are added or /// removed from a slot. /// /// The least-significant bit represents slot zero.
occupied: u64,
/// Slots
slot: [T; LEVEL_MULT],
}
/// Indicates when a slot must be processed next. #[derive(Debug)] pub(crate) struct Expiration { /// The level containing the slot. pub(crate) level: usize,
/// The slot index. pub(crate) slot: usize,
/// The instant at which the slot needs to be processed. pub(crate) deadline: u64,
}
/// Level multiplier. /// /// Being a power of 2 is very important. const LEVEL_MULT: usize = 64;
impl<T: Stack> Level<T> { pub(crate) fn new(level: usize) -> Level<T> { // Rust's derived implementations for arrays require that the value // contained by the array be `Copy`. So, here we have to manually // initialize every single slot.
macro_rules! s {
() => {
T::default()
};
}
/// Finds the slot that needs to be processed next and returns the slot and /// `Instant` at which this slot must be processed. pub(crate) fn next_expiration(&self, now: u64) -> Option<Expiration> { // Use the `occupied` bit field to get the index of the next slot that // needs to be processed. let slot = matchself.next_occupied_slot(now) {
Some(slot) => slot,
None => return None,
};
// From the slot index, calculate the `Instant` at which it needs to be // processed. This value *must* be in the future with respect to `now`.
let level_range = level_range(self.level); let slot_range = slot_range(self.level);
// TODO: This can probably be simplified w/ power of 2 math let level_start = now - (now % level_range); let deadline = level_start + slot as u64 * slot_range;
// Get the slot for now using Maths let now_slot = (now / slot_range(self.level)) as usize; let occupied = self.occupied.rotate_right(now_slot as u32); let zeros = occupied.trailing_zeros() as usize; let slot = (zeros + now_slot) % 64;
/// Convert 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
}
#[cfg(all(test, not(loom)))] mod test { usesuper::*;
#[test] fn test_slot_for() { for pos in0..64 {
assert_eq!(pos as usize, slot_for(pos, 0));
}
for level in1..5 { for pos in level..64 { let a = pos * 64_usize.pow(level as u32);
assert_eq!(pos as usize, slot_for(a as u64, level));
}
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-22)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.