⚡️ Speed up function list_remove_by_value_list by 14%
#121
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.
📄 14% (0.14x) speedup for
list_remove_by_value_listinaerospike_helpers/operations/list_operations.py⏱️ Runtime :
46.3 microseconds→40.6 microseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 13% runtime improvement (from 46.3μs to 40.6μs) by eliminating a repeated attribute lookup overhead on every function call.
Key Optimization:
The main change caches the Aerospike operation constant
aerospike.OP_LIST_REMOVE_BY_VALUE_LISTas a module-level variable_OP_LIST_REMOVE_BY_VALUE_LISTat import time. This eliminates the need to perform an attribute lookup through theaerospikemodule namespace on every function invocation.Why This Improves Performance:
In Python, attribute access (like
aerospike.OP_LIST_REMOVE_BY_VALUE_LIST) requires a dictionary lookup in the module's namespace dictionary at runtime. By caching this constant once at module import time and referencing the cached value, we avoid this repeated lookup overhead. The line profiler data shows this optimization reduced the time spent on that specific line from 45,577ns (20.4% of total time) to 38,468ns (17.4% of total time) - a clear reduction in per-call overhead.Impact on Test Cases:
The optimization provides consistent speedups across all test scenarios:
The gains are most pronounced in simpler test cases where the attribute lookup represents a larger proportion of the total work. All test cases benefit because every invocation eliminates one attribute lookup.
Why This Matters:
This function is likely called frequently when building operation lists for Aerospike database operations. Since it's a lightweight helper that constructs dictionaries, eliminating even a small per-call overhead compounds significantly when called many times in batch operations or hot paths. The optimization is especially valuable for high-throughput scenarios where this function might be invoked thousands of times per second.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_nested_cdt_ctx.py::TestCTXOperations.test_ctx_list_remove_by_value_listtest_nested_cdt_ctx.py::TestCTXOperations.test_ctx_list_remove_by_value_list_negativetest_new_list_operation_helpers.py::TestNewListOperationsHelpers.test_remove_by_value_listtest_new_list_operation_helpers.py::TestNewListOperationsHelpers.test_remove_by_value_list_inverted🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-list_remove_by_value_list-ml0puuumand push.