From aa80c6f9a4964c2cbf19b00a541941c07610f8a6 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 13:20:32 +0000 Subject: [PATCH] Optimize _compute_wrapped_segment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This optimization achieves a **43% runtime improvement** (from 1.20ms to 835μs) through two key changes that eliminate Python overhead: ## Primary Optimization: Explicit Loop in `_contains_jsx` The original used `any(_contains_jsx(child) for child in node.children)`, which creates a generator object on every call. The optimized version replaces this with an explicit `for` loop that can short-circuit immediately upon finding JSX: ```python for child in node.children: if _contains_jsx(child): return True return False ``` This eliminates generator allocation overhead and enables early termination. The line profiler shows the impact: the original's generator expression took **1.04μs** (73.1% of function time), while the optimized explicit loop takes only **0.39μs** total for iteration and checks (40.6% + 0.7%). This is especially effective for the common case where JSX is found early in the children list. ## Secondary Optimization: Loop Consolidation in `_compute_wrapped_segment` The original code iterated over `return_node.children` twice - once to find JSX and again to check for parenthesized expressions. The optimized version merges these into a single pass by checking `child.type == "parenthesized_expression"` immediately when JSX is found, before breaking from the loop. This eliminates the redundant iteration that processed ~1395 children in the second loop (shown in the original profiler). The optimization is particularly effective for test cases with many children - the large performance test shows **104% speedup** (117μs → 57.7μs) and the parenthesized test with 200+ children shows **106% speedup** (77.1μs → 37.4μs). ## Runtime Impact by Test Case - Simple JSX: 6% faster (baseline benefit) - Self-closing JSX with whitespace: 36.5% faster (generator elimination effect) - Parenthesized expression: 58% faster (both optimizations) - Nested JSX detection: 55.7% faster (recursive generator elimination) - Large workloads (300+ children): 104-106% faster (loop consolidation dominates) The optimizations are most impactful when processing complex React component trees with many children or deeply nested JSX structures, which are common in real-world React codebases. --- .../javascript/frameworks/react/profiler.py | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/codeflash/languages/javascript/frameworks/react/profiler.py b/codeflash/languages/javascript/frameworks/react/profiler.py index 0723b139f..66786ae4c 100644 --- a/codeflash/languages/javascript/frameworks/react/profiler.py +++ b/codeflash/languages/javascript/frameworks/react/profiler.py @@ -193,7 +193,10 @@ def _contains_jsx(node: Node) -> bool: """Check if a tree-sitter node contains JSX elements.""" if node.type in ("jsx_element", "jsx_self_closing_element", "jsx_fragment"): return True - return any(_contains_jsx(child) for child in node.children) + for child in node.children: + if _contains_jsx(child): + return True + return False def _wrap_return_with_profiler(source: str, return_node: Node, profiler_id: str, safe_name: str) -> str: @@ -306,26 +309,19 @@ def _compute_wrapped_segment( jsx_end = child.start_byte continue if _contains_jsx(child): - jsx_start = child.start_byte - jsx_end = child.end_byte + if child.type == "parenthesized_expression": + jsx_start = child.start_byte + 1 + jsx_end = child.end_byte - 1 + else: + jsx_start = child.start_byte + jsx_end = child.end_byte break if jsx_start is None: return None - # Default jsx_content from bytes slice jsx_content = source_bytes[jsx_start:jsx_end].decode("utf-8").strip() - # Check if the return uses parentheses: return (...) - # If so, we need to wrap inside the parens - for child in return_node.children: - if child.type == "parenthesized_expression": - # skip outer parentheses - jsx_start = child.start_byte + 1 # skip ( - jsx_end = child.end_byte - 1 # skip ) - jsx_content = source_bytes[jsx_start:jsx_end].decode("utf-8").strip() - break - wrapped = ( f'' f"\n{jsx_content}\n"