Skip to content

Commit

Permalink
Merge pull request #2279 from PyO3/extract-error-is-slow
Browse files Browse the repository at this point in the history
Add benchmark highlighting the costs of failed calls to FromPyObject::extract.
  • Loading branch information
adamreichold committed Apr 10, 2022
2 parents e6901e3 + 10c285b commit 551db72
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Default to "m" ABI tag when choosing `libpython` link name for CPython 3.7 on Unix. [#2288](https://github.com/PyO3/pyo3/pull/2288)
- Improved performance of failing calls to `FromPyObject::extract` which is common when functions accept multiple distinct types. [#2279](https://github.com/PyO3/pyo3/pull/2279)

### Fixed

Expand Down
70 changes: 68 additions & 2 deletions benches/bench_frompyobject.rs
@@ -1,6 +1,9 @@
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};

use pyo3::{prelude::*, types::PyString};
use pyo3::{
prelude::*,
types::{PyList, PyString},
};

#[derive(FromPyObject)]
enum ManyTypes {
Expand All @@ -18,8 +21,71 @@ fn enum_from_pyobject(b: &mut Bencher<'_>) {
})
}

fn list_via_cast_as(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let any: &PyAny = PyList::empty(py).into();

b.iter(|| {
let _list: &PyList = black_box(any).cast_as().unwrap();
});
})
}

fn list_via_extract(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let any: &PyAny = PyList::empty(py).into();

b.iter(|| {
let _list: &PyList = black_box(any).extract().unwrap();
});
})
}

fn not_a_list_via_cast_as(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let any: &PyAny = PyString::new(py, "foobar").into();

b.iter(|| {
black_box(any).cast_as::<PyList>().unwrap_err();
});
})
}

fn not_a_list_via_extract(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let any: &PyAny = PyString::new(py, "foobar").into();

b.iter(|| {
black_box(any).extract::<&PyList>().unwrap_err();
});
})
}

#[derive(FromPyObject)]
enum ListOrNotList<'a> {
List(&'a PyList),
NotList(&'a PyAny),
}

fn not_a_list_via_extract_enum(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let any: &PyAny = PyString::new(py, "foobar").into();

b.iter(|| match black_box(any).extract::<ListOrNotList<'_>>() {
Ok(ListOrNotList::List(_list)) => panic!(),
Ok(ListOrNotList::NotList(_any)) => (),
Err(_) => panic!(),
});
})
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("enum_from_pyobject", enum_from_pyobject);
c.bench_function("list_via_cast_as", list_via_cast_as);
c.bench_function("list_via_extract", list_via_extract);
c.bench_function("not_a_list_via_cast_as", not_a_list_via_cast_as);
c.bench_function("not_a_list_via_extract", not_a_list_via_extract);
c.bench_function("not_a_list_via_extract_enum", not_a_list_via_extract_enum);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
26 changes: 25 additions & 1 deletion src/err/mod.rs
Expand Up @@ -663,10 +663,34 @@ impl<'a> IntoPy<PyObject> for &'a PyErr {
}
}

struct PyDowncastErrorArguments {
from: Py<PyType>,
to: Cow<'static, str>,
}

impl PyErrArguments for PyDowncastErrorArguments {
fn arguments(self, py: Python<'_>) -> PyObject {
format!(
"'{}' object cannot be converted to '{}'",
self.from
.as_ref(py)
.name()
.unwrap_or("<failed to extract type name>"),
self.to
)
.to_object(py)
}
}

/// Convert `PyDowncastError` to Python `TypeError`.
impl<'a> std::convert::From<PyDowncastError<'a>> for PyErr {
fn from(err: PyDowncastError<'_>) -> PyErr {
exceptions::PyTypeError::new_err(err.to_string())
let args = PyDowncastErrorArguments {
from: err.from.get_type().into(),
to: err.to,
};

exceptions::PyTypeError::new_err(args)
}
}

Expand Down

0 comments on commit 551db72

Please sign in to comment.