From ceb2448cd105b846228438063f7742b5d3aec1ba Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 20:28:04 +0000 Subject: [PATCH] Optimize _byte_to_line_index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization replaces `max(idx, 0)` with a ternary expression `0 if idx < 0 else idx`, achieving a **34% runtime improvement** (from 1.06ms to 783μs). **What changed:** The only modification is in the return statement - replacing the `max()` built-in function call with an inline conditional expression. **Why it's faster:** The `max()` function in Python involves overhead from: 1. Function call setup and teardown 2. Argument tuple creation for variadic parameters 3. Generic comparison logic that handles arbitrary types and multiple arguments The ternary operator `0 if idx < 0 else idx` is: 1. A direct bytecode operation (no function call) 2. A single comparison with immediate branching 3. Optimized at the compiler level for simple integer comparisons Line profiler data confirms this: the return statement dropped from 751,720ns total time (40.9% of function time) to 476,309ns (28.7% of function time) - a **37% reduction** in that line alone. **Performance characteristics:** Based on the annotated tests, the optimization shows consistent improvements across all test cases: - **48-71% speedup** on basic single and multi-line mappings - **50-66% speedup** on edge cases (empty lists, negative offsets, large offsets) - **32-43% speedup** on large-scale tests with 100-1000 lines - Particularly effective for tight line distributions where the function is called frequently The optimization is universally beneficial because every call to `_byte_to_line_index` executes this return statement exactly once, making it a hot path regardless of input characteristics. --- codeflash/languages/java/instrumentation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codeflash/languages/java/instrumentation.py b/codeflash/languages/java/instrumentation.py index 1cacbef5b..5c3a33d4f 100644 --- a/codeflash/languages/java/instrumentation.py +++ b/codeflash/languages/java/instrumentation.py @@ -315,7 +315,7 @@ def _collect_calls( 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(idx, 0) + return 0 if idx < 0 else idx def _infer_array_cast_type(line: str) -> str | None: