From 0578da79904f9d31390ac4c78772624ad72d792d 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:03:21 +0000 Subject: [PATCH] Optimize list_get_by_index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves an **8% runtime improvement** by eliminating conditional dictionary mutation in favor of separate return paths based on the `ctx` parameter. **Key Optimization:** In the original code, a dictionary is always created with 4 keys, then conditionally mutated to add a 5th key (`CTX_KEY`) when `ctx` is truthy: ```python op_dict = {OP_KEY: ..., BIN_KEY: ..., RETURN_TYPE_KEY: ..., INDEX_KEY: ...} if ctx: op_dict[CTX_KEY] = ctx return op_dict ``` The optimized version creates the dictionary once with the exact keys needed, avoiding the mutation step: ```python if ctx: return {OP_KEY: ..., BIN_KEY: ..., RETURN_TYPE_KEY: ..., INDEX_KEY: ..., CTX_KEY: ctx} return {OP_KEY: ..., BIN_KEY: ..., RETURN_TYPE_KEY: ..., INDEX_KEY: ...} ``` **Why This is Faster:** 1. **Eliminates dictionary mutation overhead**: The original code incurs the cost of `op_dict[CTX_KEY] = ctx` as a separate dictionary insertion operation, which requires rehashing and potential dictionary resizing checks. 2. **Predictable dictionary size**: Python can optimize dictionary creation when the final size is known upfront (literal dictionary construction), as opposed to incremental growth. 3. **Reduced bytecode operations**: The optimized version has fewer bytecode instructions—no intermediate variable assignment, no post-creation mutation. **Performance Characteristics:** Looking at the annotated tests, the optimization shows consistent improvements: - **With ctx provided** (minority case, ~2% of calls): 8-18% faster (e.g., 2.79μs → 2.36μs) - **Without ctx** (majority case, ~98% of calls): 2-18% faster (e.g., 2.43μs → 2.17μs) The speedup is most pronounced when `ctx` is provided because the original code performed both dictionary creation AND mutation, while the optimized version only performs creation. For the no-ctx case, the benefit comes from avoiding the intermediate variable and having a more direct return path. This optimization is particularly effective for this function since it appears to be a lightweight operation builder, likely called frequently when constructing Aerospike database operations. The 8% aggregate improvement scales well across all test scenarios. --- aerospike_helpers/operations/list_operations.py | 16 ++++++++++------ 1 file changed, 10 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..ef3ff78a85 --- a/aerospike_helpers/operations/list_operations.py +++ b/aerospike_helpers/operations/list_operations.py @@ -489,18 +489,22 @@ def list_get_by_index(bin_name: str, index, return_type, ctx: Optional[list] = N 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_GET_BY_INDEX, + BIN_KEY: bin_name, + RETURN_TYPE_KEY: return_type, + INDEX_KEY: index, + CTX_KEY: ctx, + } + + return { OP_KEY: aerospike.OP_LIST_GET_BY_INDEX, BIN_KEY: bin_name, RETURN_TYPE_KEY: return_type, INDEX_KEY: index, } - if ctx: - op_dict[CTX_KEY] = ctx - - return op_dict - def list_get_by_index_range(bin_name: str, index, return_type, count=None, inverted=False, ctx: Optional[list] = None): """Create a list get index range operation.