⚡️ Speed up function retrieve_selectors_from_union_definition by 98% in PR #1764 (joao/fix-array-input-references)
#1907
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.
⚡️ This pull request contains optimizations for PR #1764
If you approve this dependent PR, these changes will be merged into the original PR branch
joao/fix-array-input-references.📄 98% (0.98x) speedup for
retrieve_selectors_from_union_definitionininference/core/workflows/execution_engine/introspection/schema_parser.py⏱️ Runtime :
246 microseconds→125 microseconds(best of190runs)📝 Explanation and details
The optimized code achieves a 97% speedup (246μs → 125μs) through two key optimizations:
1. Inlining
property_defines_union()(Primary Impact)What changed: The
property_defines_union()function call was replaced with inline dictionary key checks inretrieve_selectors_from_simple_property.Why it's faster:
Key Change in
retrieve_selectors_from_simple_property:The line profiler shows this optimization reduced time spent on the union check from 2.91ms (61.3%) to 1.59ms (45.5%) - a significant improvement in the most expensive operation.
Key optimization in
retrieve_selectors_from_union_definition:The original code concatenated three lists using
+operator:The optimized version iterates over keys directly and skips empty results:
Why this is faster:
Inlining
property_defines_union(): The original code called a function that performed 3 dictionary lookups. Inlining eliminates function call overhead (~300-400ns per call) and reduces the lookup pattern from "check all 3 keys in function → check all 3 keys again in union handling" to just checking once.Avoiding list concatenation: The original code created 3 intermediate lists via
.get()calls and concatenated them with+operators, creating a new list. The optimized version iterates over the three keys directly, checking existence before iteration, avoiding list construction overhead.Flattening nested iteration: The original code used
itertools.chain.from_iterable()to flattenallowed_referencesfrom multiple results. The optimized version uses a single nested loop that directly iterates over references, reducing intermediate list creation overhead.Performance Impact:
property_defines_unioncall inretrieve_selectors_from_simple_propertydropped from 61.3% of function time (2.91ms) to ~45% (1.59ms total for the three inlined checks).retrieve_selectors_from_union_definition, eliminating list concatenation and using early-exit iteration reduced union type processing from 1.1% to 0.6% of total time.Why this is faster:
property_defines_union()removes ~900ns of function call overhead per invocation (visible in line profiler: 2.9μs spent on that call alone for 951 hits).get()calls plus list concatenation inretrieve_selectors_from_union_definition. The optimized version uses a single loop checking each key once with earlycontinueon empty results+operator concatenation; the optimized version iterates keys directly, eliminating temporary list allocationsitertools.chain.from_iterable()on a generator to direct nested iteration, reducing iterator overheadImpact on workloads:
Based on
function_references, this function is called recursively during schema parsing, particularly when processing union types in workflow definitions. The optimizations reduce overhead per call, which compounds when processing schemas with multiple union definitions. The test results show consistent 130-150% speedup across all test cases, with the largest gains (190-199%) in edge cases with empty unions where the early exit optimization is most beneficial. The 39% speedup on the large mixed union test (300 references) demonstrates good scalability of the optimization.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1764-2026-01-12T18.20.55and push.