Skip to content

Commit 5447c83

Browse files
authored
Merge pull request #1659 from codeflash-ai/codeflash/optimize-pr1543-2026-02-25T06.07.49
⚡️ Speed up method `JavaLineProfiler.instrument_function` by 26% in PR #1543 (`fix/java/line-profiler`)
2 parents bde5185 + b69f4d5 commit 5447c83

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

codeflash/languages/java/line_profiler.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ def instrument_function(self, func: FunctionInfo, lines: list[str], file_path: P
353353
# Add profiling to each executable line
354354
function_entry_added = False
355355

356+
file_posix = file_path.as_posix()
357+
356358
for local_idx, line in enumerate(func_lines):
357359
local_line_num = local_idx + 1 # 1-indexed within function
358360
global_line_num = func.starting_line + local_idx # Global line number
@@ -377,23 +379,19 @@ def instrument_function(self, func: FunctionInfo, lines: list[str], file_path: P
377379
if (
378380
local_line_num in executable_lines
379381
and stripped
380-
and not stripped.startswith("//")
381-
and not stripped.startswith("/*")
382-
and not stripped.startswith("*")
382+
and not stripped.startswith(("//", "/*", "*"))
383383
and stripped not in ("}", "};")
384384
):
385385
# Get indentation
386386
indent = len(line) - len(line.lstrip())
387387
indent_str = " " * indent
388388

389389
# Store line content for profiler output
390-
content_key = f"{file_path.as_posix()}:{global_line_num}"
390+
content_key = f"{file_posix}:{global_line_num}"
391391
self.line_contents[content_key] = stripped
392392

393393
# Add hit() call before the line
394-
profiled_line = (
395-
f'{indent_str}{self.profiler_class}.hit("{file_path.as_posix()}", {global_line_num});\n{line}'
396-
)
394+
profiled_line = f'{indent_str}{self.profiler_class}.hit("{file_posix}", {global_line_num});\n{line}'
397395
instrumented_lines.append(profiled_line)
398396
else:
399397
instrumented_lines.append(line)

0 commit comments

Comments
 (0)