⚡️ Speed up function _get_all_json_refs by 38%#282
Open
codeflash-ai[bot] wants to merge 1 commit intopython-onlyfrom
Open
⚡️ Speed up function _get_all_json_refs by 38%#282codeflash-ai[bot] wants to merge 1 commit intopython-onlyfrom
_get_all_json_refs by 38%#282codeflash-ai[bot] wants to merge 1 commit intopython-onlyfrom
Conversation
This optimization achieves a **37% runtime improvement** (from 20.3ms to 14.7ms) by eliminating the overhead of creating and merging temporary sets during recursive traversal of JSON schemas. ## Key Optimization: Shared Set Pattern The optimized code replaces the original's `refs.update(_get_all_json_refs(value))` pattern with a helper function `_collect_json_refs(item, refs)` that mutates a shared set in-place. This eliminates: 1. **Set creation overhead**: The original creates a new `set()` on every recursive call (72,401 allocations according to line profiler) 2. **Set merge overhead**: Each `refs.update()` call copies elements from the returned set into the parent set The line profiler data shows the impact clearly: - Original: Lines with `refs.update()` consume 33-34% of total time (79.46M + 2.677M + 2.615M ns) - Optimized: A single call to `_collect_json_refs()` handles all work (224.6M ns total) ## Why This Works In Python, set operations like `update()` have overhead even when merging small sets. For deeply nested JSON schemas with many `$ref` entries (like the test cases with 1000+ refs), this overhead multiplies across recursive calls. By passing a single shared set through the recursion stack and using direct `add()` operations, we avoid this multiplicative cost. ## Performance Characteristics The optimization excels for **nested and large-scale JSON schemas**: - **Deeply nested structures** (100 levels): 170% faster (125μs → 46.2μs) - **Wide structures** (500 refs): 30-52% faster - **Mixed nested lists/dicts** (1000+ items): 36-42% faster For **trivial inputs** (empty dicts/lists, primitives), there's a 10-28% slowdown due to the additional function call overhead. However, these cases complete in ~300-700ns (negligible absolute time), while the optimization targets real-world JSON schema parsing where nested structures are common. ## Impact on Workloads Since `_get_all_json_refs` is used for JSON schema reference extraction, this optimization particularly benefits: - Large API schemas with interconnected type definitions - Schema validation pipelines processing many documents - Tools that analyze or transform complex JSON schemas The test results confirm that real-world usage patterns (complex schemas with multiple nesting levels and many refs) see significant speedups, making this optimization valuable despite minor regressions on trivial cases.
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
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.
📄 38% (0.38x) speedup for
_get_all_json_refsinsrc/algorithms/search.py⏱️ Runtime :
20.3 milliseconds→14.7 milliseconds(best of103runs)📝 Explanation and details
This optimization achieves a 37% runtime improvement (from 20.3ms to 14.7ms) by eliminating the overhead of creating and merging temporary sets during recursive traversal of JSON schemas.
Key Optimization: Shared Set Pattern
The optimized code replaces the original's
refs.update(_get_all_json_refs(value))pattern with a helper function_collect_json_refs(item, refs)that mutates a shared set in-place. This eliminates:set()on every recursive call (72,401 allocations according to line profiler)refs.update()call copies elements from the returned set into the parent setThe line profiler data shows the impact clearly:
refs.update()consume 33-34% of total time (79.46M + 2.677M + 2.615M ns)_collect_json_refs()handles all work (224.6M ns total)Why This Works
In Python, set operations like
update()have overhead even when merging small sets. For deeply nested JSON schemas with many$refentries (like the test cases with 1000+ refs), this overhead multiplies across recursive calls. By passing a single shared set through the recursion stack and using directadd()operations, we avoid this multiplicative cost.Performance Characteristics
The optimization excels for nested and large-scale JSON schemas:
For trivial inputs (empty dicts/lists, primitives), there's a 10-28% slowdown due to the additional function call overhead. However, these cases complete in ~300-700ns (negligible absolute time), while the optimization targets real-world JSON schema parsing where nested structures are common.
Impact on Workloads
Since
_get_all_json_refsis used for JSON schema reference extraction, this optimization particularly benefits:The test results confirm that real-world usage patterns (complex schemas with multiple nesting levels and many refs) see significant speedups, making this optimization valuable despite minor regressions on trivial cases.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-_get_all_json_refs-mlujbi71and push.