⚡️ Speed up function get_op_result_name by 21%
#113
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.
📄 21% (0.21x) speedup for
get_op_result_nameinpandas/core/ops/common.py⏱️ Runtime :
2.69 milliseconds→2.22 milliseconds(best of83runs)📝 Explanation and details
The optimization achieves a 21% speedup through two key changes that reduce redundant operations:
1. Fast Path for Same-Type Objects in
get_op_result_name:Added
if type(right) is type(left) and hasattr(right, "name"):before the expensiveisinstancecheck. This optimization significantly benefits when both objects are the same pandas type (Series-Series or Index-Index operations), which is common in pandas operations. The test results show substantial gains for same-type scenarios (38-62% faster for large same-name tests).2. Reduced Attribute Access in
_maybe_match_name:Replaced multiple
hasattrcalls and direct attribute access with cachedgetattr(obj, "name", None). This eliminates redundant attribute lookups - the original code accessed.namemultiple times per object, while the optimized version caches the result. Also combinedTypeErrorandValueErrorexception handling since both are handled identically.Performance Characteristics:
leftandrightare both Series or both Index, the fast path avoids expensiveisinstancechecksThe optimization particularly shines in high-frequency scenarios like vectorized operations where the same pattern (same types, matching names) repeats thousands of times.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_op_result_name-mhchq3koand push.