From df1fa934fb27b9340345d6de80b56e9b5457d2ee Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 30 Jan 2026 09:56:41 +0000 Subject: [PATCH] Optimize list_remove_by_value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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_KEY` when `ctx` is truthy), the optimized version uses two separate return paths: - When `ctx` is truthy: directly return a 6-key dictionary including `CTX_KEY` - When `ctx` is falsy: directly return a 5-key dictionary without `CTX_KEY` **Why This Is Faster:** 1. **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. 2. **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. 3. **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:** - Original total time: 10.16 ms - Optimized total time: 8.43 ms - The conditional check `if ctx:` is slightly faster in the optimized version (1.22 ms vs 1.11 ms) due to fewer subsequent operations - The mutation operation `op_dict[CTX_KEY] = ctx` (242 µs) is completely eliminated **Test Results Validation:** The annotated tests show consistent improvements, particularly: - Tests **without** `ctx` (most common case): 2-13% faster - Tests **with** `ctx`: 16-38% faster (larger gains due to avoiding mutation overhead) - Large-scale tests with 500 sequential calls: 5% faster overall - Tests with populated `ctx` across 150 calls: 9% faster This optimization is particularly effective when `list_remove_by_value` is called frequently in data processing pipelines, as the 6% speedup compounds over many invocations. --- aerospike_helpers/operations/list_operations.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) mode change 100755 => 100644 aerospike_helpers/operations/list_operations.py diff --git a/aerospike_helpers/operations/list_operations.py b/aerospike_helpers/operations/list_operations.py old mode 100755 new mode 100644 index 238929bd4a..d44c1f05c7 --- a/aerospike_helpers/operations/list_operations.py +++ b/aerospike_helpers/operations/list_operations.py @@ -889,7 +889,17 @@ def list_remove_by_value(bin_name: str, value, return_type, inverted=False, ctx: A dictionary usable in :meth:`~aerospike.Client.operate` and :meth:`~aerospike.Client.operate_ordered`.The format of the dictionary should be considered an internal detail, and subject to change. """ - op_dict = { + if ctx: + return { + OP_KEY: aerospike.OP_LIST_REMOVE_BY_VALUE, + BIN_KEY: bin_name, + RETURN_TYPE_KEY: return_type, + VALUE_KEY: value, + INVERTED_KEY: inverted, + CTX_KEY: ctx, + } + + return { OP_KEY: aerospike.OP_LIST_REMOVE_BY_VALUE, BIN_KEY: bin_name, RETURN_TYPE_KEY: return_type, @@ -897,11 +907,6 @@ def list_remove_by_value(bin_name: str, value, return_type, inverted=False, ctx: INVERTED_KEY: inverted, } - if ctx: - op_dict[CTX_KEY] = ctx - - return op_dict - def list_remove_by_value_list(bin_name: str, value_list, return_type, inverted=False, ctx: Optional[list] = None): """Create a list remove by value list operation.