⚡️ Speed up function remove_indices by 16%
#589
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.
📄 16% (0.16x) speedup for
remove_indicesininference/core/workflows/execution_engine/v1/executor/execution_data_manager/step_input_assembler.py⏱️ Runtime :
277 microseconds→240 microseconds(best of410runs)📝 Explanation and details
The optimized code achieves a 15% speedup through three key changes that reduce Python's type-checking overhead:
1. Type checking optimization: Replaced
isinstance(value, dict)andisinstance(value, list)withtype(value) is dictandtype(value) is list. Thetype() iscomparison is faster because it performs exact type matching without walking the inheritance hierarchy, whileisinstance()must check for subclasses.2. Control flow restructuring: Changed from multiple independent
ifstatements to anif-elif-elsechain. This eliminates redundant type checks - once a condition matches, the remaining checks are skipped entirely.3. Function call streamlining: Removed the explicit
value=keyword argument in recursive calls, which slightly reduces Python's function call overhead.Performance characteristics from tests:
test_remove_indices_large_list_of_batches: 21.3% faster)elifcheck, but these are typically fast operations anywayThe optimization is most effective when processing large collections with many recursive calls, where the cumulative savings from faster type checking compound significantly. The
isinstance(value, Batch)check remains unchanged to preserve compatibility with potential Batch subclasses.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
workflows/unit_tests/execution_engine/executor/execution_data_manager/test_step_input_assembler.py::test_remove_empty_indices🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-remove_indices-mh9v8vpaand push.