Skip to content

Commit 84a868a

Browse files
committed
Add benchmark comparing the two variants of allow_threads.
1 parent 7780365 commit 84a868a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

pyo3-benches/benches/bench_gil.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};
1+
use codspeed_criterion_compat::{
2+
black_box, criterion_group, criterion_main, BatchSize, Bencher, Criterion,
3+
};
24

35
use pyo3::{prelude::*, GILPool};
46

@@ -27,10 +29,24 @@ fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) {
2729
);
2830
}
2931

32+
fn bench_allow_threads(b: &mut Bencher<'_>) {
33+
Python::with_gil(|py| {
34+
b.iter(|| py.allow_threads(|| black_box(42)));
35+
});
36+
}
37+
38+
fn bench_unsafe_allow_threads(b: &mut Bencher<'_>) {
39+
Python::with_gil(|py| {
40+
b.iter(|| unsafe { py.unsafe_allow_threads(|| black_box(42)) });
41+
});
42+
}
43+
3044
fn criterion_benchmark(c: &mut Criterion) {
3145
c.bench_function("clean_gilpool_new", bench_clean_gilpool_new);
3246
c.bench_function("clean_acquire_gil", bench_clean_acquire_gil);
3347
c.bench_function("dirty_acquire_gil", bench_dirty_acquire_gil);
48+
c.bench_function("allow_threads", bench_allow_threads);
49+
c.bench_function("unsafe_allow_threads", bench_unsafe_allow_threads);
3450
}
3551

3652
criterion_group!(benches, criterion_benchmark);

0 commit comments

Comments
 (0)