⚡️ Speed up method _BaseExpr.__floordiv__ by 5%
#97
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.
📄 5% (0.05x) speedup for
_BaseExpr.__floordiv__inaerospike_helpers/expressions/resources.py⏱️ Runtime :
20.8 microseconds→19.8 microseconds(best of6runs)📝 Explanation and details
The optimized code achieves a 5% runtime improvement by eliminating an intermediate expression object creation in the
__floordiv__method.What Changed:
The original implementation called
__truediv__(which creates a DIV expression object), stored it indiv_expr, and then called__floor__()on that intermediate object. The optimized version directly chains_overload_op_va_args(right, _ExprOp.DIV)._overload_op_unary(_ExprOp.FLOOR), bypassing the temporary variable and method dispatch overhead.Why This Is Faster:
Eliminates intermediate object storage: The original code created a local variable
div_exprthat held the result of__truediv__. This required Python to manage an additional name binding and reference in the local scope.Removes extra method dispatch: By calling the internal helper methods directly instead of going through the public
__truediv__and__floor__methods, the optimized version saves two method lookup and dispatch operations.Reduces stack frame overhead: The chained call reduces the number of discrete operations Python must track in its execution stack.
Performance Characteristics:
The line profiler shows the optimized
__floordiv__takes 3.25ms total (11.7μs per call) versus 6.69ms (24.1μs per call) in the original—a 51% improvement in this method's execution time. Test results consistently show 3-7% speedups across various usage patterns:Impact:
This optimization is most beneficial when
__floordiv__is called frequently in expression building pipelines. Since Aerospike expression trees can be constructed with many chained operations, this micro-optimization compounds across multiple calls. The behavior remains identical—the same expression tree structure is produced, just more efficiently.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-_BaseExpr.__floordiv__-ml0hdmk8and push.