From 4644aa8eb7af428a35143ce6f810fd0fe9682764 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 04:05:52 +0000 Subject: [PATCH 1/2] Optimize _extract_hook_usages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves a **15% runtime improvement** by replacing character-by-character scanning with targeted string searches using Python's built-in `str.find()` method when matching parentheses in React hook calls. **Key Optimization:** Instead of iterating through every character to find matching parentheses: ```python # Original: char-by-char iteration while j < n: char = cs[j] if char == "(": bracket_depth += 1 elif char == ")": bracket_depth -= 1 j += 1 ``` The optimized version jumps directly between parentheses: ```python # Optimized: direct search for next ( or ) next_open = cs.find("(", j) next_close = cs.find(")", j) # Then jump to whichever comes first ``` **Why This Is Faster:** 1. **Reduced iterations**: Line profiler shows the inner while loop dropped from **40,809 hits** to **8,005 hits** (~80% reduction), directly correlating to the speedup 2. **Native string search**: Python's `str.find()` is implemented in C and optimized for substring searching, far faster than Python-level character comparisons 3. **Bigger jumps**: Instead of checking every character, the code skips directly to the next relevant delimiter **Performance Impact Based on Test Results:** The optimization particularly excels with: - **Large dependency arrays**: 414% faster on 200-dependency arrays (131μs → 25.7μs) - **Complex nested structures**: 40.5% faster with newline-heavy dependency arrays - **High hook counts**: 34.4% faster on 1000 hooks, 15% faster on realistic 200-hook components **Context from function_references:** The function is called in unit tests for React hook analysis, where components may contain dozens of hooks. Since React components with many `useEffect`, `useMemo`, and custom hooks are common in production codebases, this optimization directly benefits build-time static analysis tools that parse large component files. The 15% overall speedup becomes significant when analyzing hundreds of React components in a codebase. The optimization maintains identical behavior—same hook detection, same dependency counting logic—while simply reducing the number of character-level operations needed to find parenthesis boundaries. --- .../javascript/frameworks/react/context.py | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/codeflash/languages/javascript/frameworks/react/context.py b/codeflash/languages/javascript/frameworks/react/context.py index 83b927ed3..da9fb5a7c 100644 --- a/codeflash/languages/javascript/frameworks/react/context.py +++ b/codeflash/languages/javascript/frameworks/react/context.py @@ -131,14 +131,27 @@ def _extract_hook_usages(component_source: str) -> list[HookUsage]: j = match.end() # Scan by index to avoid allocating a new substring for the rest_of_line on each iteration while j < n: - char = cs[j] - if char == "(": + next_open = cs.find("(", j) + next_close = cs.find(")", j) + + # If there is no next closing parenthesis, abort scanning for this hook + if next_close == -1: + break + + # If next '(' occurs before next ')', it's an opening; otherwise it's a closing + if next_open != -1 and next_open < next_close: + # opening parenthesis bracket_depth += 1 - elif char == ")": + j = next_open + 1 + continue + else: + # closing parenthesis bracket_depth -= 1 + # position of this closing paren + kpos = next_close if bracket_depth == 0: # Find last non-space character before this closing paren - k = j - 1 + k = kpos - 1 while k >= match.end() and cs[k].isspace(): k -= 1 if k >= match.end() and cs[k] == "]": @@ -154,7 +167,9 @@ def _extract_hook_usages(component_source: str) -> list[HookUsage]: has_deps = True break - j += 1 + # continue scanning after this close + j = next_close + 1 + hooks.append(HookUsage(name=hook_name, has_dependency_array=has_deps, dependency_count=dep_count)) From 5a7f6bf79b057f45131fe8af7f9eaae3e4b559fa Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 04:08:06 +0000 Subject: [PATCH 2/2] style: auto-fix linting issues --- .../javascript/frameworks/react/context.py | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/codeflash/languages/javascript/frameworks/react/context.py b/codeflash/languages/javascript/frameworks/react/context.py index da9fb5a7c..c1f059825 100644 --- a/codeflash/languages/javascript/frameworks/react/context.py +++ b/codeflash/languages/javascript/frameworks/react/context.py @@ -144,32 +144,30 @@ def _extract_hook_usages(component_source: str) -> list[HookUsage]: bracket_depth += 1 j = next_open + 1 continue - else: - # closing parenthesis - bracket_depth -= 1 - # position of this closing paren - kpos = next_close - if bracket_depth == 0: - # Find last non-space character before this closing paren - k = kpos - 1 - while k >= match.end() and cs[k].isspace(): - k -= 1 - if k >= match.end() and cs[k] == "]": - has_deps = True - # Find the opening '[' for the dependency array within the search window - array_start = cs.rfind("[", match.end(), k + 1) - if array_start >= 0: - array_content = cs[array_start + 1 : k].strip() - if array_content: - dep_count = array_content.count(",") + 1 - else: - dep_count = 0 # empty deps [] - has_deps = True - break - - # continue scanning after this close - j = next_close + 1 + # closing parenthesis + bracket_depth -= 1 + # position of this closing paren + kpos = next_close + if bracket_depth == 0: + # Find last non-space character before this closing paren + k = kpos - 1 + while k >= match.end() and cs[k].isspace(): + k -= 1 + if k >= match.end() and cs[k] == "]": + has_deps = True + # Find the opening '[' for the dependency array within the search window + array_start = cs.rfind("[", match.end(), k + 1) + if array_start >= 0: + array_content = cs[array_start + 1 : k].strip() + if array_content: + dep_count = array_content.count(",") + 1 + else: + dep_count = 0 # empty deps [] + has_deps = True + break + # continue scanning after this close + j = next_close + 1 hooks.append(HookUsage(name=hook_name, has_dependency_array=has_deps, dependency_count=dep_count))