Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize for timer timeout extension #184

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ jobs:
command: update
args: --package log --precise 0.4.16

- name: Downgrade regex
uses: actions-rs/cargo@v1
if: ${{ matrix.rust == '1.63.0' }}
with:
command: update
args: --package regex --precise 1.9.6

- name: Downgrade half
uses: actions-rs/cargo@v1
if: ${{ matrix.rust == '1.63.0' || matrix.rust == '1.69.0' }}
with:
command: update
args: --package half --precise 2.2.1

- name: Run tests
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -174,6 +188,20 @@ jobs:
toolchain: ${{ matrix.rust }}
override: true

- name: Downgrade regex
uses: actions-rs/cargo@v1
if: ${{ matrix.rust == '1.63.0' }}
with:
command: update
args: --package regex --precise 1.9.6

- name: Downgrade half
uses: actions-rs/cargo@v1
if: ${{ matrix.rust == '1.63.0' }}
with:
command: update
args: --package half --precise 2.2.1

- name: Run tests
uses: actions-rs/cargo@v1
with:
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ nix = { version = "0.29", default-features = false, features = ["signal"], optio
[dev-dependencies]
futures = "0.3.5"
rustix = { version = "0.38", default-features = false, features = ["net"] }
criterion = { version = "0.4" }

[features]
block_on = ["pin-utils"]
Expand All @@ -48,3 +49,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[[test]]
name = "signals"
harness = false

[[bench]]
name = "timer"
harness = false
100 changes: 100 additions & 0 deletions benches/timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use std::time::Duration;

use calloop::timer::TimeoutAction;
use criterion::{criterion_group, criterion_main, Criterion};

fn single(c: &mut Criterion) {
let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
let loop_handle = event_loop.handle();

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
let mut timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

c.bench_function("extend_single", |b| {
b.iter(|| {
loop_handle.remove(timeout_token);

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
});
});
}

fn mixed(c: &mut Criterion) {
let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
let loop_handle = event_loop.handle();

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10 - 1));
loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
let mut timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(90 * 10));
loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

c.bench_function("extend_mixed", |b| {
b.iter(|| {
loop_handle.remove(timeout_token);

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
});
});
}

fn mixed_multiple(c: &mut Criterion) {
let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
let loop_handle = event_loop.handle();

for _ in 0..1000 {
let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10 - 1));
loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();
}

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
let mut timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

for _ in 0..1000 {
let timer = calloop::timer::Timer::from_duration(Duration::from_secs(90 * 10));
loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();
}

c.bench_function("extend_mixed_many", |b| {
b.iter(|| {
loop_handle.remove(timeout_token);

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
});
});
}

criterion_group!(benches, single, mixed, mixed_multiple);
criterion_main!(benches);
14 changes: 14 additions & 0 deletions src/sources/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,19 @@ impl TimerWheel {
}

pub(crate) fn cancel(&mut self, counter: u32) {
if self
.heap
.peek()
.map(|data| data.counter == counter)
.unwrap_or(false)
{
self.heap.pop();
return;
};

self.heap
.iter()
.rev()
.find(|data| data.counter == counter)
.map(|data| data.token.take());
}
Expand Down Expand Up @@ -265,13 +276,15 @@ impl TimerWheel {
// trait implementations for TimeoutData

impl std::cmp::Ord for TimeoutData {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// earlier values have priority
self.deadline.cmp(&other.deadline).reverse()
}
}

impl std::cmp::PartialOrd for TimeoutData {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
Expand All @@ -281,6 +294,7 @@ impl std::cmp::PartialOrd for TimeoutData {
// and the type is private, so ignore its coverage
impl std::cmp::PartialEq for TimeoutData {
#[cfg_attr(feature = "nightly_coverage", coverage(off))]
#[inline]
fn eq(&self, other: &Self) -> bool {
self.deadline == other.deadline
}
Expand Down
Loading