From c10165efe713a400de91bdeb24cb77c3ac4c1e30 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:13:14 +0000 Subject: [PATCH] Optimize _extract_context_subscriptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization replaces `finditer()` with `findall()` for regex matching, achieving a **29% speedup** in runtime (from 1.32ms to 1.02ms). **What Changed:** Instead of iterating over match objects and calling `.group(1)` on each (`[match.group(1) for match in _CONTEXT_RE.finditer(component_source)]`), the code now uses `_CONTEXT_RE.findall(component_source)` to directly extract captured groups. **Why It's Faster:** 1. **Eliminates intermediate objects**: `finditer()` returns an iterator of match objects, each requiring instantiation and a method call to `group(1)`. `findall()` with a capturing group directly returns the captured strings as a list. 2. **Reduces overhead**: The list comprehension in the original code introduces iteration overhead and function call overhead for each match. `findall()` is implemented in C within Python's regex engine, making it significantly more efficient. 3. **Memory efficiency**: No intermediate match objects need to be created, reducing allocations and garbage collection pressure. **Performance Impact:** The test results show consistent speedups across all cases: - Simple cases (single context): **89-98% faster** (4.28μs → 2.26μs) - Multiple contexts: **52-67% faster** - Large-scale tests (1000 contexts): **37-39% faster** (454μs → 330μs) - Edge cases (empty strings): **206% faster** (1.65μs → 541ns) **Workload Benefits:** Based on the function references, `_extract_context_subscriptions` is called during React component analysis for detecting context dependencies. While not in a tight loop, this function processes potentially large source files (test shows 1500+ line files). The optimization particularly benefits: - Codebases with many context subscriptions (the 100-context test shows 39% speedup) - Large source files (1000+ line files show 12-37% improvement) - Batch analysis of multiple components The optimization is universally beneficial across all input patterns with no regressions, making it a pure performance win. --- 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 83b927ed3..84d56e6a3 100644 --- a/codeflash/languages/javascript/frameworks/react/context.py +++ b/codeflash/languages/javascript/frameworks/react/context.py @@ -174,7 +174,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.""" - 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: