⚡️ Speed up function _byte_to_line_index by 41% in PR #1580 (fix/java-direct-jvm-and-bugs)#1586
Conversation
The main optimization here is eliminating the `max(0, idx)` call by handling the edge case directly. Since `bisect_right` returns 0 when `byte_offset` is less than all elements, subtracting 1 gives -1, which we can catch with a simple comparison. This avoids the function call overhead of `max()`.
| """Map a byte offset in body_text to a body_lines index.""" | ||
| idx = bisect.bisect_right(line_byte_starts, byte_offset) - 1 | ||
| return max(0, idx) | ||
| return max(idx, 0) |
There was a problem hiding this comment.
Note: The optimization in this PR (0 if idx < 0 else idx instead of max(0, idx)) was reverted by prek's FURB136 lint rule back to max(idx, 0). This effectively nullifies the performance improvement this PR intended to provide, since max(idx, 0) is functionally equivalent to the original max(0, idx).
This PR no longer provides any meaningful optimization after the lint auto-fix.
PR Review SummaryPrek Checks
Mypy
Code ReviewCritical finding: Optimization nullified by lint rule The optimization in this PR changed No bugs, security issues, or breaking API changes found — the change is functionally equivalent to the original. Test Coverage
Last updated: 2026-02-20 |
1f6001f
into
fix/java-direct-jvm-and-bugs
⚡️ This pull request contains optimizations for PR #1580
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/java-direct-jvm-and-bugs.📄 41% (0.41x) speedup for
_byte_to_line_indexincodeflash/languages/java/instrumentation.py⏱️ Runtime :
584 microseconds→415 microseconds(best of165runs)📝 Explanation and details
The main optimization here is eliminating the
max(0, idx)call by handling the edge case directly. Sincebisect_rightreturns 0 whenbyte_offsetis less than all elements, subtracting 1 gives -1, which we can catch with a simple comparison. This avoids the function call overhead ofmax().✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1580-2026-02-20T06.34.48and push.