⚡️ Speed up method Batch.init by 22%
#594
Open
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.
📄 22% (0.22x) speedup for
Batch.initininference/core/workflows/execution_engine/entities/base.py⏱️ Runtime :
46.8 microseconds→38.2 microseconds(best of320runs)📝 Explanation and details
The optimization removes keyword arguments from the constructor call in the
initmethod, changingcls(content=content, indices=indices)tocls(content, indices).This eliminates the overhead of Python's keyword argument handling mechanism, which involves:
The 22% speedup is achieved because object instantiation becomes more direct - Python can pass arguments positionally without the extra dictionary creation and lookup steps. This optimization is particularly effective for frequently called factory methods like
init.The test results show consistent 20-35% improvements across all scenarios, with the best gains on simpler cases (empty lists: 36.1%, basic operations: 25-30%). Even complex scenarios with large datasets maintain 15-30% improvements, demonstrating that the optimization scales well regardless of content size or complexity.
Since the constructor signature remains unchanged and arguments are passed in the same order, this is a pure performance optimization with no behavioral changes.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
workflows/unit_tests/execution_engine/entities/test_base.py::test_broadcast_batch_when_requested_size_is_equal_to_batch_sizeworkflows/unit_tests/execution_engine/entities/test_base.py::test_broadcast_batch_when_requested_size_is_invalidworkflows/unit_tests/execution_engine/entities/test_base.py::test_broadcast_batch_when_requested_size_is_valid_and_batch_size_is_not_matchingworkflows/unit_tests/execution_engine/entities/test_base.py::test_broadcast_batch_when_requested_size_is_valid_and_batch_size_is_oneworkflows/unit_tests/execution_engine/entities/test_base.py::test_filtering_out_batch_elementsworkflows/unit_tests/execution_engine/entities/test_base.py::test_getting_batch_element_when_valid_element_is_chosenworkflows/unit_tests/execution_engine/entities/test_base.py::test_getting_batch_element_when_valid_invalid_element_is_chosenworkflows/unit_tests/execution_engine/entities/test_base.py::test_getting_batch_lengthworkflows/unit_tests/execution_engine/entities/test_base.py::test_initialising_batch_with_misaligned_indicesworkflows/unit_tests/execution_engine/entities/test_base.py::test_standard_iteration_through_batchworkflows/unit_tests/execution_engine/entities/test_base.py::test_standard_iteration_through_batch_with_indices🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Batch.init-mh9y6sa3and push.