Skip to content

Commit

Permalink
fixup benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 1, 2024
1 parent 8c1b7a0 commit 94d21ee
Show file tree
Hide file tree
Showing 19 changed files with 296 additions and 98 deletions.
4 changes: 2 additions & 2 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ impl<'a, 'py> pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a
type Holder = ::std::option::Option<pyo3::PyRef<'py, MyClass>>;

#[inline]
fn extract(obj: &'py pyo3::PyAny, holder: &'a mut Self::Holder) -> pyo3::PyResult<Self> {
fn extract(obj: pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> pyo3::PyResult<Self> {
pyo3::impl_::extract_argument::extract_pyclass_ref(obj, holder)
}
}
Expand All @@ -1233,7 +1233,7 @@ impl<'a, 'py> pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a
type Holder = ::std::option::Option<pyo3::PyRefMut<'py, MyClass>>;

#[inline]
fn extract(obj: &'py pyo3::PyAny, holder: &'a mut Self::Holder) -> pyo3::PyResult<Self> {
fn extract(obj: pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> pyo3::PyResult<Self> {
pyo3::impl_::extract_argument::extract_pyclass_ref_mut(obj, holder)
}
}
Expand Down
7 changes: 5 additions & 2 deletions pyo3-benches/benches/bench_any.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::prelude::*;
use pyo3::{
types::{
PyBool, PyByteArray, PyBytes, PyDict, PyFloat, PyFrozenSet, PyInt, PyList, PyMapping,
PySequence, PySet, PyString, PyTuple,
},
PyAny, PyResult, Python,
Bound, PyAny, PyResult, Python,
};

#[derive(PartialEq, Eq, Debug)]
Expand All @@ -27,7 +28,7 @@ enum ObjectType {
Unknown,
}

fn find_object_type(obj: &PyAny) -> ObjectType {
fn find_object_type(obj: &Bound<'_, PyAny>) -> ObjectType {
if obj.is_none() {
ObjectType::None
} else if obj.is_instance_of::<PyBool>() {
Expand Down Expand Up @@ -64,6 +65,7 @@ fn find_object_type(obj: &PyAny) -> ObjectType {
fn bench_identify_object_type(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let obj = py.eval("object()", None, None).unwrap();
let obj = &obj.as_borrowed();

b.iter(|| find_object_type(obj));

Expand All @@ -74,6 +76,7 @@ fn bench_identify_object_type(b: &mut Bencher<'_>) {
fn bench_collect_generic_iterator(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let collection = py.eval("list(range(1 << 20))", None, None).unwrap();
let collection = &collection.as_borrowed();

b.iter(|| {
collection
Expand Down
7 changes: 7 additions & 0 deletions pyo3-benches/benches/bench_bigint.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};

use pyo3::prelude::*;
use pyo3::{types::PyDict, PyAny, Python};

use num_bigint::BigInt;

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

bench.iter(|| match black_box(d).extract::<BigInt>() {
Ok(v) => panic!("should err {}", v),
Expand All @@ -18,6 +20,7 @@ fn extract_bigint_extract_fail(bench: &mut Bencher<'_>) {
fn extract_bigint_small(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("-42", None, None).unwrap();
let int = &int.as_borrowed();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
Expand All @@ -29,6 +32,7 @@ fn extract_bigint_small(bench: &mut Bencher<'_>) {
fn extract_bigint_big_negative(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("-10**300", None, None).unwrap();
let int = &int.as_borrowed();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
Expand All @@ -40,6 +44,7 @@ fn extract_bigint_big_negative(bench: &mut Bencher<'_>) {
fn extract_bigint_big_positive(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("10**300", None, None).unwrap();
let int = &int.as_borrowed();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
Expand All @@ -51,6 +56,7 @@ fn extract_bigint_big_positive(bench: &mut Bencher<'_>) {
fn extract_bigint_huge_negative(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("-10**3000", None, None).unwrap();
let int = &int.as_borrowed();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
Expand All @@ -62,6 +68,7 @@ fn extract_bigint_huge_negative(bench: &mut Bencher<'_>) {
fn extract_bigint_huge_positive(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("10**3000", None, None).unwrap();
let int = &int.as_borrowed();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions pyo3-benches/benches/bench_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn bench_call_0(b: &mut Bencher<'_>) {
let module = test_module!(py, "def foo(): pass");

let foo_module = module.getattr("foo").unwrap();
let foo_module = &foo_module.as_borrowed();

b.iter(|| {
for _ in 0..1000 {
Expand All @@ -34,6 +35,7 @@ class Foo:
);

let foo_module = module.getattr("Foo").unwrap().call0().unwrap();
let foo_module = &foo_module.as_borrowed();

b.iter(|| {
for _ in 0..1000 {
Expand Down
28 changes: 20 additions & 8 deletions pyo3-benches/benches/bench_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,31 @@ impl OrderedRichcmp {

fn bench_ordered_dunder_methods(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let obj1 = Py::new(py, OrderedDunderMethods(0)).unwrap().into_ref(py);
let obj2 = Py::new(py, OrderedDunderMethods(1)).unwrap().into_ref(py);

b.iter(|| obj2.gt(obj1).unwrap());
let obj1 = Py::new(py, OrderedDunderMethods(0))
.unwrap()
.attach_into(py)
.into_any();
let obj2 = Py::new(py, OrderedDunderMethods(1))
.unwrap()
.attach_into(py)
.into_any();

b.iter(|| obj2.gt(&obj1).unwrap());
});
}

fn bench_ordered_richcmp(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let obj1 = Py::new(py, OrderedRichcmp(0)).unwrap().into_ref(py);
let obj2 = Py::new(py, OrderedRichcmp(1)).unwrap().into_ref(py);

b.iter(|| obj2.gt(obj1).unwrap());
let obj1 = Py::new(py, OrderedRichcmp(0))
.unwrap()
.attach_into(py)
.into_any();
let obj2 = Py::new(py, OrderedRichcmp(1))
.unwrap()
.attach_into(py)
.into_any();

b.iter(|| obj2.gt(&obj1).unwrap());
});
}

Expand Down
1 change: 1 addition & 0 deletions pyo3-benches/benches/bench_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ py_dec = decimal.Decimal("0.0")
)
.unwrap();
let py_dec = locals.get_item("py_dec").unwrap().unwrap();
let py_dec = &py_dec.as_borrowed();

b.iter(|| {
let _: Decimal = black_box(py_dec).extract().unwrap();
Expand Down
13 changes: 9 additions & 4 deletions pyo3-benches/benches/bench_dict.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::types::IntoPyDict;
use pyo3::types::{IntoPyDict, PyDict};
use pyo3::{prelude::*, types::PyMapping};
use std::collections::{BTreeMap, HashMap};
use std::hint::black_box;
Expand All @@ -9,6 +9,7 @@ fn iter_dict(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let dict = &dict.as_borrowed();
let mut sum = 0;
b.iter(|| {
for (k, _v) in dict {
Expand All @@ -30,6 +31,7 @@ fn dict_get_item(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
let dict = &dict.as_borrowed();
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
Expand All @@ -48,15 +50,17 @@ fn extract_hashmap(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| HashMap::<u64, u64>::extract(dict));
let dict = &dict.as_borrowed();
b.iter(|| dict.extract::<HashMap<u64, u64>>());
});
}

fn extract_btreemap(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| BTreeMap::<u64, u64>::extract(dict));
let dict = &dict.as_borrowed();
b.iter(|| dict.extract::<BTreeMap<u64, u64>>());
});
}

Expand All @@ -65,7 +69,8 @@ fn extract_hashbrown_map(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 100_000;
let dict = (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py);
b.iter(|| hashbrown::HashMap::<u64, u64>::extract(dict));
let dict = &dict.as_borrowed();
b.iter(|| dict.extract::<hashbrown::HashMap<u64, u64>>());
});
}

Expand Down
13 changes: 9 additions & 4 deletions pyo3-benches/benches/bench_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn extract_str_downcast_success(bench: &mut Bencher<'_>) {
fn extract_str_downcast_fail(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let d = PyDict::new(py) as &PyAny;
let d = &d.as_borrowed();

bench.iter(|| match black_box(d).downcast::<PyString>() {
Ok(v) => panic!("should err {}", v),
Expand All @@ -51,7 +52,7 @@ fn extract_str_downcast_fail(bench: &mut Bencher<'_>) {
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 = int_obj.attach(py);

bench.iter(|| {
let v = black_box(int).extract::<i64>().unwrap();
Expand All @@ -63,6 +64,7 @@ fn extract_int_extract_success(bench: &mut Bencher<'_>) {
fn extract_int_extract_fail(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let d = PyDict::new(py) as &PyAny;
let d = &d.as_borrowed();

bench.iter(|| match black_box(d).extract::<i64>() {
Ok(v) => panic!("should err {}", v),
Expand All @@ -74,7 +76,7 @@ fn extract_int_extract_fail(bench: &mut Bencher<'_>) {
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 = int_obj.attach(py);

bench.iter(|| {
let py_int = black_box(int).downcast::<PyInt>().unwrap();
Expand All @@ -87,6 +89,7 @@ fn extract_int_downcast_success(bench: &mut Bencher<'_>) {
fn extract_int_downcast_fail(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let d = PyDict::new(py) as &PyAny;
let d = &d.as_borrowed();

bench.iter(|| match black_box(d).downcast::<PyInt>() {
Ok(v) => panic!("should err {}", v),
Expand All @@ -98,7 +101,7 @@ fn extract_int_downcast_fail(bench: &mut Bencher<'_>) {
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 = float_obj.attach(py);

bench.iter(|| {
let v = black_box(float).extract::<f64>().unwrap();
Expand All @@ -110,6 +113,7 @@ fn extract_float_extract_success(bench: &mut Bencher<'_>) {
fn extract_float_extract_fail(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let d = PyDict::new(py) as &PyAny;
let d = &d.as_borrowed();

bench.iter(|| match black_box(d).extract::<f64>() {
Ok(v) => panic!("should err {}", v),
Expand All @@ -121,7 +125,7 @@ fn extract_float_extract_fail(bench: &mut Bencher<'_>) {
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 = float_obj.attach(py);

bench.iter(|| {
let py_int = black_box(float).downcast::<PyFloat>().unwrap();
Expand All @@ -134,6 +138,7 @@ fn extract_float_downcast_success(bench: &mut Bencher<'_>) {
fn extract_float_downcast_fail(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let d = PyDict::new(py) as &PyAny;
let d = &d.as_borrowed();

bench.iter(|| match black_box(d).downcast::<PyFloat>() {
Ok(v) => panic!("should err {}", v),
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl SelfType {
});
error_mode.handle_error(quote_spanned! { *span =>
_pyo3::impl_::extract_argument::#method::<#cls>(
#py.from_borrowed_ptr::<_pyo3::PyAny>(#slf),
_pyo3::impl_::extract_argument::PyArg::from_gil_ref(#py.from_borrowed_ptr::<_pyo3::PyAny>(#slf)),
&mut #holder,
)
})
Expand Down
12 changes: 7 additions & 5 deletions pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn impl_arg_params(
.collect::<Result<_>>()?;
return Ok((
quote! {
let _args = py.from_borrowed_ptr::<_pyo3::types::PyTuple>(_args);
let _kwargs: ::std::option::Option<&_pyo3::types::PyDict> = py.from_borrowed_ptr_or_opt(_kwargs);
let _args = _pyo3::impl_::extract_argument::PyArg::from_ptr_unchecked(py, _args);
let _kwargs = _pyo3::impl_::extract_argument::PyArg::from_ptr_or_opt(py, _kwargs);
},
arg_convert,
));
Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn impl_arg_params(
keyword_only_parameters: &[#(#keyword_only_parameters),*],
};
let mut #args_array = [::std::option::Option::None; #num_params];
let (_args, _kwargs) = #extract_expression;
let (_args, _kwargs) = &#extract_expression;
},
param_conversion,
))
Expand Down Expand Up @@ -180,7 +180,8 @@ fn impl_arg_param(
let holder = push_holder();
return Ok(quote_arg_span! {
_pyo3::impl_::extract_argument::extract_argument(
_args,
#[allow(clippy::useless_conversion)]
::std::convert::From::from(_args),
&mut #holder,
#name_str
)?
Expand All @@ -193,7 +194,8 @@ fn impl_arg_param(
let holder = push_holder();
return Ok(quote_arg_span! {
_pyo3::impl_::extract_argument::extract_optional_argument(
_kwargs.map(::std::convert::AsRef::as_ref),
#[allow(clippy::useless_conversion, clippy::redundant_closure)]
_kwargs.as_ref().map(|kwargs| ::std::convert::From::from(kwargs)),
&mut #holder,
#name_str,
|| ::std::option::Option::None
Expand Down
6 changes: 3 additions & 3 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,7 @@ impl<'a> PyClassImplsBuilder<'a> {
type Holder = ::std::option::Option<_pyo3::PyRef<'py, #cls>>;

#[inline]
fn extract(obj: &'py _pyo3::PyAny, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
fn extract(obj: _pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
_pyo3::impl_::extract_argument::extract_pyclass_ref(obj, holder)
}
}
Expand All @@ -1381,7 +1381,7 @@ impl<'a> PyClassImplsBuilder<'a> {
type Holder = ::std::option::Option<_pyo3::PyRef<'py, #cls>>;

#[inline]
fn extract(obj: &'py _pyo3::PyAny, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
fn extract(obj: _pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
_pyo3::impl_::extract_argument::extract_pyclass_ref(obj, holder)
}
}
Expand All @@ -1391,7 +1391,7 @@ impl<'a> PyClassImplsBuilder<'a> {
type Holder = ::std::option::Option<_pyo3::PyRefMut<'py, #cls>>;

#[inline]
fn extract(obj: &'py _pyo3::PyAny, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
fn extract(obj: _pyo3::impl_::extract_argument::PyArg<'py>, holder: &'a mut Self::Holder) -> _pyo3::PyResult<Self> {
_pyo3::impl_::extract_argument::extract_pyclass_ref_mut(obj, holder)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ fn extract_object(
});
extract_error_mode.handle_error(quote! {
_pyo3::impl_::extract_argument::extract_argument(
#source,
_pyo3::impl_::extract_argument::PyArg::from_gil_ref(#source),
&mut #holder,
#name
)
Expand Down
3 changes: 2 additions & 1 deletion pyo3-macros-backend/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) fn ok_wrap(obj: TokenStream) -> TokenStream {

pub(crate) fn map_result_into_ptr(result: TokenStream) -> TokenStream {
quote! {
_pyo3::impl_::wrap::map_result_into_ptr(py, #result)
let result = _pyo3::impl_::wrap::map_result_into_ptr(py, #result);
result
}
}
Loading

0 comments on commit 94d21ee

Please sign in to comment.