forked from pydantic/pydantic-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
⚡️ Speed up method JsonSchemaTransformer.walk
by 730%
#33
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
Open
codeflash-ai
wants to merge
50
commits into
main
Choose a base branch
from
codeflash/optimize-JsonSchemaTransformer.walk-mdeysnzp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
⚡️ Speed up method JsonSchemaTransformer.walk
by 730%
#33
codeflash-ai
wants to merge
50
commits into
main
from
codeflash/optimize-JsonSchemaTransformer.walk-mdeysnzp
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…sage part instead of stringifying (pydantic#2173) Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: Douwe Maan <[email protected]>
REFINEMENT Here’s an optimized rewrite of your program that focuses on **avoiding unnecessary deepcopies, minimizing dict/list allocations, reducing method call overhead, and short-circuiting where possible**. **No changes are made to function signatures or return values. Comments are preserved unless necessary to update.** Key optimizations. - **Avoid deepcopy:** If in-place mutation is not needed, shallow-copy only what must be mutated (especially root-level dictionaries). - **Minimize new dict/list creation:** Use generator dict comprehensions where possible. - **Short-circuit early:** Reduce key lookups and regexp use if not needed. - **Hoist attribute/constant lookups:** Assign methods/attrs to local names in tight loops. - **String handling:** For `$ref`, use slicing if the pattern is constant instead of `re.sub`. - **Reduce handle calls for non-structured types:** Only dispatch the necessary function. **Notable changes:** - Avoid full `deepcopy` of large root schema (only copy what's changing). - Avoid regex unless necessary (use string slice for `#/$defs/`). - Inline `.get()` calls where used only once. - Use explicit checks for keys instead of calling `_handle_union` unconditionally. - Inline local variable bindings for hot-attribute access. This should make traversal and transformation notably faster, especially for large schema documents or many nested `$refs`. **All function signatures and expected behavior are preserved.**
mohammedahmed18
approved these changes
Jul 23, 2025
Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: Loïc POISOT <[email protected]>
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
…c#2247) Co-authored-by: Douwe Maan <[email protected]>
misrasaurabh1
approved these changes
Jul 23, 2025
… tools (pydantic#2094) Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: Douwe Maan <[email protected]>
… media type (pydantic#2270) Co-authored-by: David <[email protected]>
…ient` arg (pydantic#2214) Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: Douwe Maan <[email protected]>
…tic#2282) Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: Alex Hall <[email protected]>
Co-authored-by: Douwe Maan <[email protected]>
… with output tools (pydantic#2314) Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Douwe Maan <[email protected]>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Douwe Maan <[email protected]>
Signed-off-by: Saurabh Misra <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 730% (7.30x) speedup for
JsonSchemaTransformer.walk
inpydantic_ai_slim/pydantic_ai/profiles/_json_schema.py
⏱️ Runtime :
6.49 milliseconds
→782 microseconds
(best of144
runs)📝 Explanation and details
Saurabh's note: Test suite manually reviewed — includes recursion, nesting, union flattening, $ref/$defs logic, and large inputs to ensure correctness across common and edge schema patterns.
Here’s an optimized rewrite of your program that focuses on avoiding unnecessary deepcopies, minimizing dict/list allocations, reducing method call overhead, and short-circuiting where possible.
No changes are made to function signatures or return values. Comments are preserved unless necessary to update.
Key optimizations.
$ref
, use slicing if the pattern is constant instead ofre.sub
.This should make traversal and transformation notably faster, especially for large schema documents or many nested
$refs
.All function signatures and expected behavior are preserved.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-JsonSchemaTransformer.walk-mdeysnzp
and push.