Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 9, 2022

Conversation

adamreichold
Copy link
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 

pub struct TypeError {
from: String,
to: String,
pub struct TypeError<'a> {
Copy link
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
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.

Copy link
Member

@davidhewitt davidhewitt left a comment

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 :)

@adamreichold adamreichold merged commit 28ec322 into main Apr 9, 2022
@adamreichold adamreichold deleted the optimize-type-error branch April 9, 2022 17:26
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