-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
⚡️ Speed up unwrap_wrapped_function() by 93% in pydantic/_internal/_decorators.py
#9727
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
⚡️ Speed up unwrap_wrapped_function() by 93% in pydantic/_internal/_decorators.py
#9727
Conversation
To optimize the given Python program for faster runtime, we can make several enhancements. Primarily, we make use of Python's tuple for type checks and avoid unnecessary set operations by consolidating them into a single set operation. Here's the optimized version. ### Key Optimizations. 1. **Tuple Consolidation for `isinstance`:** - Instead of building a set and updating it conditionally, we use a tuple that is directly used in `isinstance` checks. 2. **Simplified `isinstance` Checks:** - We handle type checks and reassignments more efficiently without repeated checks or updates to the set, streamlining the process. These changes collectively reduce the complexity and improve the performance by minimizing unnecessary operations and making the control flow straightforward.
…024-06-06T21.13.38
CodSpeed Performance ReportMerging #9727 will not alter performanceComparing Summary
|
sydney-runkle
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look great overall here, but I did find one issue with the unwrap_types logic.
Great work on the docstring corrections and general performance improvements.
| unwrap_types = ( | ||
| (property, cached_property) | ||
| + (partial, partialmethod if unwrap_partial else ()) | ||
| + (staticmethod, classmethod if unwrap_class_static_method else ()) | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| unwrap_types = ( | |
| (property, cached_property) | |
| + (partial, partialmethod if unwrap_partial else ()) | |
| + (staticmethod, classmethod if unwrap_class_static_method else ()) | |
| ) | |
| unwrap_types = ( | |
| (property, cached_property) | |
| + (partial, partialmethod) if unwrap_partial else () | |
| + (staticmethod, classmethod) if unwrap_class_static_method else () | |
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for this, I played a bit with the different ways of doing this and found the best one which had the correct behavior. Updated the PR with the change
pydantic/_internal/_decorators.py
Outdated
| else: | ||
| # Make coverage happy as it can only get here in the last possible case | ||
| assert isinstance(func, cached_property) | ||
| elif isinstance(func, cached_property): | ||
| func = func.func # type: ignore | ||
| elif unwrap_class_static_method and isinstance(func, (classmethod, staticmethod)): | ||
| func = func.__func__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One note here - I think we might make coverage unhappy with this change
sydney-runkle
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Waiting on some annoying CI flakiness to merge, but will do soon!
…024-06-06T21.13.38
Change Summary
📄
unwrap_wrapped_function()inpydantic/_internal/_decorators.py📈 Performance improved by
93%(0.93xfaster)⏱️ Runtime went down from
69.2 microsecondsto35.8 microsecondsExplanation and details
To optimize the given Python program for faster runtime, we can make several enhancements. Primarily, we make use of Python's tuple for type checks and avoid unnecessary set operations by consolidating them into a single set operation. Here's the optimized version.
Key Optimizations.
Tuple Consolidation for
isinstance:isinstancechecks.Simplified
isinstanceChecks:These changes collectively reduce the complexity and improve the performance by minimizing unnecessary operations and making the control flow straightforward.
Correctness verification
The new optimized code was tested for correctness. The results are listed below.
🔘 (none found) − ⚙️ Existing Unit Tests
✅ 12 Passed − 🌀 Generated Regression Tests
(click to show generated tests)
🔘 (none found) − ⏪ Replay Tests
This optimization was discovered automatically with codeflash.ai
Related issue number
Checklist