2023-08-29 06:26:36 +00:00
|
|
|
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
|
2021-12-22 00:27:13 +00:00
|
|
|
|
2022-04-06 11:57:23 +00:00
|
|
|
use pyo3::{
|
|
|
|
prelude::*,
|
2023-07-27 07:55:52 +00:00
|
|
|
types::{PyFloat, PyList, PyString},
|
2022-04-06 11:57:23 +00:00
|
|
|
};
|
2021-12-22 00:27:13 +00:00
|
|
|
|
|
|
|
#[derive(FromPyObject)]
|
|
|
|
enum ManyTypes {
|
|
|
|
Int(i32),
|
|
|
|
Bytes(Vec<u8>),
|
2021-12-22 00:29:22 +00:00
|
|
|
String(String),
|
2021-12-22 00:27:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn enum_from_pyobject(b: &mut Bencher<'_>) {
|
2021-12-22 00:27:13 +00:00
|
|
|
Python::with_gil(|py| {
|
2023-11-21 11:41:43 +00:00
|
|
|
let any: &Bound<'_, PyAny> = &PyString::new_bound(py, "hello world");
|
|
|
|
|
|
|
|
b.iter(|| any.extract::<ManyTypes>().unwrap());
|
2021-12-22 00:27:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2022-11-14 06:15:33 +00:00
|
|
|
fn list_via_downcast(b: &mut Bencher<'_>) {
|
2022-04-06 11:57:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-01-23 08:39:19 +00:00
|
|
|
let any: &Bound<'_, PyAny> = &PyList::empty_bound(py);
|
2022-04-06 11:57:23 +00:00
|
|
|
|
2024-01-23 08:39:19 +00:00
|
|
|
b.iter(|| black_box(any).downcast::<PyList>().unwrap());
|
2022-04-06 11:57:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2022-04-06 11:57:23 +00:00
|
|
|
fn list_via_extract(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-01-23 08:39:19 +00:00
|
|
|
let any: &Bound<'_, PyAny> = &PyList::empty_bound(py);
|
2022-04-06 11:57:23 +00:00
|
|
|
|
2024-01-23 08:39:19 +00:00
|
|
|
b.iter(|| black_box(any).extract::<Bound<'_, PyList>>().unwrap());
|
2022-04-06 11:57:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2022-11-14 06:15:33 +00:00
|
|
|
fn not_a_list_via_downcast(b: &mut Bencher<'_>) {
|
2022-04-06 11:57:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2023-11-21 11:41:43 +00:00
|
|
|
let any: &Bound<'_, PyAny> = &PyString::new_bound(py, "foobar");
|
2022-04-06 11:57:23 +00:00
|
|
|
|
2024-01-23 08:39:19 +00:00
|
|
|
b.iter(|| black_box(any).downcast::<PyList>().unwrap_err());
|
2022-04-06 11:57:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn not_a_list_via_extract(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2023-11-21 11:41:43 +00:00
|
|
|
let any: &Bound<'_, PyAny> = &PyString::new_bound(py, "foobar");
|
2022-04-06 11:57:23 +00:00
|
|
|
|
2023-11-21 11:41:43 +00:00
|
|
|
b.iter(|| black_box(any).extract::<Bound<'_, PyList>>().unwrap_err());
|
2022-04-06 11:57:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromPyObject)]
|
|
|
|
enum ListOrNotList<'a> {
|
2023-11-21 11:41:43 +00:00
|
|
|
List(Bound<'a, PyList>),
|
|
|
|
NotList(Bound<'a, PyAny>),
|
2022-04-06 11:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn not_a_list_via_extract_enum(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2023-11-21 11:41:43 +00:00
|
|
|
let any: &Bound<'_, PyAny> = &PyString::new_bound(py, "foobar");
|
2022-04-06 11:57:23 +00:00
|
|
|
|
|
|
|
b.iter(|| match black_box(any).extract::<ListOrNotList<'_>>() {
|
|
|
|
Ok(ListOrNotList::List(_list)) => panic!(),
|
2023-11-21 11:41:43 +00:00
|
|
|
Ok(ListOrNotList::NotList(any)) => any,
|
2022-04-06 11:57:23 +00:00
|
|
|
Err(_) => panic!(),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-07-27 07:55:52 +00:00
|
|
|
fn f64_from_pyobject(b: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-01-29 10:25:40 +00:00
|
|
|
let obj = &PyFloat::new_bound(py, 1.234);
|
|
|
|
b.iter(|| black_box(obj).extract::<f64>().unwrap());
|
2023-07-27 07:55:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-22 00:27:13 +00:00
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
c.bench_function("enum_from_pyobject", enum_from_pyobject);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2022-11-14 06:15:33 +00:00
|
|
|
c.bench_function("list_via_downcast", list_via_downcast);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2022-04-06 11:57:23 +00:00
|
|
|
c.bench_function("list_via_extract", list_via_extract);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2022-11-14 06:15:33 +00:00
|
|
|
c.bench_function("not_a_list_via_downcast", not_a_list_via_downcast);
|
2022-04-06 11:57:23 +00:00
|
|
|
c.bench_function("not_a_list_via_extract", not_a_list_via_extract);
|
|
|
|
c.bench_function("not_a_list_via_extract_enum", not_a_list_via_extract_enum);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-07-27 07:55:52 +00:00
|
|
|
c.bench_function("f64_from_pyobject", f64_from_pyobject);
|
2021-12-22 00:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|