⚡️ Speed up method _BaseExpr.__mod__ by 22%
#99
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.
📄 22% (0.22x) speedup for
_BaseExpr.__mod__inaerospike_helpers/expressions/resources.py⏱️ Runtime :
20.6 microseconds→16.9 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 21% runtime improvement by inlining the
_create_operator_expressionfunction call directly into the_overload_opmethod. This eliminates function call overhead that was consuming 61.2% of the method's execution time in the original implementation.Key optimization:
Instead of calling
_create_operator_expression(l, r, op_type), the optimized version constructs the result expression inline:Why this is faster:
_create_operator_expressionused(*left_children, *right_children)which requires unpacking and repacking tuples. The optimized version uses simple tuple concatenationl + r.Test results show consistent improvements:
The optimization particularly benefits workloads that frequently construct operator expressions, which is common when building complex query filters or database expressions. All behavioral semantics are preserved - the same children tuples are created and the same expression tree structure results.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-_BaseExpr.__mod__-ml0hst3fand push.