diff --git a/codeflash/languages/java/instrumentation.py b/codeflash/languages/java/instrumentation.py index 18fdb1409..0bb2515a3 100644 --- a/codeflash/languages/java/instrumentation.py +++ b/codeflash/languages/java/instrumentation.py @@ -16,6 +16,7 @@ import logging import re +from bisect import bisect_right from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -230,10 +231,8 @@ def _collect_calls(node, wrapper_bytes, body_bytes, prefix_len, func_name, analy def _byte_to_line_index(byte_offset: int, line_byte_starts: list[int]) -> int: """Map a byte offset in body_text to a body_lines index.""" - for i in range(len(line_byte_starts) - 1, -1, -1): - if byte_offset >= line_byte_starts[i]: - return i - return 0 + idx = bisect_right(line_byte_starts, byte_offset) - 1 + return max(idx, 0) def _infer_array_cast_type(line: str) -> str | None: