Skip to content

Commit

Permalink
Avoid allocation in the benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Mar 31, 2023
1 parent 2ac3be0 commit bc07d43
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
use std::pin::Pin;

use criterion::{criterion_group, criterion_main, Criterion};
use event_listener::Event;
use event_listener::{Event, EventListener};

const COUNT: usize = 8000;

fn bench_events(c: &mut Criterion) {
c.bench_function("notify_and_wait", |b| {
let ev = Event::new();
b.iter(|| {
let mut handles = Vec::with_capacity(COUNT);
let mut handles = Vec::with_capacity(COUNT);
for _ in 0..COUNT {
handles.push(EventListener::new(&ev));
}

for _ in 0..COUNT {
handles.push(ev.listen());
b.iter(|| {
for handle in &mut handles {
// SAFETY: The handle is not moved out.
let listener = unsafe { Pin::new_unchecked(handle) };
listener.listen();
}

ev.notify(COUNT);

for mut handle in handles {
handle.as_mut().wait();
for handle in &mut handles {
// SAFETY: The handle is not moved out.
let listener = unsafe { Pin::new_unchecked(handle) };
listener.wait();
}
});
});
Expand Down

0 comments on commit bc07d43

Please sign in to comment.