2021-05-16 07:11:37 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};
|
|
|
|
|
|
|
|
use pyo3::{prelude::*, GILPool};
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn bench_clean_gilpool_new(b: &mut Bencher<'_>) {
|
2021-05-16 07:11:37 +00:00
|
|
|
Python::with_gil(|_py| {
|
|
|
|
b.iter(|| {
|
|
|
|
let _ = unsafe { GILPool::new() };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn bench_clean_acquire_gil(b: &mut Bencher<'_>) {
|
2021-05-16 07:11:37 +00:00
|
|
|
// Acquiring first GIL will also create a "clean" GILPool, so this measures the Python overhead.
|
|
|
|
b.iter(|| {
|
|
|
|
let _ = Python::acquire_gil();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) {
|
2021-05-16 07:11:37 +00:00
|
|
|
let obj = Python::with_gil(|py| py.None());
|
|
|
|
b.iter_batched(
|
|
|
|
|| {
|
|
|
|
// Clone and drop an object so that the GILPool has work to do.
|
|
|
|
let _ = obj.clone();
|
|
|
|
},
|
|
|
|
|_| {
|
|
|
|
let _ = Python::acquire_gil();
|
|
|
|
},
|
|
|
|
BatchSize::NumBatches(1),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|