Skip to content

Commit

Permalink
update pytests benchmarks for Py2
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 1, 2024
1 parent 94d21ee commit 8ffb364
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 79 deletions.
8 changes: 4 additions & 4 deletions pyo3-benches/benches/bench_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ fn bench_ordered_dunder_methods(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let obj1 = Py::new(py, OrderedDunderMethods(0))
.unwrap()
.attach_into(py)
.into_bound(py)
.into_any();
let obj2 = Py::new(py, OrderedDunderMethods(1))
.unwrap()
.attach_into(py)
.into_bound(py)
.into_any();

b.iter(|| obj2.gt(&obj1).unwrap());
Expand All @@ -62,11 +62,11 @@ fn bench_ordered_richcmp(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let obj1 = Py::new(py, OrderedRichcmp(0))
.unwrap()
.attach_into(py)
.into_bound(py)
.into_any();
let obj2 = Py::new(py, OrderedRichcmp(1))
.unwrap()
.attach_into(py)
.into_bound(py)
.into_any();

b.iter(|| obj2.gt(&obj1).unwrap());
Expand Down
4 changes: 2 additions & 2 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, PyDict};
use pyo3::types::IntoPyDict;
use pyo3::{prelude::*, types::PyMapping};
use std::collections::{BTreeMap, HashMap};
use std::hint::black_box;
Expand All @@ -12,7 +12,7 @@ fn iter_dict(b: &mut Bencher<'_>) {
let dict = &dict.as_borrowed();
let mut sum = 0;
b.iter(|| {
for (k, _v) in dict {
for (k, _v) in dict.iter() {
let i: u64 = k.extract().unwrap();
sum += i;
}
Expand Down
8 changes: 4 additions & 4 deletions pyo3-benches/benches/bench_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,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.attach(py);
let int = int_obj.bind(py);

bench.iter(|| {
let v = black_box(int).extract::<i64>().unwrap();
Expand All @@ -76,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.attach(py);
let int = int_obj.bind(py);

bench.iter(|| {
let py_int = black_box(int).downcast::<PyInt>().unwrap();
Expand All @@ -101,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.attach(py);
let float = float_obj.bind(py);

bench.iter(|| {
let v = black_box(float).extract::<f64>().unwrap();
Expand All @@ -125,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.attach(py);
let float = float_obj.bind(py);

bench.iter(|| {
let py_int = black_box(float).downcast::<PyFloat>().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion pyo3-benches/benches/bench_frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn not_a_list_via_extract_enum(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let any: &Bound<'_, PyAny> = &PyString::new_bound(py, "foobar");

b.iter(|| match black_box(any).extract::<ListOrNotList<'_>>() {
b.iter(|| match black_box(&any).extract::<ListOrNotList<'_>>() {
Ok(ListOrNotList::List(_list)) => panic!(),
Ok(ListOrNotList::NotList(any)) => any,
Err(_) => panic!(),
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn impl_arg_params(
.collect::<Result<_>>()?;
return Ok((
quote! {
let _args = _pyo3::impl_::extract_argument::PyArg::from_ptr_unchecked(py, _args);
let _args = _pyo3::impl_::extract_argument::PyArg::from_ptr(py, _args);
let _kwargs = _pyo3::impl_::extract_argument::PyArg::from_ptr_or_opt(py, _kwargs);
},
arg_convert,
Expand Down
74 changes: 39 additions & 35 deletions pytests/src/pyfunctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,66 @@ use pyo3::types::{PyDict, PyTuple};
#[pyfunction(signature = ())]
fn none() {}

type Any<'py> = Bound<'py, pyo3::PyAny>;
type Dict<'py> = Bound<'py, PyDict>;
type Tuple<'py> = Bound<'py, PyTuple>;

#[pyfunction(signature = (a, b = None, *, c = None))]
fn simple<'a>(
a: &'a PyAny,
b: Option<&'a PyAny>,
c: Option<&'a PyAny>,
) -> (&'a PyAny, Option<&'a PyAny>, Option<&'a PyAny>) {
fn simple<'py>(
a: Any<'py>,
b: Option<Any<'py>>,
c: Option<Any<'py>>,
) -> (Any<'py>, Option<Any<'py>>, Option<Any<'py>>) {
(a, b, c)
}

#[pyfunction(signature = (a, b = None, *args, c = None))]
fn simple_args<'a>(
a: &'a PyAny,
b: Option<&'a PyAny>,
args: &'a PyTuple,
c: Option<&'a PyAny>,
) -> (&'a PyAny, Option<&'a PyAny>, &'a PyTuple, Option<&'a PyAny>) {
fn simple_args<'py>(
a: Any<'py>,
b: Option<Any<'py>>,
args: Tuple<'py>,
c: Option<Any<'py>>,
) -> (Any<'py>, Option<Any<'py>>, Tuple<'py>, Option<Any<'py>>) {
(a, b, args, c)
}

#[pyfunction(signature = (a, b = None, c = None, **kwargs))]
fn simple_kwargs<'a>(
a: &'a PyAny,
b: Option<&'a PyAny>,
c: Option<&'a PyAny>,
kwargs: Option<&'a PyDict>,
fn simple_kwargs<'py>(
a: Any<'py>,
b: Option<Any<'py>>,
c: Option<Any<'py>>,
kwargs: Option<Dict<'py>>,
) -> (
&'a PyAny,
Option<&'a PyAny>,
Option<&'a PyAny>,
Option<&'a PyDict>,
Any<'py>,
Option<Any<'py>>,
Option<Any<'py>>,
Option<Dict<'py>>,
) {
(a, b, c, kwargs)
}

#[pyfunction(signature = (a, b = None, *args, c = None, **kwargs))]
fn simple_args_kwargs<'a>(
a: &'a PyAny,
b: Option<&'a PyAny>,
args: &'a PyTuple,
c: Option<&'a PyAny>,
kwargs: Option<&'a PyDict>,
fn simple_args_kwargs<'py>(
a: Any<'py>,
b: Option<Any<'py>>,
args: Tuple<'py>,
c: Option<Any<'py>>,
kwargs: Option<Dict<'py>>,
) -> (
&'a PyAny,
Option<&'a PyAny>,
&'a PyTuple,
Option<&'a PyAny>,
Option<&'a PyDict>,
Any<'py>,
Option<Any<'py>>,
Tuple<'py>,
Option<Any<'py>>,
Option<Dict<'py>>,
) {
(a, b, args, c, kwargs)
}

#[pyfunction(signature = (*args, **kwargs))]
fn args_kwargs<'a>(
args: &'a PyTuple,
kwargs: Option<&'a PyDict>,
) -> (&'a PyTuple, Option<&'a PyDict>) {
fn args_kwargs<'py>(
args: Tuple<'py>,
kwargs: Option<Dict<'py>>,
) -> (Tuple<'py>, Option<Dict<'py>>) {
(args, kwargs)
}

Expand Down
3 changes: 3 additions & 0 deletions src/conversions/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ use crate::{
FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, Python, ToPyObject,
};

#[cfg(Py_LIMITED_API)]
use crate::types::bytes::PyBytesMethods;

use num_bigint::{BigInt, BigUint};

#[cfg(not(Py_LIMITED_API))]
Expand Down
Loading

0 comments on commit 8ffb364

Please sign in to comment.