⚡️ Speed up function create_operand_builder by 129%
#784
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.
📄 129% (1.29x) speedup for
create_operand_builderininference/core/workflows/core_steps/common/query_language/evaluation_engine/core.py⏱️ Runtime :
67.7 microseconds→29.6 microseconds(best of16runs)📝 Explanation and details
The optimization achieves a 128% speedup by eliminating redundant work and streamlining the type checking logic in the frequently-called
create_operand_builderfunction.Key optimizations:
Moved imports to module level: The original code performed local imports of
build_operations_chain,static_operand_builder, anddynamic_operand_builderinside each function call. Moving these to the top eliminates repeated import overhead on every invocation.Replaced
isinstance()with direct type comparison: Changed fromisinstance(definition, StaticOperand)totype(definition) is StaticOperand. This is faster becauseisinstance()has to handle inheritance chains and protocol checking, whileisperforms a simple identity comparison.Consolidated logic: Instead of dispatching to separate
create_static_operand_builderandcreate_dynamic_operand_builderfunctions, the optimized version inlines both paths directly increate_operand_builder. This eliminates function call overhead and duplicatedbuild_operations_chaincalls.Performance impact: Based on the function references,
create_operand_builderis called in hot paths withinbuild_binary_statementandbuild_unary_statementfunctions that construct evaluation chains. Since these functions are likely called frequently during query language evaluation, the 128% improvement translates to meaningful performance gains in real workloads.Test case benefits: The annotated tests show consistent 108-150% speedups across various scenarios, with particularly strong performance on large input dictionaries and edge cases like empty operand names, demonstrating the optimization's broad effectiveness regardless of operand complexity.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-create_operand_builder-miql7bxjand push.