Skip to content

Commit bc07d43

Browse files
committed
Avoid allocation in the benchmarks
1 parent 2ac3be0 commit bc07d43

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

benches/bench.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1+
use std::pin::Pin;
2+
13
use criterion::{criterion_group, criterion_main, Criterion};
2-
use event_listener::Event;
4+
use event_listener::{Event, EventListener};
35

46
const COUNT: usize = 8000;
57

68
fn bench_events(c: &mut Criterion) {
79
c.bench_function("notify_and_wait", |b| {
810
let ev = Event::new();
9-
b.iter(|| {
10-
let mut handles = Vec::with_capacity(COUNT);
11+
let mut handles = Vec::with_capacity(COUNT);
12+
for _ in 0..COUNT {
13+
handles.push(EventListener::new(&ev));
14+
}
1115

12-
for _ in 0..COUNT {
13-
handles.push(ev.listen());
16+
b.iter(|| {
17+
for handle in &mut handles {
18+
// SAFETY: The handle is not moved out.
19+
let listener = unsafe { Pin::new_unchecked(handle) };
20+
listener.listen();
1421
}
1522

1623
ev.notify(COUNT);
1724

18-
for mut handle in handles {
19-
handle.as_mut().wait();
25+
for handle in &mut handles {
26+
// SAFETY: The handle is not moved out.
27+
let listener = unsafe { Pin::new_unchecked(handle) };
28+
listener.wait();
2029
}
2130
});
2231
});

0 commit comments

Comments
 (0)