Skip to content

Delay formatting type errors to improve performance of failed extractions.#315

Merged
adamreichold merged 2 commits into
mainfrom
optimize-type-error
Apr 9, 2022
Merged

Delay formatting type errors to improve performance of failed extractions.#315
adamreichold merged 2 commits into
mainfrom
optimize-type-error

Conversation

@adamreichold

Copy link
Copy Markdown
Member

This should be especially useful for functions that accept several element types like what was discussed in #289. Even more so, as rust-numpy implements PyTypeInfo::is_type_of in terms of FromPyObject::extract.

The benchmark results seem to justify the manual implementation of PyErrArguments:

name             main ns/iter  pr ns/iter  diff ns/iter   diff %   speedup 
extract_failure  13,326        90               -13,236  -99.32%  x 148.07 
extract_success  20            19                    -1   -5.00%    x 1.05 

Comment thread src/error.rs
pub struct TypeError {
from: String,
to: String,
pub struct TypeError<'a> {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, this addition of a lifetime parameter to TypeError means this cannot be part of a maintenance release because TypeError is part of the crate API even it is not part of any type signature as it is only ever produced wrapped within PyErr. :-(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to actually remove DimensionalityError and TypeError from the API. They are never used directly and cannot be recovered via downcasting either. So the additional API surface does not gain us anything.

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice speedup! I hadn't looked too hard at optimizing error pathways in PyO3 so far. It looks like we might be introducing some nice speedups into the 0.17 release cycle :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants