⚡️ Speed up function wrap_target_calls_with_treesitter by 81% in PR #1552 (fix/java-e2e-bugs)#1553
Merged
misrasaurabh1 merged 1 commit intofix/java-e2e-bugsfrom Feb 19, 2026
Conversation
The optimized code achieves an **80% speedup** (from 71.3ms to 39.5ms) through two focused algorithmic improvements:
## Primary Optimization: Binary Search for Line Index Lookup
The `_byte_to_line_index` function was the primary bottleneck, consuming 78% of the original runtime (572ms out of 733ms total profiled time). The optimization replaces a **linear O(n) reverse iteration** with **O(log n) binary search** using `bisect.bisect_right()`:
**Original approach (O(n)):**
```python
for i in range(len(line_byte_starts) - 1, -1, -1):
if byte_offset >= line_byte_starts[i]:
return i
```
**Optimized approach (O(log n)):**
```python
idx = bisect.bisect_right(line_byte_starts, byte_offset) - 1
return max(0, idx)
```
With 2,887 calls to this function and an average list size from the test cases, the binary search reduces the function's time from **572ms to 2.6ms** (99.5% reduction). This is particularly effective in the large-scale test cases like `test_large_scale_many_expression_statements` (149% faster) and `test_very_large_body_many_targets` (48.4% faster), where the number of calls and list sizes are substantial.
## Secondary Optimization: String Containment Check
The `_infer_array_cast_type` function optimization simplifies the assertion method detection from using `any()` with a generator to direct boolean checks:
**Original:**
```python
if not any(method in line for method in assertion_methods):
```
**Optimized:**
```python
if "assertArrayEquals" not in line and "assertArrayNotEquals" not in line:
```
This avoids tuple creation and iterator overhead, reducing function time by 75% (from 6.1ms to 1.6ms). While smaller in absolute terms, this contributes meaningfully when called 2,887 times per run.
## Impact Across Test Cases
The optimizations show **consistent improvements across all test cases**, with particularly strong gains in:
- **Large-scale scenarios**: Functions processing 500-1000+ method calls show 48-149% speedup
- **Realistic workloads**: Mixed expression tests show 15-16% improvements
- **Small inputs**: Even single-call tests benefit 1-5% from reduced overhead
The code path for `wrap_target_calls_with_treesitter` typically calls `_byte_to_line_index` once per method invocation found in the source, making the binary search optimization highly impactful for any non-trivial Java method body being instrumented.
Closed
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 #1552
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/java-e2e-bugs.📄 81% (0.81x) speedup for
wrap_target_calls_with_treesitterincodeflash/languages/java/instrumentation.py⏱️ Runtime :
71.3 milliseconds→39.5 milliseconds(best of49runs)📝 Explanation and details
The optimized code achieves an 80% speedup (from 71.3ms to 39.5ms) through two focused algorithmic improvements:
Primary Optimization: Binary Search for Line Index Lookup
The
_byte_to_line_indexfunction was the primary bottleneck, consuming 78% of the original runtime (572ms out of 733ms total profiled time). The optimization replaces a linear O(n) reverse iteration with O(log n) binary search usingbisect.bisect_right():Original approach (O(n)):
Optimized approach (O(log n)):
With 2,887 calls to this function and an average list size from the test cases, the binary search reduces the function's time from 572ms to 2.6ms (99.5% reduction). This is particularly effective in the large-scale test cases like
test_large_scale_many_expression_statements(149% faster) andtest_very_large_body_many_targets(48.4% faster), where the number of calls and list sizes are substantial.Secondary Optimization: String Containment Check
The
_infer_array_cast_typefunction optimization simplifies the assertion method detection from usingany()with a generator to direct boolean checks:Original:
Optimized:
This avoids tuple creation and iterator overhead, reducing function time by 75% (from 6.1ms to 1.6ms). While smaller in absolute terms, this contributes meaningfully when called 2,887 times per run.
Impact Across Test Cases
The optimizations show consistent improvements across all test cases, with particularly strong gains in:
The code path for
wrap_target_calls_with_treesittertypically calls_byte_to_line_indexonce per method invocation found in the source, making the binary search optimization highly impactful for any non-trivial Java method body being instrumented.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1552-2026-02-19T18.54.22and push.