2024-03-23 20:17:33 +00:00
|
|
|
use std::hint::black_box;
|
|
|
|
|
|
|
|
use codspeed_criterion_compat::{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| {
|
2024-03-23 20:17:33 +00:00
|
|
|
let s = PyString::new_bound(py, "Hello, World!").into_any();
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-03-23 20:17:33 +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),
|
2024-03-23 20:17:33 +00:00
|
|
|
Err(e) => e,
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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| {
|
2024-03-23 20:17:33 +00:00
|
|
|
let s = PyString::new_bound(py, "Hello, World!").into_any();
|
2023-05-25 17:57:33 +00:00
|
|
|
|
|
|
|
bench.iter(|| {
|
2024-03-23 20:17:33 +00:00
|
|
|
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),
|
2024-03-23 20:17:33 +00:00
|
|
|
Err(e) => e,
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_int_extract_success(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-03-23 20:17:33 +00:00
|
|
|
let int = 123.to_object(py).into_bound(py);
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-03-23 20:17:33 +00:00
|
|
|
bench.iter(|| black_box(&int).extract::<i64>().unwrap());
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2024-03-23 20:17:33 +00:00
|
|
|
Err(e) => e,
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_int_downcast_success(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-03-23 20:17:33 +00:00
|
|
|
let int = 123.to_object(py).into_bound(py);
|
2023-05-25 17:57:33 +00:00
|
|
|
|
|
|
|
bench.iter(|| {
|
2024-03-23 20:17:33 +00:00
|
|
|
let py_int = black_box(&int).downcast::<PyInt>().unwrap();
|
|
|
|
py_int.extract::<i64>().unwrap()
|
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),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_float_extract_success(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-03-23 20:17:33 +00:00
|
|
|
let float = 23.42.to_object(py).into_bound(py);
|
2023-05-25 17:57:33 +00:00
|
|
|
|
2024-03-23 20:17:33 +00:00
|
|
|
bench.iter(|| black_box(&float).extract::<f64>().unwrap());
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2024-03-23 20:17:33 +00:00
|
|
|
Err(e) => e,
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_float_downcast_success(bench: &mut Bencher<'_>) {
|
|
|
|
Python::with_gil(|py| {
|
2024-03-23 20:17:33 +00:00
|
|
|
let float = 23.42.to_object(py).into_bound(py);
|
2023-05-25 17:57:33 +00:00
|
|
|
|
|
|
|
bench.iter(|| {
|
2024-03-23 20:17:33 +00:00
|
|
|
let py_float = black_box(&float).downcast::<PyFloat>().unwrap();
|
|
|
|
py_float.value()
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2024-03-23 20:17:33 +00:00
|
|
|
Err(e) => e,
|
2023-05-25 17:57:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
c.bench_function("extract_int_extract_success", extract_int_extract_success);
|
|
|
|
c.bench_function("extract_int_extract_fail", extract_int_extract_fail);
|
|
|
|
c.bench_function("extract_int_downcast_success", extract_int_downcast_success);
|
|
|
|
c.bench_function("extract_int_downcast_fail", extract_int_downcast_fail);
|
|
|
|
c.bench_function(
|
|
|
|
"extract_float_extract_success",
|
|
|
|
extract_float_extract_success,
|
|
|
|
);
|
|
|
|
c.bench_function("extract_float_extract_fail", extract_float_extract_fail);
|
|
|
|
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);
|