⚡️ Speed up function retrieve_selectors_from_schema by 19% in PR #1504 (feature/try-to-beat-the-limitation-of-ee-in-terms-of-singular-elements-pushed-into-batch-inputs)
#1505
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 #1504
If you approve this dependent PR, these changes will be merged into the original PR branch
feature/try-to-beat-the-limitation-of-ee-in-terms-of-singular-elements-pushed-into-batch-inputs.📄 19% (0.19x) speedup for
retrieve_selectors_from_schemaininference/core/workflows/execution_engine/introspection/schema_parser.py⏱️ Runtime :
186 microseconds→157 microseconds(best of86runs)📝 Explanation and details
The optimized code achieves an 18% speedup through several targeted micro-optimizations:
1. Direct OrderedDict Construction
The most significant improvement eliminates the intermediate list allocation in
retrieve_selectors_from_schema. Instead of building a list and then converting it to an OrderedDict with a generator expression, selectors are added directly to the OrderedDict during iteration. This saves memory allocation and reduces the final conversion overhead.2. Reduced Dictionary Access Overhead
In
retrieve_selectors_from_simple_property, theproperty_definitionparameter is aliased topdto avoid repeated dictionary name lookups. While seemingly minor, this reduces attribute resolution overhead in the function's hot path.3. Optimized Set Membership Testing
The dynamic points-to-batch logic now caches set membership results in local variables (
in_batches_and_scalars,in_batches,in_auto_cast) rather than performing the same set membership tests multiple times.4. Conditional List Comprehension
When processing KIND_KEY values, the code now checks if the list is empty before creating the list comprehension, avoiding unnecessary iterator creation for empty cases.
Performance Analysis from Tests:
The optimizations show consistent improvements across all test scenarios, with particularly strong gains (20-30%) on simpler schemas and smaller but meaningful gains (6-11%) on complex union cases. The optimizations are most effective for schemas with many properties, where the direct dictionary construction and reduced lookups compound their benefits. Edge cases like empty schemas show the highest relative improvements (50%+) due to reduced overhead in the main loop structure.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr1504-2025-08-22T08.27.46and push.