Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benches: add bench_gil #1609

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde = {version = "1.0", optional = true}

[dev-dependencies]
assert_approx_eq = "1.1.0"
criterion = "0.3"
trybuild = "1.0.23"
rustversion = "1.0"
proptest = { version = "0.10.1", default-features = false, features = ["std"] }
Expand Down Expand Up @@ -69,6 +70,10 @@ auto-initialize = []
# Optimizes PyObject to Vec conversion and so on.
nightly = []

[[bench]]
name = "bench_gil"
harness = false

[workspace]
members = [
"pyo3-macros",
Expand All @@ -83,4 +88,4 @@ members = [
[package.metadata.docs.rs]
no-default-features = true
features = ["macros", "num-bigint", "num-complex", "hashbrown", "serde", "multiple-pymethods"]
rustdoc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]
41 changes: 41 additions & 0 deletions benches/bench_gil.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use criterion::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};

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

fn bench_clean_gilpool_new(b: &mut Bencher) {
Python::with_gil(|_py| {
b.iter(|| {
let _ = unsafe { GILPool::new() };
});
});
}

fn bench_clean_acquire_gil(b: &mut Bencher) {
// Acquiring first GIL will also create a "clean" GILPool, so this measures the Python overhead.
b.iter(|| {
let _ = Python::acquire_gil();
});
}

fn bench_dirty_acquire_gil(b: &mut Bencher) {
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);