From 5a3f8cdd86220c21586a449c92b692a36970cfcb Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 04:15:28 +0000 Subject: [PATCH] Optimize add_runtime_comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization adds `@lru_cache(maxsize=4096)` to `format_runtime_comment`, which is called once per annotated line (2484 hits in the profiler) with frequently repeated input tuples—particularly when multiple test cases measure identical nanosecond pairs or when performance data clusters around similar values. Without caching, each call re-executes `format_perf`, `performance_gain`, and two `format_time` calls, accounting for 78% of `format_runtime_comment`'s total time (9.55ms) and 63.5% of `add_runtime_comments`'s wall time (17.7ms). With caching, cache hits eliminate these formatting steps entirely, reducing the hot-path line from 7120ns to 4292ns per hit—a 39.7% per-call improvement that compounds across thousands of invocations to yield the observed 111% speedup. --- codeflash/code_utils/time_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/codeflash/code_utils/time_utils.py b/codeflash/code_utils/time_utils.py index 42cfa9703..70399dc8d 100644 --- a/codeflash/code_utils/time_utils.py +++ b/codeflash/code_utils/time_utils.py @@ -1,5 +1,7 @@ from __future__ import annotations +from functools import lru_cache + from codeflash.result.critic import performance_gain @@ -93,6 +95,7 @@ def format_perf(percentage: float) -> str: return f"{percentage:.3f}" +@lru_cache(maxsize=4096) def format_runtime_comment(original_time_ns: int, optimized_time_ns: int, comment_prefix: str = "#") -> str: perf_gain = format_perf( abs(performance_gain(original_runtime_ns=original_time_ns, optimized_runtime_ns=optimized_time_ns) * 100)