Skip to content

Commit

Permalink
Merge pull request #2518 from mejrs/acquire_gil_tests
Browse files Browse the repository at this point in the history
Use Python::with_gil in tests
  • Loading branch information
davidhewitt committed Jul 20, 2022
2 parents 2b6d59f + 1983671 commit e40b252
Show file tree
Hide file tree
Showing 41 changed files with 2,687 additions and 2,803 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -181,7 +181,7 @@ jobs:
run: |
set -x
cargo update -p indexmap --precise 1.6.2
cargo update -p hashbrown:0.12.2 --precise 0.9.1
cargo update -p hashbrown:0.12.3 --precise 0.9.1
PROJECTS=("." "examples/decorator" "examples/maturin-starter" "examples/setuptools-rust-starter" "examples/word-count")
for PROJ in ${PROJECTS[@]}; do
cargo update --manifest-path "$PROJ/Cargo.toml" -p parking_lot --precise 0.11.0
Expand Down
78 changes: 39 additions & 39 deletions benches/bench_dict.rs
Expand Up @@ -5,62 +5,62 @@ use pyo3::types::IntoPyDict;
use std::collections::{BTreeMap, HashMap};

fn iter_dict(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let mut sum = 0;
b.iter(|| {
for (k, _v) in dict.iter() {
let i: u64 = k.extract().unwrap();
sum += i;
}
});
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let mut sum = 0;
b.iter(|| {
for (k, _v) in dict.iter() {
let i: u64 = k.extract().unwrap();
sum += i;
}
});
})
}

fn dict_new(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
b.iter(|| (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py));
Python::with_gil(|py| {
const LEN: usize = 50_000;
b.iter(|| (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py));
});
}

fn dict_get_item(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
sum += dict.get_item(i).unwrap().extract::<usize>().unwrap();
}
Python::with_gil(|py| {
const LEN: usize = 50_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
sum += dict.get_item(i).unwrap().extract::<usize>().unwrap();
}
});
});
}

fn extract_hashmap(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| HashMap::<u64, u64>::extract(dict));
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| HashMap::<u64, u64>::extract(dict));
});
}

fn extract_btreemap(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| BTreeMap::<u64, u64>::extract(dict));
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| BTreeMap::<u64, u64>::extract(dict));
});
}

#[cfg(feature = "hashbrown")]
fn extract_hashbrown_map(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| hashbrown::HashMap::<u64, u64>::extract(dict));
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| hashbrown::HashMap::<u64, u64>::extract(dict));
});
}

fn criterion_benchmark(c: &mut Criterion) {
Expand Down
4 changes: 2 additions & 2 deletions benches/bench_gil.rs
Expand Up @@ -13,7 +13,7 @@ fn bench_clean_gilpool_new(b: &mut Bencher<'_>) {
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();
let _ = Python::with_gil(|_| {});
});
}

Expand All @@ -25,7 +25,7 @@ fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) {
let _ = obj.clone();
},
|_| {
let _ = Python::acquire_gil();
let _ = Python::with_gil(|_| {});
},
BatchSize::NumBatches(1),
);
Expand Down
66 changes: 33 additions & 33 deletions benches/bench_list.rs
Expand Up @@ -4,52 +4,52 @@ use pyo3::prelude::*;
use pyo3::types::PyList;

fn iter_list(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for x in list.iter() {
let i: u64 = x.extract().unwrap();
sum += i;
}
Python::with_gil(|py| {
const LEN: usize = 100_000;
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for x in list.iter() {
let i: u64 = x.extract().unwrap();
sum += i;
}
});
});
}

fn list_new(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
b.iter(|| PyList::new(py, 0..LEN));
Python::with_gil(|py| {
const LEN: usize = 50_000;
b.iter(|| PyList::new(py, 0..LEN));
});
}

fn list_get_item(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
sum += list.get_item(i).unwrap().extract::<usize>().unwrap();
}
Python::with_gil(|py| {
const LEN: usize = 50_000;
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
sum += list.get_item(i).unwrap().extract::<usize>().unwrap();
}
});
});
}

#[cfg(not(Py_LIMITED_API))]
fn list_get_item_unchecked(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 50_000;
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
unsafe {
sum += list.get_item_unchecked(i).extract::<usize>().unwrap();
Python::with_gil(|py| {
const LEN: usize = 50_000;
let list = PyList::new(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
unsafe {
sum += list.get_item_unchecked(i).extract::<usize>().unwrap();
}
}
}
});
});
}

Expand Down
14 changes: 7 additions & 7 deletions benches/bench_pyclass.rs
Expand Up @@ -27,13 +27,13 @@ impl MyClass {
}

pub fn first_time_init(b: &mut criterion::Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
b.iter(|| {
// This is using an undocumented internal PyO3 API to measure pyclass performance; please
// don't use this in your own code!
let ty = LazyStaticType::new();
ty.get_or_init::<MyClass>(py);
Python::with_gil(|py| {
b.iter(|| {
// This is using an undocumented internal PyO3 API to measure pyclass performance; please
// don't use this in your own code!
let ty = LazyStaticType::new();
ty.get_or_init::<MyClass>(py);
});
});
}

Expand Down
12 changes: 6 additions & 6 deletions benches/bench_pyobject.rs
Expand Up @@ -3,12 +3,12 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use pyo3::prelude::*;

fn drop_many_objects(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
b.iter(|| {
for _ in 0..1000 {
std::mem::drop(py.None());
}
Python::with_gil(|py| {
b.iter(|| {
for _ in 0..1000 {
std::mem::drop(py.None());
}
});
});
}

Expand Down
50 changes: 25 additions & 25 deletions benches/bench_set.rs
Expand Up @@ -5,42 +5,42 @@ use pyo3::types::PySet;
use std::collections::{BTreeSet, HashSet};

fn iter_set(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
let mut sum = 0;
b.iter(|| {
for x in set.iter() {
let i: u64 = x.extract().unwrap();
sum += i;
}
Python::with_gil(|py| {
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
let mut sum = 0;
b.iter(|| {
for x in set.iter() {
let i: u64 = x.extract().unwrap();
sum += i;
}
});
});
}

fn extract_hashset(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| HashSet::<u64>::extract(set));
Python::with_gil(|py| {
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| HashSet::<u64>::extract(set));
});
}

fn extract_btreeset(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| BTreeSet::<u64>::extract(set));
Python::with_gil(|py| {
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| BTreeSet::<u64>::extract(set));
});
}

#[cfg(feature = "hashbrown")]
fn extract_hashbrown_set(b: &mut Bencher<'_>) {
let gil = Python::acquire_gil();
let py = gil.python();
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| hashbrown::HashSet::<u64>::extract(set));
Python::with_gil(|py| {
const LEN: usize = 100_000;
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
b.iter(|| hashbrown::HashSet::<u64>::extract(set));
});
}

fn criterion_benchmark(c: &mut Criterion) {
Expand Down

0 comments on commit e40b252

Please sign in to comment.