2023-08-29 06:26:36 +00:00
|
|
|
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
|
2023-05-25 17:57:33 +00:00
|
|
|
|
|
|
|
use pyo3::{
|
2023-11-21 11:41:43 +00:00
|
|
|
prelude::*,
|
2023-05-25 17:57:33 +00:00
|
|
|
types::{PyDict, PyFloat, PyInt, PyString},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn extract_str_extract_success(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2023-11-21 11:41:43 +00:00
|
|
|
let s = &PyString::new_bound(py, "Hello, World!");
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2023-11-21 11:41:43 +00:00
|
|
|
bench.iter(|| black_box(s).extract::<&str>().unwrap());
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_str_extract_fail(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-02-11 23:55:56 +00:00
|
|
|
let d = PyDict::new_bound(py).into_any();
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-02-11 23:55:56 +00:00
|
|
|
bench.iter(|| match black_box(&d).extract::<&str>() {
|
2023-05-25 17:57:33 +00:00
|
|
|
Ok(v) => panic!("should err {}", v),
|
|
|
|
Err(e) => black_box(e),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:41:43 +00:00
|
|
|
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
|
2023-05-25 17:57:33 +00:00
|
|
|
fn extract_str_downcast_success(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2023-11-21 11:41:43 +00:00
|
|
|
let s = &PyString::new_bound(py, "Hello, World!");
|
2023-05-25 17:57:33 +00:00
|
|
|
|
|
|
|
bench.iter(|| {
|
|
|
|
let py_str = black_box(s).downcast::<PyString>().unwrap();
|
2023-11-21 11:41:43 +00:00
|
|
|
py_str.to_str().unwrap()
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_str_downcast_fail(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-02-11 23:55:56 +00:00
|
|
|
let d = PyDict::new_bound(py).into_any();
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-02-11 23:55:56 +00:00
|
|
|
bench.iter(|| match black_box(&d).downcast::<PyString>() {
|
2023-05-25 17:57:33 +00:00
|
|
|
Ok(v) => panic!("should err {}", v),
|
|
|
|
Err(e) => black_box(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);
|
|
|
|
|
|
|
|
bench.iter(|| {
|
|
|
|
let v = black_box(int).extract::<i64>().unwrap();
|
|
|
|
black_box(v);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_int_extract_fail(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-02-11 23:55:56 +00:00
|
|
|
let d = PyDict::new_bound(py).into_any();
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-02-11 23:55:56 +00:00
|
|
|
bench.iter(|| match black_box(&d).extract::<i64>() {
|
2023-05-25 17:57:33 +00:00
|
|
|
Ok(v) => panic!("should err {}", v),
|
|
|
|
Err(e) => black_box(e),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
bench.iter(|| {
|
|
|
|
let py_int = black_box(int).downcast::<PyInt>().unwrap();
|
|
|
|
let v = py_int.extract::<i64>().unwrap();
|
|
|
|
black_box(v);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
fn extract_int_downcast_fail(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-02-11 23:55:56 +00:00
|
|
|
let d = PyDict::new_bound(py).into_any();
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-02-11 23:55:56 +00:00
|
|
|
bench.iter(|| match black_box(&d).downcast::<PyInt>() {
|
2023-05-25 17:57:33 +00:00
|
|
|
Ok(v) => panic!("should err {}", v),
|
|
|
|
Err(e) => black_box(e),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
bench.iter(|| {
|
|
|
|
let v = black_box(float).extract::<f64>().unwrap();
|
|
|
|
black_box(v);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_float_extract_fail(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-02-11 23:55:56 +00:00
|
|
|
let d = PyDict::new_bound(py).into_any();
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-02-11 23:55:56 +00:00
|
|
|
bench.iter(|| match black_box(&d).extract::<f64>() {
|
2023-05-25 17:57:33 +00:00
|
|
|
Ok(v) => panic!("should err {}", v),
|
|
|
|
Err(e) => black_box(e),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
bench.iter(|| {
|
|
|
|
let py_int = black_box(float).downcast::<PyFloat>().unwrap();
|
|
|
|
let v = py_int.extract::<f64>().unwrap();
|
|
|
|
black_box(v);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_float_downcast_fail(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-02-11 23:55:56 +00:00
|
|
|
let d = PyDict::new_bound(py).into_any();
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-02-11 23:55:56 +00:00
|
|
|
bench.iter(|| match black_box(&d).downcast::<PyFloat>() {
|
2023-05-25 17:57:33 +00:00
|
|
|
Ok(v) => panic!("should err {}", v),
|
|
|
|
Err(e) => black_box(e),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
c.bench_function("extract_str_extract_success", extract_str_extract_success);
|
|
|
|
c.bench_function("extract_str_extract_fail", extract_str_extract_fail);
|
2023-11-21 11:41:43 +00:00
|
|
|
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
|
2023-05-25 17:57:33 +00:00
|
|
|
c.bench_function("extract_str_downcast_success", extract_str_downcast_success);
|
|
|
|
c.bench_function("extract_str_downcast_fail", extract_str_downcast_fail);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
c.bench_function("extract_int_extract_success", extract_int_extract_success);
|
|
|
|
c.bench_function("extract_int_extract_fail", extract_int_extract_fail);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
c.bench_function("extract_int_downcast_success", extract_int_downcast_success);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
c.bench_function("extract_int_downcast_fail", extract_int_downcast_fail);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
c.bench_function(
|
|
|
|
"extract_float_extract_success",
|
|
|
|
extract_float_extract_success,
|
|
|
|
);
|
|
|
|
c.bench_function("extract_float_extract_fail", extract_float_extract_fail);
|
2024-02-19 22:40:08 +00:00
|
|
|
#[cfg(not(codspeed))]
|
2023-05-25 17:57:33 +00:00
|
|
|
c.bench_function(
|
|
|
|
"extract_float_downcast_success",
|
|
|
|
extract_float_downcast_success,
|
|
|
|
);
|
|
|
|
c.bench_function("extract_float_downcast_fail", extract_float_downcast_fail);
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|