From 6da810cf6723d1fd689ec6b57c18d5f2d8eddfc1 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 29 Mar 2024 17:24:13 +0100 Subject: [PATCH] No % operation --- neqo-common/benches/timer.rs | 57 ++++++++++++++++++------------------ neqo-common/src/timer.rs | 8 +++-- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/neqo-common/benches/timer.rs b/neqo-common/benches/timer.rs index d6d75ef20b..280f1b8281 100644 --- a/neqo-common/benches/timer.rs +++ b/neqo-common/benches/timer.rs @@ -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); diff --git a/neqo-common/src/timer.rs b/neqo-common/src/timer.rs index 7f70da1924..41afd2cdab 100644 --- a/neqo-common/src/timer.rs +++ b/neqo-common/src/timer.rs @@ -198,12 +198,16 @@ impl Timer { /// /// Impossible, I think. pub fn take_next(&mut self, until: Instant) -> Option { - 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) };