-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
⚡️ Speed up is_pydantic_dataclass() by 41% in pydantic/dataclasses.py
#9652
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 is_pydantic_dataclass() by 41% in pydantic/dataclasses.py
#9652
Conversation
Here's the optimized version of the provided program. ### Explanation. 1. **Short-circuit Evaluation:** I reordered the conditions in the `return` statement to utilize short-circuit evaluation. The check for `'__pydantic_validator__' in class_.__dict__` is less expensive and will fail fast if `class_` is not a valid type with this attribute, reducing unnecessary calls to the relatively more expensive `dataclasses.is_dataclass` function. 2. **Error Handling:** Added a `try-except` block to quickly handle the scenario where `class_` may not have the `__dict__` attribute (`__dict__` is not present for some objects that do not have attribute dictionaries). This prevents the function from throwing an attribute error and failing incompletely. This should make the function slightly faster, especially when dealing with objects that early out on the `__dict__` check.
CodSpeed Performance ReportMerging #9652 will not alter performanceComparing Summary
|
|
How about: if hasattr(class_, '__dict__'):
return '__pydantic_validator__' in class_.__dict__ and dataclasses.is_dataclass(class_)
return FalseWhat are the performance changes like with that approach? |
|
Great suggestion, when I tested your recommendation with codeflash on my laptop, I got a 23% speedup. But when I try the try/except approach (the one that codeflash originally recommended), I got a 36% speedup. So the try/except approach seems to be faster. |
Awesome, thanks for looking into this. We're happy with the existing change, then! I'll note, this isn't a super critical function to speed up, but we'll take the performance benefit where we can get it :). We're excited to collaborate more on more critical functions! |
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! Thanks folks!
Change Summary
📄
is_pydantic_dataclass()inpydantic/dataclasses.py📈 Performance improved by
41%(0.41xfaster)⏱️ Runtime went down from
4.80 microsecondsto3.40 microsecondsExplanation and details
Here's the optimized version of the provided program.
Explanation.
Short-circuit Evaluation: I reordered the conditions in the
returnstatement to utilize short-circuit evaluation. The check for'__pydantic_validator__' in class_.__dict__is less expensive and will fail fast ifclass_is not a valid type with this attribute, reducing unnecessary calls to the relatively more expensivedataclasses.is_dataclassfunction.Error Handling: Added a
try-exceptblock to quickly handle the scenario whereclass_may not have the__dict__attribute (__dict__is not present for some objects that do not have attribute dictionaries). This prevents the function from throwing an attribute error and failing incompletely.This should make the function slightly faster, especially when dealing with objects that early out on the
__dict__check.Correctness verification
The new optimized code was tested for correctness. The results are listed below.
✅ 211 Passed − ⚙️ Existing Unit Tests
(click to show existing tests)
Checklist