⚡️ Speed up function retrieve_batch_compatibility_of_input_selectors by 30% in PR #1504 (feature/try-to-beat-the-limitation-of-ee-in-terms-of-singular-elements-pushed-into-batch-inputs)#1507
Closed
Conversation
…` by 30% in PR #1504 (`feature/try-to-beat-the-limitation-of-ee-in-terms-of-singular-elements-pushed-into-batch-inputs`) The optimized code achieves a **29% speedup** through two key optimizations that reduce overhead in the inner loop: **Key optimizations:** 1. **Eliminates repeated attribute lookups**: Caches `parsed_selector.definition.property_name` in a local variable instead of accessing it twice per inner loop iteration 2. **Reduces dictionary access overhead**: Stores a reference to the target set (`batch_compatibility_of_properties[property_name]`) and reuses it, avoiding repeated dictionary lookups 3. **Uses in-place set union (`|=`)** instead of the `update()` method, which has slightly less overhead for set operations **Performance impact by test case:** - **Small inputs (1-10 selectors)**: Modest 1-10% improvements due to reduced method call overhead - **Medium inputs (100-500 selectors)**: 12-25% speedups as the optimizations compound with more iterations - **Large inputs with many references**: Up to 149% improvement in cases with many references per selector, where the inner loop dominates runtime The line profiler shows the optimization moves expensive work (attribute lookups and dictionary access) from the inner loop to the outer loop. The original code performed `parsed_selector.definition.property_name` lookup 12,672 times, while the optimized version does it only 3,432 times - exactly once per selector instead of once per reference. This optimization is particularly effective for workflows with selectors containing many allowed references, which is common in batch processing scenarios.
4 tasks
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.
⚡️ 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.📄 30% (0.30x) speedup for
retrieve_batch_compatibility_of_input_selectorsininference/core/workflows/execution_engine/v1/compiler/graph_constructor.py⏱️ Runtime :
1.28 milliseconds→987 microseconds(best of274runs)📝 Explanation and details
The optimized code achieves a 29% speedup through two key optimizations that reduce overhead in the inner loop:
Key optimizations:
parsed_selector.definition.property_namein a local variable instead of accessing it twice per inner loop iterationbatch_compatibility_of_properties[property_name]) and reuses it, avoiding repeated dictionary lookups|=) instead of theupdate()method, which has slightly less overhead for set operationsPerformance impact by test case:
The line profiler shows the optimization moves expensive work (attribute lookups and dictionary access) from the inner loop to the outer loop. The original code performed
parsed_selector.definition.property_namelookup 12,672 times, while the optimized version does it only 3,432 times - exactly once per selector instead of once per reference.This optimization is particularly effective for workflows with selectors containing many allowed references, which is common in batch processing scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr1504-2025-08-22T15.35.37and push.