okee-wheel-timer
Deterministic hashed wheel timer with keyed deduplication.
Features
HashedWheelTimer<T>: core single-level hashed wheel timer.KeyedHashedWheelTimer<T, K, F>: keyed dedup wrapper over the core timer.- Stable pop ordering by
(tick, delta_tick, event_id).
Installation
[]
= "0.1.0"
How It Works
use HashedWheelTimer;
// 8 buckets in one wheel cycle.
let mut wheel = new;
new(8)means one wheel cycle has 8 buckets.- Time is tracked by
tick. step()advances one tick and moves to the next bucket.- Events are stored with
(tick, delta_tick).tickis the absolute time when the event is eligible.delta_tickis the wave index inside the same tick.
delta_tick prevents processing newly scheduled "same tick" events in the same wave.
Core Timer Example
use HashedWheelTimer;
let mut wheel = new;
let created = wheel.schedule;
let first_wave = wheel.pop_events;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Keyed Timer Example
HashedWheelTimer always creates a new event ID. KeyedHashedWheelTimer can replace an existing event by your dedup key.
use KeyedHashedWheelTimer;
let mut wheel = new;
let first = wheel.schedule;
let second = wheel.schedule;
assert_eq!;
assert!;
assert!;
Dedup Key Design
- In keyed mode, you are responsible for dedup semantics.
- The dedup key is produced from
&T(Fn(&T) -> K). - If timing should affect uniqueness, include timing fields in your payload
Tand key them explicitly.
Processing Contract
- Call
pop_events()whilehas_events_in_current_tick()istrue. - Call
step()to move to the next tick. - Typical loop:
while wheel.has_events_in_current_tick
wheel.step;
Benchmarks
Benchmarks are implemented with Criterion in benches/wheel.rs.
# run all timer benchmarks
# save current results as baseline
# compare current run with a saved baseline
HTML reports are generated in target/criterion/report/index.html.