⚡️ Speed up function list_remove_by_value by 6%
#120
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.
📄 6% (0.06x) speedup for
list_remove_by_valueinaerospike_helpers/operations/list_operations.py⏱️ Runtime :
1.03 milliseconds→964 microseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 6% runtime improvement by eliminating dictionary mutation overhead through a dual-path return strategy.
Key Optimization:
Instead of creating a single dictionary and conditionally mutating it (adding
CTX_KEYwhenctxis truthy), the optimized version uses two separate return paths:ctxis truthy: directly return a 6-key dictionary includingCTX_KEYctxis falsy: directly return a 5-key dictionary withoutCTX_KEYWhy This Is Faster:
Eliminates dictionary mutation: The original code creates a 5-key dictionary, then conditionally adds a 6th key via
op_dict[CTX_KEY] = ctx. Dictionary item assignment involves hash computation, collision handling, and potential resizing - all avoided in the optimized version.Reduces variable lookups: The original code stores the dictionary in
op_dict, then returns it - requiring one variable assignment and one variable lookup. The optimized version returns directly, eliminating these operations.Dictionary literal optimization: Python can optimize dictionary literals at compile time more effectively than runtime mutations. The optimized code constructs the dictionary at the return statement, allowing the interpreter to potentially pre-size the dictionary correctly.
Performance Analysis from Line Profiler:
if ctx:is slightly faster in the optimized version (1.22 ms vs 1.11 ms) due to fewer subsequent operationsop_dict[CTX_KEY] = ctx(242 µs) is completely eliminatedTest Results Validation:
The annotated tests show consistent improvements, particularly:
ctx(most common case): 2-13% fasterctx: 16-38% faster (larger gains due to avoiding mutation overhead)ctxacross 150 calls: 9% fasterThis optimization is particularly effective when
list_remove_by_valueis called frequently in data processing pipelines, as the 6% speedup compounds over many invocations.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_key_ordered_dict_get_by_value.py::TestOrderedDictGetByValue.test_list_remove_by_value_ordered_dicttest_nested_cdt_ctx.py::TestCTXOperations.test_ctx_list_remove_by_valuetest_nested_cdt_ctx.py::TestCTXOperations.test_ctx_list_remove_by_value_negativetest_new_list_operation_helpers.py::TestNewListOperationsHelpers.test_remove_by_value_invertedtest_new_list_operation_helpers.py::TestNewListOperationsHelpers.test_remove_by_value_no_duplicatestest_new_list_operation_helpers.py::TestNewListOperationsHelpers.test_remove_by_value_with_duplicates🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-list_remove_by_value-ml0pkr7dand push.