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

ci: tidy up benchmarks a little #3986

Merged
merged 1 commit into from
Mar 23, 2024
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
34 changes: 10 additions & 24 deletions pyo3-benches/benches/bench_bigint.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use std::hint::black_box;

use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
use num_bigint::BigInt;

use pyo3::prelude::*;
use pyo3::types::PyDict;

use num_bigint::BigInt;

fn extract_bigint_extract_fail(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let d = PyDict::new_bound(py).into_any();

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

bench.iter(|| {
let v = black_box(&int).extract::<BigInt>().unwrap();
black_box(v);
});
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
});
}

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

bench.iter(|| {
let v = black_box(&int).extract::<BigInt>().unwrap();
black_box(v);
});
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
});
}

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

bench.iter(|| {
let v = black_box(&int).extract::<BigInt>().unwrap();
black_box(v);
});
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
});
}

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

bench.iter(|| {
let v = black_box(&int).extract::<BigInt>().unwrap();
black_box(v);
});
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
});
}

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

bench.iter(|| {
let v = black_box(&int).extract::<BigInt>().unwrap();
black_box(v);
});
bench.iter_with_large_drop(|| black_box(&int).extract::<BigInt>().unwrap());
});
}

Expand Down
10 changes: 5 additions & 5 deletions pyo3-benches/benches/bench_decimal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use std::hint::black_box;

use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
use rust_decimal::Decimal;

use pyo3::prelude::*;
use pyo3::types::PyDict;
use rust_decimal::Decimal;

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

b.iter(|| {
let _: Decimal = black_box(&py_dec).extract().unwrap();
});
b.iter(|| black_box(&py_dec).extract::<Decimal>().unwrap());
})
}

Expand Down
10 changes: 4 additions & 6 deletions pyo3-benches/benches/bench_dict.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::{BTreeMap, HashMap};
use std::hint::black_box;

use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::types::IntoPyDict;
use pyo3::{prelude::*, types::PyMapping};
use std::collections::{BTreeMap, HashMap};
use std::hint::black_box;

fn iter_dict(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
Expand Down Expand Up @@ -69,7 +70,6 @@ fn extract_hashbrown_map(b: &mut Bencher<'_>) {
});
}

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

#[cfg(feature = "hashbrown")]
c.bench_function("extract_hashbrown_map", extract_hashbrown_map);

#[cfg(not(codspeed))]
c.bench_function("mapping_from_dict", mapping_from_dict);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
63 changes: 22 additions & 41 deletions pyo3-benches/benches/bench_extract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use std::hint::black_box;

use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::{
prelude::*,
Expand All @@ -7,9 +9,9 @@ use pyo3::{

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

bench.iter(|| black_box(s).extract::<&str>().unwrap());
bench.iter(|| black_box(&s).extract::<&str>().unwrap());
});
}

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

bench.iter(|| match black_box(&d).extract::<&str>() {
Ok(v) => panic!("should err {}", v),
Err(e) => black_box(e),
Err(e) => e,
});
});
}

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

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

bench.iter(|| match black_box(&d).downcast::<PyString>() {
Ok(v) => panic!("should err {}", v),
Err(e) => black_box(e),
Err(e) => e,
});
});
}

fn extract_int_extract_success(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int_obj: PyObject = 123.into_py(py);
let int = int_obj.as_ref(py);
let int = 123.to_object(py).into_bound(py);

bench.iter(|| {
let v = black_box(int).extract::<i64>().unwrap();
black_box(v);
});
bench.iter(|| black_box(&int).extract::<i64>().unwrap());
});
}

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

bench.iter(|| match black_box(&d).extract::<i64>() {
Ok(v) => panic!("should err {}", v),
Err(e) => black_box(e),
Err(e) => e,
});
});
}

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

bench.iter(|| {
let py_int = black_box(int).downcast::<PyInt>().unwrap();
let v = py_int.extract::<i64>().unwrap();
black_box(v);
let py_int = black_box(&int).downcast::<PyInt>().unwrap();
py_int.extract::<i64>().unwrap()
});
});
}

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

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

bench.iter(|| {
let v = black_box(float).extract::<f64>().unwrap();
black_box(v);
});
bench.iter(|| black_box(&float).extract::<f64>().unwrap());
});
}

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

bench.iter(|| match black_box(&d).extract::<f64>() {
Ok(v) => panic!("should err {}", v),
Err(e) => black_box(e),
Err(e) => e,
});
});
}

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

bench.iter(|| {
let py_int = black_box(float).downcast::<PyFloat>().unwrap();
let v = py_int.extract::<f64>().unwrap();
black_box(v);
let py_float = black_box(&float).downcast::<PyFloat>().unwrap();
py_float.value()
});
});
}
Expand All @@ -140,7 +126,7 @@ fn extract_float_downcast_fail(bench: &mut Bencher<'_>) {

bench.iter(|| match black_box(&d).downcast::<PyFloat>() {
Ok(v) => panic!("should err {}", v),
Err(e) => black_box(e),
Err(e) => e,
});
});
}
Expand All @@ -151,20 +137,15 @@ fn criterion_benchmark(c: &mut Criterion) {
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
c.bench_function("extract_str_downcast_success", extract_str_downcast_success);
c.bench_function("extract_str_downcast_fail", extract_str_downcast_fail);
#[cfg(not(codspeed))]
c.bench_function("extract_int_extract_success", extract_int_extract_success);
c.bench_function("extract_int_extract_fail", extract_int_extract_fail);
#[cfg(not(codspeed))]
c.bench_function("extract_int_downcast_success", extract_int_downcast_success);
#[cfg(not(codspeed))]
c.bench_function("extract_int_downcast_fail", extract_int_downcast_fail);
#[cfg(not(codspeed))]
c.bench_function(
"extract_float_extract_success",
extract_float_extract_success,
);
c.bench_function("extract_float_extract_fail", extract_float_extract_fail);
#[cfg(not(codspeed))]
c.bench_function(
"extract_float_downcast_success",
extract_float_downcast_success,
Expand Down
Loading
Loading