From 5100a29acb9f5913349f3fe23f1bbe31aad0a2da Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 03:27:13 +0000 Subject: [PATCH 1/2] Optimize _extract_context_subscriptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimized code achieves a **33% runtime speedup** through two key changes: **1. Pre-compiled regex at module scope** - Moving `re.compile()` from inside the function to module-level initialization (`_CONTEXT_RE`) eliminates redundant pattern compilation on every function call. The line profiler shows this overhead was consuming 15.1% of the original runtime (602μs across 54 calls). This is particularly impactful when the function is called repeatedly. **2. Using `findall()` instead of list comprehension with `finditer()`** - Replacing `[match.group(1) for match in context_re.finditer(component_source)]` with `_CONTEXT_RE.findall(component_source)` leverages regex's built-in C implementation for direct group extraction. The original approach required: - Iterating over match objects in Python - Calling `.group(1)` on each match object - Building a list through comprehension This Python-level iteration consumed 83.4% of the original runtime. The `findall()` method performs the entire operation in optimized C code, returning the captured groups directly as a list. **Performance improvements across test cases:** - **Empty/small inputs see dramatic gains** (493-505% faster): The elimination of import and compilation overhead has outsized impact when actual matching is minimal - **Single matches improve 148-207% faster**: Pre-compilation and direct extraction both contribute significantly - **Multiple matches improve 83-186% faster**: Reduced per-match overhead from avoiding Python iteration - **Large-scale tests (1000 contexts) improve 39-71% faster**: Benefits compound with volume, though relative gains decrease as actual matching work dominates The optimization is universally beneficial across all test patterns - from single calls to large-scale parsing - making it valuable regardless of where this React context extraction function is called in the codebase. --- .../languages/javascript/frameworks/react/context.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/codeflash/languages/javascript/frameworks/react/context.py b/codeflash/languages/javascript/frameworks/react/context.py index 0d53e5c8b..5a15527a7 100644 --- a/codeflash/languages/javascript/frameworks/react/context.py +++ b/codeflash/languages/javascript/frameworks/react/context.py @@ -10,6 +10,7 @@ from dataclasses import dataclass, field from pathlib import Path from typing import TYPE_CHECKING +import re if TYPE_CHECKING: from tree_sitter import Node @@ -18,6 +19,8 @@ from codeflash.languages.javascript.frameworks.react.discovery import ReactComponentInfo from codeflash.languages.javascript.treesitter import TreeSitterAnalyzer +_CONTEXT_RE = re.compile(r"\buseContext\s*\(\s*(\w+)") + logger = logging.getLogger(__name__) @@ -167,10 +170,7 @@ def _extract_child_components(component_source: str, analyzer: TreeSitterAnalyze def _extract_context_subscriptions(component_source: str) -> list[str]: """Find React context subscriptions via useContext calls.""" - import re - - context_re = re.compile(r"\buseContext\s*\(\s*(\w+)") - return [match.group(1) for match in context_re.finditer(component_source)] + return _CONTEXT_RE.findall(component_source) def _find_type_definition(type_name: str, source: str, analyzer: TreeSitterAnalyzer) -> str | None: From 4bec0b929e05456f923274a7fa094ebcc895f85a Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 03:29:51 +0000 Subject: [PATCH 2/2] style: auto-fix linting issues --- codeflash/languages/javascript/frameworks/react/context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codeflash/languages/javascript/frameworks/react/context.py b/codeflash/languages/javascript/frameworks/react/context.py index 5a15527a7..8f30b863d 100644 --- a/codeflash/languages/javascript/frameworks/react/context.py +++ b/codeflash/languages/javascript/frameworks/react/context.py @@ -7,10 +7,10 @@ from __future__ import annotations import logging +import re from dataclasses import dataclass, field from pathlib import Path from typing import TYPE_CHECKING -import re if TYPE_CHECKING: from tree_sitter import Node