Skip to content

Commit

Permalink
fixup benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 13, 2024
1 parent ec7598f commit c3d9e69
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 13 deletions.
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
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
8 changes: 4 additions & 4 deletions pyo3-benches/benches/bench_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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.bind(py);

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

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

bench.iter(|| {
let v = black_box(float).extract::<f64>().unwrap();
Expand All @@ -120,7 +120,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.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
4 changes: 2 additions & 2 deletions src/conversions/num_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ macro_rules! complex_conversion {
unsafe {
let complex;
let obj = if obj.is_instance_of::<PyComplex>() {
obj.clone()
obj
} else if let Some(method) =
obj.lookup_special(crate::intern!(obj.py(), "__complex__"))?
{
Expand All @@ -161,7 +161,7 @@ macro_rules! complex_conversion {
// `obj` might still implement `__float__` or `__index__`, which will be
// handled by `PyComplex_{Real,Imag}AsDouble`, including propagating any
// errors if those methods don't exist / raise exceptions.
obj.clone()
obj
};
let ptr = obj.as_ptr();
let real = ffi::PyComplex_RealAsDouble(ptr);
Expand Down
6 changes: 3 additions & 3 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,15 +875,15 @@ impl PyErrArguments for PyDowncastErrorArguments {
}

/// Convert `PyDowncastError` to Python `TypeError`.
impl<'a> std::convert::From<PyDowncastError<'a>> for PyErr {
impl std::convert::From<PyDowncastError<'_>> for PyErr {
fn from(err: PyDowncastError<'_>) -> PyErr {
PyErr::from(err.0)
}
}

impl<'a> std::error::Error for PyDowncastError<'a> {}
impl std::error::Error for PyDowncastError<'_> {}

impl<'a> std::fmt::Display for PyDowncastError<'a> {
impl std::fmt::Display for PyDowncastError<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
self.0.fmt(f)
}
Expand Down
11 changes: 10 additions & 1 deletion src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ impl<'py> Bound<'py, PyAny> {
) -> Option<Self> {
Py::from_borrowed_ptr_or_opt(py, ptr).map(|obj| Self(py, ManuallyDrop::new(obj)))
}

/// Constructs a new Bound from a borrowed pointer, incrementing the reference count.
///
/// # Safety
/// ptr must be a valid pointer to a Python object.
pub unsafe fn from_borrowed_ptr_unchecked(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self {
Self::from_borrowed_ptr_or_opt(py, ptr).unwrap()
}
}

impl<'py, T> Bound<'py, T>
Expand Down Expand Up @@ -511,7 +519,8 @@ impl<'a, 'py> Borrowed<'a, 'py, PyAny> {
/// This is similar to `std::slice::from_raw_parts`, the lifetime `'a` is completely defined by
/// the caller and it's the caller's responsibility to ensure that the reference this is
/// derived from is valid for the lifetime `'a`.
pub(crate) unsafe fn from_ptr_unchecked(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self {
#[doc(hidden)] // Used in macro code.
pub unsafe fn from_ptr_unchecked(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self {
Self(NonNull::new_unchecked(ptr), PhantomData, py)
}

Expand Down
1 change: 0 additions & 1 deletion src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ use crate::ffi_ptr_ext::FfiPtrExt;
use crate::impl_::pyclass::{
PyClassBaseType, PyClassDict, PyClassImpl, PyClassThreadChecker, PyClassWeakRef,
};
use crate::instance::Bound;
use crate::pyclass::{
boolean_struct::{False, True},
PyClass,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_result_conversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ error[E0277]: the trait bound `PyErr: From<MyError>` is not satisfied
| ^^^^^^^^^^^^^ the trait `From<MyError>` is not implemented for `PyErr`
|
= help: the following other types implement trait `From<T>`:
<PyErr as From<PyDowncastError<'_>>>
<PyErr as From<std::io::Error>>
<PyErr as From<PyBorrowError>>
<PyErr as From<PyBorrowMutError>>
<PyErr as From<PyDowncastError<'_>>>
<PyErr as From<DowncastError<'_, '_>>>
<PyErr as From<DowncastIntoError<'_>>>
<PyErr as From<NulError>>
Expand Down

0 comments on commit c3d9e69

Please sign in to comment.