mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-19 00:00:09 +02:00
time: use bit manipulation instead of modulo (#4480)
time: resolve TODO ## Motivation Existing `TODO` comment in `src/time/driver/wheel/level.rs`. ## Solution `level_range()` always return a strictly positive power of 2. If `b` is a strictly positive power of 2, `a - (a % b)` is equal to `a & !(b - 1)`.
This commit is contained in:
@@ -143,8 +143,9 @@ impl Level {
|
||||
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);
|
||||
// Compute the start date of the current level by masking the low bits
|
||||
// of `now` (`level_range` is a power of 2).
|
||||
let level_start = now & !(level_range - 1);
|
||||
let mut deadline = level_start + slot as u64 * slot_range;
|
||||
|
||||
if deadline <= now {
|
||||
|
||||
Reference in New Issue
Block a user