Skip to content

Commit 2d790dc

Browse files
committed
ci: tidy up benchmarks a little
1 parent 009cd32 commit 2d790dc

File tree

8 files changed

+81
-119
lines changed

8 files changed

+81
-119
lines changed

pyo3-benches/benches/bench_bigint.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
1+
use std::hint::black_box;
2+
3+
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
4+
use num_bigint::BigInt;
25

36
use pyo3::prelude::*;
47
use pyo3::types::PyDict;
58

6-
use num_bigint::BigInt;
7-
89
fn extract_bigint_extract_fail(bench: &mut Bencher<'_>) {
910
Python::with_gil(|py| {
1011
let d = PyDict::new_bound(py).into_any();
1112

1213
bench.iter(|| match black_box(&d).extract::<BigInt>() {
1314
Ok(v) => panic!("should err {}", v),
14-
Err(e) => black_box(e),
15+
Err(e) => e,
1516
});
1617
});
1718
}
@@ -20,54 +21,39 @@ fn extract_bigint_small(bench: &mut Bencher<'_>) {
2021
Python::with_gil(|py| {
2122
let int = py.eval_bound("-42", None, None).unwrap();
2223

23-
bench.iter(|| {
24-
let v = black_box(&int).extract::<BigInt>().unwrap();
25-
black_box(v);
26-
});
24+
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
2725
});
2826
}
2927

3028
fn extract_bigint_big_negative(bench: &mut Bencher<'_>) {
3129
Python::with_gil(|py| {
3230
let int = py.eval_bound("-10**300", None, None).unwrap();
3331

34-
bench.iter(|| {
35-
let v = black_box(&int).extract::<BigInt>().unwrap();
36-
black_box(v);
37-
});
32+
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
3833
});
3934
}
4035

4136
fn extract_bigint_big_positive(bench: &mut Bencher<'_>) {
4237
Python::with_gil(|py| {
4338
let int = py.eval_bound("10**300", None, None).unwrap();
4439

45-
bench.iter(|| {
46-
let v = black_box(&int).extract::<BigInt>().unwrap();
47-
black_box(v);
48-
});
40+
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
4941
});
5042
}
5143

5244
fn extract_bigint_huge_negative(bench: &mut Bencher<'_>) {
5345
Python::with_gil(|py| {
5446
let int = py.eval_bound("-10**3000", None, None).unwrap();
5547

56-
bench.iter(|| {
57-
let v = black_box(&int).extract::<BigInt>().unwrap();
58-
black_box(v);
59-
});
48+
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
6049
});
6150
}
6251

6352
fn extract_bigint_huge_positive(bench: &mut Bencher<'_>) {
6453
Python::with_gil(|py| {
6554
let int = py.eval_bound("10**3000", None, None).unwrap();
6655

67-
bench.iter(|| {
68-
let v = black_box(&int).extract::<BigInt>().unwrap();
69-
black_box(v);
70-
});
56+
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
7157
});
7258
}
7359

pyo3-benches/benches/bench_decimal.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
1+
use std::hint::black_box;
2+
3+
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
4+
use rust_decimal::Decimal;
25

36
use pyo3::prelude::*;
47
use pyo3::types::PyDict;
5-
use rust_decimal::Decimal;
68

79
fn decimal_via_extract(b: &mut Bencher<'_>) {
810
Python::with_gil(|py| {
@@ -18,9 +20,7 @@ py_dec = decimal.Decimal("0.0")
1820
.unwrap();
1921
let py_dec = locals.get_item("py_dec").unwrap().unwrap();
2022

21-
b.iter(|| {
22-
let _: Decimal = black_box(&py_dec).extract().unwrap();
23-
});
23+
b.iter(|| black_box(&py_dec).extract::<Decimal>().unwrap());
2424
})
2525
}
2626

pyo3-benches/benches/bench_dict.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use std::collections::{BTreeMap, HashMap};
2+
use std::hint::black_box;
3+
14
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
25

36
use pyo3::types::IntoPyDict;
47
use pyo3::{prelude::*, types::PyMapping};
5-
use std::collections::{BTreeMap, HashMap};
6-
use std::hint::black_box;
78

89
fn iter_dict(b: &mut Bencher<'_>) {
910
Python::with_gil(|py| {
@@ -69,7 +70,6 @@ fn extract_hashbrown_map(b: &mut Bencher<'_>) {
6970
});
7071
}
7172

72-
#[cfg(not(codspeed))]
7373
fn mapping_from_dict(b: &mut Bencher<'_>) {
7474
Python::with_gil(|py| {
7575
const LEN: usize = 100_000;
@@ -84,12 +84,10 @@ fn criterion_benchmark(c: &mut Criterion) {
8484
c.bench_function("dict_get_item", dict_get_item);
8585
c.bench_function("extract_hashmap", extract_hashmap);
8686
c.bench_function("extract_btreemap", extract_btreemap);
87+
c.bench_function("mapping_from_dict", mapping_from_dict);
8788

8889
#[cfg(feature = "hashbrown")]
8990
c.bench_function("extract_hashbrown_map", extract_hashbrown_map);
90-
91-
#[cfg(not(codspeed))]
92-
c.bench_function("mapping_from_dict", mapping_from_dict);
9391
}
9492

9593
criterion_group!(benches, criterion_benchmark);

pyo3-benches/benches/bench_extract.rs

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
1+
use std::hint::black_box;
2+
3+
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
24

35
use pyo3::{
46
prelude::*,
@@ -7,9 +9,9 @@ use pyo3::{
79

810
fn extract_str_extract_success(bench: &mut Bencher<'_>) {
911
Python::with_gil(|py| {
10-
let s = &PyString::new_bound(py, "Hello, World!");
12+
let s = PyString::new_bound(py, "Hello, World!").into_any();
1113

12-
bench.iter(|| black_box(s).extract::<&str>().unwrap());
14+
bench.iter(|| black_box(&s).extract::<&str>().unwrap());
1315
});
1416
}
1517

@@ -19,18 +21,18 @@ fn extract_str_extract_fail(bench: &mut Bencher<'_>) {
1921

2022
bench.iter(|| match black_box(&d).extract::<&str>() {
2123
Ok(v) => panic!("should err {}", v),
22-
Err(e) => black_box(e),
24+
Err(e) => e,
2325
});
2426
});
2527
}
2628

2729
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
2830
fn extract_str_downcast_success(bench: &mut Bencher<'_>) {
2931
Python::with_gil(|py| {
30-
let s = &PyString::new_bound(py, "Hello, World!");
32+
let s = PyString::new_bound(py, "Hello, World!").into_any();
3133

3234
bench.iter(|| {
33-
let py_str = black_box(s).downcast::<PyString>().unwrap();
35+
let py_str = black_box(&s).downcast::<PyString>().unwrap();
3436
py_str.to_str().unwrap()
3537
});
3638
});
@@ -42,20 +44,16 @@ fn extract_str_downcast_fail(bench: &mut Bencher<'_>) {
4244

4345
bench.iter(|| match black_box(&d).downcast::<PyString>() {
4446
Ok(v) => panic!("should err {}", v),
45-
Err(e) => black_box(e),
47+
Err(e) => e,
4648
});
4749
});
4850
}
4951

5052
fn extract_int_extract_success(bench: &mut Bencher<'_>) {
5153
Python::with_gil(|py| {
52-
let int_obj: PyObject = 123.into_py(py);
53-
let int = int_obj.as_ref(py);
54+
let int = 123.to_object(py).into_bound(py);
5455

55-
bench.iter(|| {
56-
let v = black_box(int).extract::<i64>().unwrap();
57-
black_box(v);
58-
});
56+
bench.iter(|| black_box(&int).extract::<i64>().unwrap());
5957
});
6058
}
6159

@@ -65,26 +63,22 @@ fn extract_int_extract_fail(bench: &mut Bencher<'_>) {
6563

6664
bench.iter(|| match black_box(&d).extract::<i64>() {
6765
Ok(v) => panic!("should err {}", v),
68-
Err(e) => black_box(e),
66+
Err(e) => e,
6967
});
7068
});
7169
}
7270

73-
#[cfg(not(codspeed))]
7471
fn extract_int_downcast_success(bench: &mut Bencher<'_>) {
7572
Python::with_gil(|py| {
76-
let int_obj: PyObject = 123.into_py(py);
77-
let int = int_obj.as_ref(py);
73+
let int = 123.to_object(py).into_bound(py);
7874

7975
bench.iter(|| {
80-
let py_int = black_box(int).downcast::<PyInt>().unwrap();
81-
let v = py_int.extract::<i64>().unwrap();
82-
black_box(v);
76+
let py_int = black_box(&int).downcast::<PyInt>().unwrap();
77+
py_int.extract::<i64>().unwrap()
8378
});
8479
});
8580
}
8681

87-
#[cfg(not(codspeed))]
8882
fn extract_int_downcast_fail(bench: &mut Bencher<'_>) {
8983
Python::with_gil(|py| {
9084
let d = PyDict::new_bound(py).into_any();
@@ -96,16 +90,11 @@ fn extract_int_downcast_fail(bench: &mut Bencher<'_>) {
9690
});
9791
}
9892

99-
#[cfg(not(codspeed))]
10093
fn extract_float_extract_success(bench: &mut Bencher<'_>) {
10194
Python::with_gil(|py| {
102-
let float_obj: PyObject = 23.42.into_py(py);
103-
let float = float_obj.as_ref(py);
95+
let float = 23.42.to_object(py).into_bound(py);
10496

105-
bench.iter(|| {
106-
let v = black_box(float).extract::<f64>().unwrap();
107-
black_box(v);
108-
});
97+
bench.iter(|| black_box(&float).extract::<f64>().unwrap());
10998
});
11099
}
111100

@@ -115,21 +104,18 @@ fn extract_float_extract_fail(bench: &mut Bencher<'_>) {
115104

116105
bench.iter(|| match black_box(&d).extract::<f64>() {
117106
Ok(v) => panic!("should err {}", v),
118-
Err(e) => black_box(e),
107+
Err(e) => e,
119108
});
120109
});
121110
}
122111

123-
#[cfg(not(codspeed))]
124112
fn extract_float_downcast_success(bench: &mut Bencher<'_>) {
125113
Python::with_gil(|py| {
126-
let float_obj: PyObject = 23.42.into_py(py);
127-
let float = float_obj.as_ref(py);
114+
let float = 23.42.to_object(py).into_bound(py);
128115

129116
bench.iter(|| {
130-
let py_int = black_box(float).downcast::<PyFloat>().unwrap();
131-
let v = py_int.extract::<f64>().unwrap();
132-
black_box(v);
117+
let py_float = black_box(&float).downcast::<PyFloat>().unwrap();
118+
py_float.value()
133119
});
134120
});
135121
}
@@ -140,7 +126,7 @@ fn extract_float_downcast_fail(bench: &mut Bencher<'_>) {
140126

141127
bench.iter(|| match black_box(&d).downcast::<PyFloat>() {
142128
Ok(v) => panic!("should err {}", v),
143-
Err(e) => black_box(e),
129+
Err(e) => e,
144130
});
145131
});
146132
}
@@ -151,20 +137,15 @@ fn criterion_benchmark(c: &mut Criterion) {
151137
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
152138
c.bench_function("extract_str_downcast_success", extract_str_downcast_success);
153139
c.bench_function("extract_str_downcast_fail", extract_str_downcast_fail);
154-
#[cfg(not(codspeed))]
155140
c.bench_function("extract_int_extract_success", extract_int_extract_success);
156141
c.bench_function("extract_int_extract_fail", extract_int_extract_fail);
157-
#[cfg(not(codspeed))]
158142
c.bench_function("extract_int_downcast_success", extract_int_downcast_success);
159-
#[cfg(not(codspeed))]
160143
c.bench_function("extract_int_downcast_fail", extract_int_downcast_fail);
161-
#[cfg(not(codspeed))]
162144
c.bench_function(
163145
"extract_float_extract_success",
164146
extract_float_extract_success,
165147
);
166148
c.bench_function("extract_float_extract_fail", extract_float_extract_fail);
167-
#[cfg(not(codspeed))]
168149
c.bench_function(
169150
"extract_float_downcast_success",
170151
extract_float_downcast_success,

0 commit comments

Comments
 (0)