⚡️ Speed up function ror_ by 17%
#393
Open
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.
📄 17% (0.17x) speedup for
ror_inpandas/core/roperator.py⏱️ Runtime :
1.06 milliseconds→911 microseconds(best of220runs)📝 Explanation and details
The optimization replaces the function call
operator.or_(right, left)with the direct bitwise OR operatorright | left. This eliminates the overhead of calling theoperator.or_function, which involves:operator.or_requires additional stack operations and Python function dispatchoperator.or_involves a module attribute lookup each time the function is calledThe direct
|operator bypasses these overheads and translates directly to the underlying bitwise OR operation. The test results show consistent speedups across all scenarios:__or__/__ror__methodsThe optimization is particularly effective for this function since
ror_performs a simple reverse bitwise OR operation (right | leftinstead ofleft | right). The behavior is identical - both approaches delegate to the same underlying Python operator protocols (__or__and__ror__methods), but the direct operator syntax is more efficient.This micro-optimization is valuable because bitwise operations are fundamental and may be called frequently in data manipulation contexts, especially given this is part of pandas core functionality.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-ror_-mior3jguand push.