Skip to content

Commit

Permalink
Add benchmark comparing the two variants of allow_threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Dec 16, 2023
1 parent 7780365 commit 84a868a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pyo3-benches/benches/bench_gil.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 84a868a

Please sign in to comment.