⚡️ Speed up function _byte_to_line_index by 3,873% in PR #1199 (omni-java)#1589
Open
codeflash-ai[bot] wants to merge 2 commits intoomni-javafrom
Open
⚡️ Speed up function _byte_to_line_index by 3,873% in PR #1199 (omni-java)#1589codeflash-ai[bot] wants to merge 2 commits intoomni-javafrom
_byte_to_line_index by 3,873% in PR #1199 (omni-java)#1589codeflash-ai[bot] wants to merge 2 commits intoomni-javafrom
Conversation
The optimized code achieves a **3872% speedup** (from 29.2ms to 736μs) by replacing a manual reverse linear search with Python's built-in `bisect_right` function from the bisect module. **What changed:** - **Original approach**: Iterated backwards through `line_byte_starts` using a Python for-loop, comparing `byte_offset` against each element until finding the first match - **Optimized approach**: Uses `bisect_right(line_byte_starts, byte_offset) - 1` to perform a binary search in O(log n) time instead of O(n) **Why this is faster:** 1. **Algorithm complexity**: Binary search (O(log n)) vs linear search (O(n)). For 1000 lines, this means ~10 comparisons instead of up to 1000 2. **C-level implementation**: `bisect_right` is implemented in C and highly optimized, eliminating Python interpreter overhead for the search loop 3. **Reduced memory access**: The line profiler shows the original code spent 57.5% of time on array indexing (`line_byte_starts[i]`) across many iterations. The optimized version performs far fewer array accesses **Performance characteristics from tests:** - **Small lists** (2-4 elements): ~50-130% faster - modest gains due to setup overhead - **Medium lists** (100-300 elements): ~200-500% faster - binary search advantage becomes clear - **Large lists** (1000 elements): ~3000-6400% faster - dramatic improvement as the gap between O(log n) and O(n) widens - The test `test_large_scale_sequential_mapping` with 1000 lines shows **4495% speedup** (13.5ms → 293μs), confirming the optimization's effectiveness at scale **Edge cases preserved:** - Empty lists correctly return 0 - Negative offsets work correctly - Offsets before the first element return 0 - The conditional `if idx >= 0 else 0` handles the edge case where `bisect_right` returns 0 (offset before all elements) This optimization is particularly valuable when `_byte_to_line_index` is called repeatedly with large `line_byte_starts` lists, as is typical in code instrumentation scenarios where files have hundreds or thousands of lines.
Contributor
PR Review SummaryPrek Checks✅ Passed (after auto-fix) Two issues were auto-fixed and committed:
Mypy
Code Review✅ No critical issues found The optimization is correct and well-motivated:
Test Coverage
Last updated: 2026-02-20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
⚡️ This pull request contains optimizations for PR #1199
If you approve this dependent PR, these changes will be merged into the original PR branch
omni-java.📄 3,873% (38.73x) speedup for
_byte_to_line_indexincodeflash/languages/java/instrumentation.py⏱️ Runtime :
29.2 milliseconds→736 microseconds(best of237runs)📝 Explanation and details
The optimized code achieves a 3872% speedup (from 29.2ms to 736μs) by replacing a manual reverse linear search with Python's built-in
bisect_rightfunction from the bisect module.What changed:
line_byte_startsusing a Python for-loop, comparingbyte_offsetagainst each element until finding the first matchbisect_right(line_byte_starts, byte_offset) - 1to perform a binary search in O(log n) time instead of O(n)Why this is faster:
bisect_rightis implemented in C and highly optimized, eliminating Python interpreter overhead for the search loopline_byte_starts[i]) across many iterations. The optimized version performs far fewer array accessesPerformance characteristics from tests:
test_large_scale_sequential_mappingwith 1000 lines shows 4495% speedup (13.5ms → 293μs), confirming the optimization's effectiveness at scaleEdge cases preserved:
if idx >= 0 else 0handles the edge case wherebisect_rightreturns 0 (offset before all elements)This optimization is particularly valuable when
_byte_to_line_indexis called repeatedly with largeline_byte_startslists, as is typical in code instrumentation scenarios where files have hundreds or thousands of lines.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-20T07.41.07and push.