Skip to content

Commit 28b983c

Browse files
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)`.
1 parent 0826f76 commit 28b983c

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

tokio/src/time/driver/wheel/level.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ impl Level {
143143
let level_range = level_range(self.level);
144144
let slot_range = slot_range(self.level);
145145

146-
// TODO: This can probably be simplified w/ power of 2 math
147-
let level_start = now - (now % level_range);
146+
// Compute the start date of the current level by masking the low bits
147+
// of `now` (`level_range` is a power of 2).
148+
let level_start = now & !(level_range - 1);
148149
let mut deadline = level_start + slot as u64 * slot_range;
149150

150151
if deadline <= now {

0 commit comments

Comments
 (0)