Skip to content

Commit

Permalink
time: use bit manipulation instead of modulo (#4480)
Browse files Browse the repository at this point in the history
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)`.
  • Loading branch information
samueltardieu committed Feb 15, 2022
1 parent 0826f76 commit 28b983c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tokio/src/time/driver/wheel/level.rs
Expand Up @@ -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 {
Expand Down

0 comments on commit 28b983c

Please sign in to comment.