Skip to content

Commit

Permalink
No % operation
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Mar 29, 2024
1 parent 255911f commit 6da810c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
57 changes: 28 additions & 29 deletions neqo-common/benches/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,40 @@ use neqo_common::timer::Timer;
use test_fixture::now;

fn benchmark_timer(c: &mut Criterion) {
c.bench_function("drain a small timer quickly", |b| {
b.iter_batched(make_small_timer, drain, criterion::BatchSize::SmallInput);
c.bench_function("drain a timer quickly", |b| {
b.iter_batched(
|| {
const TIMES: &[u64] = &[1, 2, 3, 5, 8, 13, 21, 34];
let now = now();
let mut timer = Timer::new(now, Duration::from_millis(777), 100);
for &t in TIMES {
timer.add(now + Duration::from_secs(t), ());
}
timer
},
|mut timer| {
while let Some(t) = timer.next_time() {
assert!(timer.take_next(t).is_some());
}
},
criterion::BatchSize::SmallInput,
);
});
c.bench_function("drain a large mostly empty timer quickly", |b| {
c.bench_function("drain an empty timer", |b| {
b.iter_batched(
make_large_mostly_empty_timer,
drain,
|| {
let now = now();
let timer = Timer::<()>::new(now, Duration::from_millis(4), 16384);
let lookup_time = now + Duration::from_millis(400);
(timer, lookup_time)
},
|(mut timer, lookup_time)| {
assert_eq!(None, timer.take_next(lookup_time));
},
criterion::BatchSize::SmallInput,
);
});
}

fn make_small_timer() -> Timer<()> {
const TIMES: &[u64] = &[1, 2, 3, 5, 8, 13, 21, 34];

let now = now();
let mut timer = Timer::new(now, Duration::from_millis(777), 100);
for &t in TIMES {
timer.add(now + Duration::from_secs(t), ());
}
timer
}

fn make_large_mostly_empty_timer() -> Timer<()> {
let now = now();
let mut timer = Timer::new(now, Duration::from_millis(4), 16384);
timer.add(now + Duration::from_millis(400), ());
timer
}

fn drain(mut timer: Timer<()>) {
while let Some(t) = timer.next_time() {
assert!(timer.take_next(t).is_some());
}
}

criterion_group!(benches, benchmark_timer);
criterion_main!(benches);
8 changes: 6 additions & 2 deletions neqo-common/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,16 @@ impl<T> Timer<T> {
///
/// Impossible, I think.
pub fn take_next(&mut self, until: Instant) -> Option<T> {
let last_bucket = (self.cursor + self.delta(until)) % self.items.len();
let range = if self.cursor < last_bucket {
let last_bucket = self.cursor + self.delta(until);

let range = if last_bucket <= self.items.len() {
// Simple case, no wrap around.
#[allow(clippy::range_plus_one)] // non-inclusive range to match with type below
(self.cursor..(last_bucket + 1)).chain(0..0) // additional empty range to match with
// type below
} else {
// Complex case, with wrap around.
let last_bucket = last_bucket - self.items.len();
(self.cursor..self.items.len()).chain(0..last_bucket)

Check warning on line 211 in neqo-common/src/timer.rs

View check run for this annotation

Codecov / codecov/patch

neqo-common/src/timer.rs#L210-L211

Added lines #L210 - L211 were not covered by tests
};

Expand Down

0 comments on commit 6da810c

Please sign in to comment.