diff --git a/pyo3-benches/benches/bench_gil.rs b/pyo3-benches/benches/bench_gil.rs index e25345e1fe9..90c02300f5f 100644 --- a/pyo3-benches/benches/bench_gil.rs +++ b/pyo3-benches/benches/bench_gil.rs @@ -1,4 +1,6 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Bencher, Criterion}; +use codspeed_criterion_compat::{ + black_box, criterion_group, criterion_main, BatchSize, Bencher, Criterion, +}; use pyo3::{prelude::*, GILPool}; @@ -27,10 +29,24 @@ fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) { ); } +fn bench_allow_threads(b: &mut Bencher<'_>) { + Python::with_gil(|py| { + b.iter(|| py.allow_threads(|| black_box(42))); + }); +} + +fn bench_unsafe_allow_threads(b: &mut Bencher<'_>) { + Python::with_gil(|py| { + b.iter(|| unsafe { py.unsafe_allow_threads(|| black_box(42)) }); + }); +} + fn criterion_benchmark(c: &mut Criterion) { c.bench_function("clean_gilpool_new", bench_clean_gilpool_new); c.bench_function("clean_acquire_gil", bench_clean_acquire_gil); c.bench_function("dirty_acquire_gil", bench_dirty_acquire_gil); + c.bench_function("allow_threads", bench_allow_threads); + c.bench_function("unsafe_allow_threads", bench_unsafe_allow_threads); } criterion_group!(benches, criterion_benchmark);