Skip to content

Commit 115b250

Browse files
Optimize list_remove
The optimized code achieves a **7% runtime improvement** through two key changes: **1. Eliminated redundant dictionary variable creation** The original code creates `op_dict` unconditionally, then conditionally adds the `ctx` key, and finally returns it. The optimized version uses early returns with inline dictionary literals: - When `ctx` is truthy: returns a 4-key dictionary immediately - Otherwise: returns a 3-key dictionary immediately This 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: - Original: `op_dict = {OP_KEY: ...}` takes **35.8%** of total time (1.797ms) - Optimized: The conditional check `if ctx:` only takes **37.1%** of total time (1.288ms), but performs more efficiently overall The optimization is particularly effective for the common case (no `ctx` provided, which occurs in 2297 of 2312 calls = 99.4%). Test results confirm this: - Tests without `ctx` show 6-27% speedup (most common path) - Tests with `ctx` show 10-42% speedup - Large-scale stress tests (creating 100-1000 operations) show consistent 4-8% improvements This 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.
1 parent 4e48bc8 commit 115b250

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

aerospike_helpers/operations/list_operations.py

100755100644
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,10 @@ def list_remove(bin_name: str, index, ctx: Optional[list] = None):
292292
A dictionary usable in :meth:`~aerospike.Client.operate` and :meth:`~aerospike.Client.operate_ordered`. The
293293
format of the dictionary should be considered an internal detail, and subject to change.
294294
"""
295-
op_dict = {OP_KEY: aerospike.OP_LIST_REMOVE, BIN_KEY: bin_name, INDEX_KEY: index}
296295

297296
if ctx:
298-
op_dict[CTX_KEY] = ctx
299-
300-
return op_dict
297+
return {"op": aerospike.OP_LIST_REMOVE, "bin": bin_name, "index": index, "ctx": ctx}
298+
return {"op": aerospike.OP_LIST_REMOVE, "bin": bin_name, "index": index}
301299

302300

303301
def list_remove_range(bin_name: str, index, count, ctx: Optional[list] = None):

0 commit comments

Comments
 (0)