From 740a8db9bb5a326888f5f947c654d71e4f61fc35 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 14:23:00 +0000 Subject: [PATCH 1/2] Optimize _byte_to_line_index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves a **39% runtime improvement** through two key micro-optimizations that reduce per-call overhead in this frequently-executed helper function: ## Primary Optimizations 1. **Direct import binding**: Changed from `bisect.bisect_right()` to importing `bisect_right` directly as `_bisect_right`. This eliminates the attribute lookup (`bisect.`) on every function call, saving ~90-100ns per invocation as shown in the line profiler (977304ns → 887516ns for the bisect line). 2. **Conditional expression over max()**: Replaced `max(0, idx)` with `idx if idx > 0 else 0`. This avoids the overhead of calling the built-in `max()` function with tuple packing/unpacking, reducing this line's execution time by ~40% (622046ns → 379545ns per the profiler). ## Why This Matters The function maps byte offsets to line indices using binary search, a core operation that happens **2,158 times** in the profiled workload. These micro-optimizations compound significantly: - **Test results show consistent 30-70% speedups** across all cases, with the most dramatic improvements (60-70%) occurring in edge cases like empty lists or single elements where the overhead of `max()` represents a larger proportion of total execution time - **Large-scale tests** (1000-line files with multiple queries) still achieve 27-43% improvements, demonstrating the optimization scales well - The optimization is particularly effective for **hot-path scenarios** like sequential offset queries (42.6% faster) and dense line mapping operations The changes preserve all behavior including edge case handling (negative indices, empty lists) while delivering substantial performance gains through elimination of unnecessary Python-level function call overhead. --- codeflash/languages/java/instrumentation.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/codeflash/languages/java/instrumentation.py b/codeflash/languages/java/instrumentation.py index 6f2725b9b..fbfa5e3be 100644 --- a/codeflash/languages/java/instrumentation.py +++ b/codeflash/languages/java/instrumentation.py @@ -18,6 +18,7 @@ import logging import re from typing import TYPE_CHECKING +from bisect import bisect_right as _bisect_right if TYPE_CHECKING: from collections.abc import Sequence @@ -235,8 +236,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.""" - idx = bisect.bisect_right(line_byte_starts, byte_offset) - 1 - return max(0, idx) + idx = _bisect_right(line_byte_starts, byte_offset) - 1 + return idx if idx > 0 else 0 def _infer_array_cast_type(line: str) -> str | None: From 4fed31b3e377758cb642f00add6e29a5ee89560c Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 14:25:36 +0000 Subject: [PATCH 2/2] style: auto-fix linting issues --- codeflash/languages/java/instrumentation.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/codeflash/languages/java/instrumentation.py b/codeflash/languages/java/instrumentation.py index fbfa5e3be..4a87aad93 100644 --- a/codeflash/languages/java/instrumentation.py +++ b/codeflash/languages/java/instrumentation.py @@ -14,11 +14,10 @@ from __future__ import annotations -import bisect import logging import re -from typing import TYPE_CHECKING from bisect import bisect_right as _bisect_right +from typing import TYPE_CHECKING if TYPE_CHECKING: from collections.abc import Sequence @@ -237,7 +236,7 @@ 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.""" idx = _bisect_right(line_byte_starts, byte_offset) - 1 - return idx if idx > 0 else 0 + return max(0, idx) def _infer_array_cast_type(line: str) -> str | None: