⚡️ Speed up function build_eval_function by 10%
#782
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.
📄 10% (0.10x) speedup for
build_eval_functionininference/core/workflows/core_steps/common/query_language/evaluation_engine/core.py⏱️ Runtime :
86.2 microseconds→78.1 microseconds(best of6runs)📝 Explanation and details
The optimized code achieves a 10% speedup through several key micro-optimizations that reduce function call overhead and attribute lookups:
Key optimizations:
Replaced
isinstance()with directtype()comparisons inbuild_eval_function: The original code usedisinstance(definition, BinaryStatement)which is more expensive thantype(definition) is BinaryStatement_type. This optimization assumes no subclassing is used, which is common in query language evaluation engines.Eliminated redundant loops with list comprehension for StatementGroup processing: Instead of explicitly creating an empty list and appending in a loop, the optimized version uses a list comprehension that's more efficient in Python.
Reduced repeated attribute lookups in
build_binary_statementandbuild_unary_statement: The original code calleddefinition.comparator.typeandtype(definition.comparator).model_fieldsmultiple times. The optimized version stores these in local variables (comparator,model_fields) to avoid repeated lookups.Minor optimization in
compound_eval: Eliminated an intermediate variable assignment by directly passingfun(values)tooperator_fun().Performance impact: The line profiler shows that
build_eval_functionexecution time dropped from 940.64μs to 821.41μs, with the most significant gains in the type checking logic (31.4% → 12.9% of total time for the first isinstance check).Context benefits: Since
build_eval_functionis called recursively for nested statement groups and is used in the mainevaluate()function that processes query language definitions, these micro-optimizations compound when processing complex nested queries. The function appears to be in a hot path for query language evaluation, making these small gains meaningful for overall system performance.Test case suitability: These optimizations are particularly effective for workloads with deeply nested statement groups or frequent query evaluations, where the recursive calls to
build_eval_functionamplify the performance benefits.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
workflows/unit_tests/core_steps/control_flow/test_control_flow.py::test_continue_if_evaluationTo edit these changes
git checkout codeflash/optimize-build_eval_function-miqkpu8hand push.