⚡️ Speed up function list_get_by_index by 8%
#119
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.
📄 8% (0.08x) speedup for
list_get_by_indexinaerospike_helpers/operations/list_operations.py⏱️ Runtime :
252 microseconds→234 microseconds(best of5runs)📝 Explanation and details
The optimized code achieves an 8% runtime improvement by eliminating conditional dictionary mutation in favor of separate return paths based on the
ctxparameter.Key Optimization:
In the original code, a dictionary is always created with 4 keys, then conditionally mutated to add a 5th key (
CTX_KEY) whenctxis truthy:The optimized version creates the dictionary once with the exact keys needed, avoiding the mutation step:
Why This is Faster:
Eliminates dictionary mutation overhead: The original code incurs the cost of
op_dict[CTX_KEY] = ctxas a separate dictionary insertion operation, which requires rehashing and potential dictionary resizing checks.Predictable dictionary size: Python can optimize dictionary creation when the final size is known upfront (literal dictionary construction), as opposed to incremental growth.
Reduced bytecode operations: The optimized version has fewer bytecode instructions—no intermediate variable assignment, no post-creation mutation.
Performance Characteristics:
Looking at the annotated tests, the optimization shows consistent improvements:
The speedup is most pronounced when
ctxis provided because the original code performed both dictionary creation AND mutation, while the optimized version only performs creation. For the no-ctx case, the benefit comes from avoiding the intermediate variable and having a more direct return path.This optimization is particularly effective for this function since it appears to be a lightweight operation builder, likely called frequently when constructing Aerospike database operations. The 8% aggregate improvement scales well across all test scenarios.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_nested_cdt_ctx.py::TestCTXOperations.test_cdt_ctx_list_index_create_negtest_nested_cdt_ctx.py::TestCTXOperations.test_cdt_ctx_list_index_create_postest_nested_cdt_ctx.py::TestCTXOperations.test_ctx_list_get_by_indextest_nested_cdt_ctx.py::TestCTXOperations.test_ctx_list_get_by_index_negativetest_new_list_operation_helpers.py::TestNewListOperationsHelpers.test_get_by_indextest_new_list_operation_helpers.py::TestNewListOperationsHelpers.test_list_get_by_index_return_types🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-list_get_by_index-ml0no5izand push.