Skip to content

Commit e40b252

Browse files
authored
Merge pull request #2518 from mejrs/acquire_gil_tests
Use Python::with_gil in tests
2 parents 2b6d59f + 1983671 commit e40b252

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2687
-2803
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ jobs:
181181
run: |
182182
set -x
183183
cargo update -p indexmap --precise 1.6.2
184-
cargo update -p hashbrown:0.12.2 --precise 0.9.1
184+
cargo update -p hashbrown:0.12.3 --precise 0.9.1
185185
PROJECTS=("." "examples/decorator" "examples/maturin-starter" "examples/setuptools-rust-starter" "examples/word-count")
186186
for PROJ in ${PROJECTS[@]}; do
187187
cargo update --manifest-path "$PROJ/Cargo.toml" -p parking_lot --precise 0.11.0

benches/bench_dict.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,62 @@ use pyo3::types::IntoPyDict;
55
use std::collections::{BTreeMap, HashMap};
66

77
fn iter_dict(b: &mut Bencher<'_>) {
8-
let gil = Python::acquire_gil();
9-
let py = gil.python();
10-
const LEN: usize = 100_000;
11-
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
12-
let mut sum = 0;
13-
b.iter(|| {
14-
for (k, _v) in dict.iter() {
15-
let i: u64 = k.extract().unwrap();
16-
sum += i;
17-
}
18-
});
8+
Python::with_gil(|py| {
9+
const LEN: usize = 100_000;
10+
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
11+
let mut sum = 0;
12+
b.iter(|| {
13+
for (k, _v) in dict.iter() {
14+
let i: u64 = k.extract().unwrap();
15+
sum += i;
16+
}
17+
});
18+
})
1919
}
2020

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

2828
fn dict_get_item(b: &mut Bencher<'_>) {
29-
let gil = Python::acquire_gil();
30-
let py = gil.python();
31-
const LEN: usize = 50_000;
32-
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
33-
let mut sum = 0;
34-
b.iter(|| {
35-
for i in 0..LEN {
36-
sum += dict.get_item(i).unwrap().extract::<usize>().unwrap();
37-
}
29+
Python::with_gil(|py| {
30+
const LEN: usize = 50_000;
31+
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
32+
let mut sum = 0;
33+
b.iter(|| {
34+
for i in 0..LEN {
35+
sum += dict.get_item(i).unwrap().extract::<usize>().unwrap();
36+
}
37+
});
3838
});
3939
}
4040

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

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

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

6666
fn criterion_benchmark(c: &mut Criterion) {

benches/bench_gil.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn bench_clean_gilpool_new(b: &mut Bencher<'_>) {
1313
fn bench_clean_acquire_gil(b: &mut Bencher<'_>) {
1414
// Acquiring first GIL will also create a "clean" GILPool, so this measures the Python overhead.
1515
b.iter(|| {
16-
let _ = Python::acquire_gil();
16+
let _ = Python::with_gil(|_| {});
1717
});
1818
}
1919

@@ -25,7 +25,7 @@ fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) {
2525
let _ = obj.clone();
2626
},
2727
|_| {
28-
let _ = Python::acquire_gil();
28+
let _ = Python::with_gil(|_| {});
2929
},
3030
BatchSize::NumBatches(1),
3131
);

benches/bench_list.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,52 @@ use pyo3::prelude::*;
44
use pyo3::types::PyList;
55

66
fn iter_list(b: &mut Bencher<'_>) {
7-
let gil = Python::acquire_gil();
8-
let py = gil.python();
9-
const LEN: usize = 100_000;
10-
let list = PyList::new(py, 0..LEN);
11-
let mut sum = 0;
12-
b.iter(|| {
13-
for x in list.iter() {
14-
let i: u64 = x.extract().unwrap();
15-
sum += i;
16-
}
7+
Python::with_gil(|py| {
8+
const LEN: usize = 100_000;
9+
let list = PyList::new(py, 0..LEN);
10+
let mut sum = 0;
11+
b.iter(|| {
12+
for x in list.iter() {
13+
let i: u64 = x.extract().unwrap();
14+
sum += i;
15+
}
16+
});
1717
});
1818
}
1919

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

2727
fn list_get_item(b: &mut Bencher<'_>) {
28-
let gil = Python::acquire_gil();
29-
let py = gil.python();
30-
const LEN: usize = 50_000;
31-
let list = PyList::new(py, 0..LEN);
32-
let mut sum = 0;
33-
b.iter(|| {
34-
for i in 0..LEN {
35-
sum += list.get_item(i).unwrap().extract::<usize>().unwrap();
36-
}
28+
Python::with_gil(|py| {
29+
const LEN: usize = 50_000;
30+
let list = PyList::new(py, 0..LEN);
31+
let mut sum = 0;
32+
b.iter(|| {
33+
for i in 0..LEN {
34+
sum += list.get_item(i).unwrap().extract::<usize>().unwrap();
35+
}
36+
});
3737
});
3838
}
3939

4040
#[cfg(not(Py_LIMITED_API))]
4141
fn list_get_item_unchecked(b: &mut Bencher<'_>) {
42-
let gil = Python::acquire_gil();
43-
let py = gil.python();
44-
const LEN: usize = 50_000;
45-
let list = PyList::new(py, 0..LEN);
46-
let mut sum = 0;
47-
b.iter(|| {
48-
for i in 0..LEN {
49-
unsafe {
50-
sum += list.get_item_unchecked(i).extract::<usize>().unwrap();
42+
Python::with_gil(|py| {
43+
const LEN: usize = 50_000;
44+
let list = PyList::new(py, 0..LEN);
45+
let mut sum = 0;
46+
b.iter(|| {
47+
for i in 0..LEN {
48+
unsafe {
49+
sum += list.get_item_unchecked(i).extract::<usize>().unwrap();
50+
}
5151
}
52-
}
52+
});
5353
});
5454
}
5555

benches/bench_pyclass.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ impl MyClass {
2727
}
2828

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

benches/bench_pyobject.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
33
use pyo3::prelude::*;
44

55
fn drop_many_objects(b: &mut Bencher<'_>) {
6-
let gil = Python::acquire_gil();
7-
let py = gil.python();
8-
b.iter(|| {
9-
for _ in 0..1000 {
10-
std::mem::drop(py.None());
11-
}
6+
Python::with_gil(|py| {
7+
b.iter(|| {
8+
for _ in 0..1000 {
9+
std::mem::drop(py.None());
10+
}
11+
});
1212
});
1313
}
1414

benches/bench_set.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,42 @@ use pyo3::types::PySet;
55
use std::collections::{BTreeSet, HashSet};
66

77
fn iter_set(b: &mut Bencher<'_>) {
8-
let gil = Python::acquire_gil();
9-
let py = gil.python();
10-
const LEN: usize = 100_000;
11-
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
12-
let mut sum = 0;
13-
b.iter(|| {
14-
for x in set.iter() {
15-
let i: u64 = x.extract().unwrap();
16-
sum += i;
17-
}
8+
Python::with_gil(|py| {
9+
const LEN: usize = 100_000;
10+
let set = PySet::new(py, &(0..LEN).collect::<Vec<_>>()).unwrap();
11+
let mut sum = 0;
12+
b.iter(|| {
13+
for x in set.iter() {
14+
let i: u64 = x.extract().unwrap();
15+
sum += i;
16+
}
17+
});
1818
});
1919
}
2020

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

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

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

4646
fn criterion_benchmark(c: &mut Criterion) {

0 commit comments

Comments
 (0)