From 2296ea591012cedd5e9c2ea628bbfa8e59baf6f4 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 24 Feb 2026 21:52:25 +0000 Subject: [PATCH] Optimize _is_jsx_component_usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Runtime improved from 874 μs to 478 μs (about 1.83× faster, ~82% relative speedup). The optimized version was accepted for this runtime improvement. What changed - Precompiled the render detection regex to a module-level compiled pattern (_RENDER_CALL_RE = re.compile(...)) so we don't recompile the same pattern on every call. - Added cheap substring checks ("render" not in code or "<" not in code or func_name not in code) to fast-fail obvious negatives before any regex work. - Kept the existing jsx regex (which must include func_name via re.escape) but now it is only executed when the cheap checks pass. Why this speeds things up - Regex compilation and execution are relatively expensive in Python. The original profiler shows two heavy costs: re.search(jsx_pattern, code) consumed ~78% of the time and re.search(r"\brender\s*\(", code) ~20%. Avoiding unnecessary regex calls produces the biggest wins. - The substring tests are O(n) scans using optimized C code (very cheap) and frequently rule out the need to run the heavier regexes. In negative/common cases (no render, no "<", or func_name absent) we return quickly with only a tiny C-level cost. - Precompiling the render regex removes repeated compilation overhead and makes the final render check slightly cheaper and more predictable. Evidence in profiling & tests - Total runtime halved in the benchmark (874 μs → 478 μs). - The optimized profiler shows the cheap substring check uses only a small fraction of time while the expensive jsx regex runs less frequently relative to overall runtime. - Tests that represent large inputs with many JSX-like tags but no render() call (the common pathological case) show the largest wins (e.g., big_no_render went from 226 μs → 9.74 μs). That demonstrates the early-exit check is extremely effective on large inputs. - Some small, positive cases (where both "<" and "render" are present and the JSX regex matches) saw tiny regressions because the extra substring checks add a marginal constant cost before the successful regex checks. Overall this trade-off is acceptable because it yields large wins on common/expensive negative cases and lowers average runtime. Behavioral impact and safety - The function’s semantics are unchanged: it still escapes func_name for the JSX check and still requires a render() call (the same word-boundary render pattern is used, but now via a compiled regex). - This optimization benefits workloads that call this function many times or process large source strings (hot-paths that analyze files or AST-less heuristic checks). In those scenarios the early-fail and compiled pattern greatly reduce CPU work per call. - If you expect many repeated calls with the same func_name and always-positive cases, a further micro-optimization could be to cache compiled jsx patterns per func_name — but that wasn't necessary to get the large runtime improvements seen here. Summary - Primary benefit: substantial runtime reduction (1.83× faster / ~82% speedup). - Key techniques: cheap substring fast-fail + module-level compiled regex. - Trade-offs: negligible extra cost for tiny positive cases, but large wins for common negative and large-input cases. This is a favorable trade-off for runtime-sensitive code paths. --- codeflash/languages/javascript/instrument.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/codeflash/languages/javascript/instrument.py b/codeflash/languages/javascript/instrument.py index 96ed550c6..b599a1fa1 100644 --- a/codeflash/languages/javascript/instrument.py +++ b/codeflash/languages/javascript/instrument.py @@ -17,6 +17,8 @@ from codeflash.code_utils.code_position import CodePosition from codeflash.discovery.functions_to_optimize import FunctionToOptimize +_RENDER_CALL_RE = re.compile(r"\brender\s*\(") + class TestingMode: """Testing mode constants.""" @@ -1192,10 +1194,13 @@ def _is_jsx_component_usage(code: str, func_name: str) -> bool: """ # Check for JSX usage: or jsx_pattern = rf"<\s*{re.escape(func_name)}[\s>/]" + # Fast-fail cheap substring checks to avoid regex work when possible. + if "render" not in code or "<" not in code or func_name not in code: + return False + # Use the precompiled render regex for correctness and efficiency. if not re.search(jsx_pattern, code): return False - # Also verify there's a render() call (from @testing-library/react or similar) - return bool(re.search(r"\brender\s*\(", code)) + return bool(_RENDER_CALL_RE.search(code)) def _is_function_used_in_test(code: str, func_name: str) -> bool: