-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
⚡️ Speed up _get_all_json_refs() by 34% in pydantic/json_schema.py
#9650
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 _get_all_json_refs() by 34% in pydantic/json_schema.py
#9650
Conversation
Here is the optimized version of the provided Python program. The key optimization here leverages a stack-based approach to avoid the overhead associated with recursive function calls, which can be significant when dealing with deeply nested structures. ### Improvements. 1. **Stack-Based Iteration**: By using an explicit stack, we avoid the overhead of recursive function calls. 2. **Stack Extension**: The `extend` method is utilized to add multiple items to the stack, reducing the number of loop iterations. This approach ensures better performance, especially for deeply nested JSON structures, by keeping the processing within the iteration loop instead of the function call stack.
CodSpeed Performance ReportMerging #9650 will not alter performanceComparing Summary
|
|
This looks great! I'd like to add a test for a complicated json schema generation function + merge that into |
|
This and the other 2 PRs by the OP look more like an advertisement for this code scanning tool with which the changes were generated . The title, PR description and code change all look auto generated. |
|
Hi @vigneshmanick, yes this optimization was auto generated and validated with Codeflash, but before opening this PR I also manually vetted it and I am in touch with the Pydantic team to confirm if these optimizations are good. |
|
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.
This is pretty awesome. Super excited about this kind of capability @misrasaurabh1.
I'm ok with merging this as is, given the pretty exhaustive test suite that was generated + attached to this PR.
Change Summary
📄
_get_all_json_refs()inpydantic/json_schema.py📈 Performance improved by
34%(0.34xfaster)⏱️ Runtime went down from
544 microsecondsto405 microsecondsExplanation and details
Here is the optimized version of the provided Python program. The key optimization here leverages a stack-based approach to avoid the overhead associated with recursive function calls, which can be significant when dealing with deeply nested structures.
Improvements.
extendmethod is utilized to add multiple items to the stack, reducing the number of loop iterations.This approach ensures better performance, especially for deeply nested JSON structures, by keeping the processing within the iteration loop instead of the function call stack.
Correctness verification
The new optimized code was tested for correctness. The results are listed below.
🔘 (none found) − ⚙️ Existing Unit Tests
✅ 15 Passed − 🌀 Generated Regression Tests
(click to show generated tests)
🔘 (none found) − ⏪ Replay Tests
This optimization was discovered automatically using codeflash.ai
Checklist