⚡️ Speed up function list_remove by 7%
#114
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.
📄 7% (0.07x) speedup for
list_removeinaerospike_helpers/operations/list_operations.py⏱️ Runtime :
803 microseconds→751 microseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 7% runtime improvement through two key changes:
1. Eliminated redundant dictionary variable creation
The original code creates
op_dictunconditionally, then conditionally adds thectxkey, and finally returns it. The optimized version uses early returns with inline dictionary literals:ctxis truthy: returns a 4-key dictionary immediatelyThis eliminates the intermediate variable and reduces memory operations.
2. Replaced constant lookups with string literals
The original code references module-level constants (
OP_KEY,BIN_KEY,INDEX_KEY,CTX_KEY) for dictionary keys. The optimized version uses direct string literals ("op","bin","index","ctx"). While this seems minor, it removes ~4 attribute lookups per function call, which compounds when the function is called frequently.Why this matters:
From the line profiler data:
op_dict = {OP_KEY: ...}takes 35.8% of total time (1.797ms)if ctx:only takes 37.1% of total time (1.288ms), but performs more efficiently overallThe optimization is particularly effective for the common case (no
ctxprovided, which occurs in 2297 of 2312 calls = 99.4%). Test results confirm this:ctxshow 6-27% speedup (most common path)ctxshow 10-42% speedupThis function appears to be a factory for operation dictionaries in the Aerospike client library. Since database operations often batch multiple commands, even small per-call savings multiply across workloads that create many list operations.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_nested_cdt_ctx.py::TestCTXOperations.test_ctx_list_removetest_nested_cdt_ctx.py::TestCTXOperations.test_ctx_list_remove_negative🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-list_remove-ml0mbtk7and push.