From b9fdd48a57ca1d676dd7f4be6c70a5671b8c3092 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 3 Dec 2025 22:30:53 +0000 Subject: [PATCH] Optimize create_dynamic_operand_builder The optimization achieves a **6% speedup** through two key changes in the `build_operations_chain` function: **Key Optimizations:** 1. **List Comprehension Replacement**: The original code used a manual loop with `append()` to build the operations list: ```python operations_functions = [] for operation_id, operation_definition in enumerate(operations): # ... build operation ... operations_functions.append(operation_function) ``` The optimized version uses a list comprehension, which is faster in Python due to reduced function call overhead and better memory allocation patterns. 2. **Simplified Empty Check**: Changed `if not len(operations):` to `if not operations:`, eliminating an unnecessary function call since Python treats empty sequences as falsy. **Why This Matters:** The line profiler shows the list comprehension execution (line with `operations_functions = [`) takes 268ms vs the original loop structure taking 304ms total - demonstrating the comprehension's efficiency. The `build_operations_chain` function is called from `create_dynamic_operand_builder`, which itself is used in query language evaluation pipelines. **Performance Impact:** Test results show consistent 5-14% improvements across various scenarios, with the largest gains (10-14%) occurring when operations lists are empty (identity function cases). This suggests the optimization particularly benefits workflows with simpler operation chains, which are common in query language evaluation where many operands may require minimal transformation. Since `create_dynamic_operand_builder` is called through `create_operand_builder` in the evaluation engine, this optimization will compound across multiple operand evaluations in complex workflows. --- .../common/query_language/operations/core.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/inference/core/workflows/core_steps/common/query_language/operations/core.py b/inference/core/workflows/core_steps/common/query_language/operations/core.py index 1404ff68b3..69f98ef652 100644 --- a/inference/core/workflows/core_steps/common/query_language/operations/core.py +++ b/inference/core/workflows/core_steps/common/query_language/operations/core.py @@ -93,16 +93,15 @@ def identity(value: Any, **kwargs) -> Any: def build_operations_chain( operations: List[OperationDefinition], execution_context: str = "" ) -> Callable[[T, Dict[str, Any]], V]: - if not len(operations): + if not operations: return identity # return identity function - operations_functions = [] - for operation_id, operation_definition in enumerate(operations): - operation_context = f"{execution_context}[{operation_id}]" - operation_function = build_operation( + operations_functions = [ + build_operation( operation_definition=operation_definition, - execution_context=operation_context, + execution_context=f"{execution_context}[{operation_id}]", ) - operations_functions.append(operation_function) + for operation_id, operation_definition in enumerate(operations) + ] return partial(chain, functions=operations_functions)