mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
Optimize _detect_export_style_cached
The hot-path replaced six separate regex scans (`finditer` calls on normalized source) with a single combined pattern that matches all export styles in one pass, eliminating the 38.8% overhead from whitespace normalization (`re.sub(r"\s+", " ", source_code)`) entirely. A fast-path substring check (`identifier not in source_code`) now short-circuits 2% of calls immediately, avoiding any regex work. The combined pattern uses three capture groups to distinguish default vs. named exports, trading slightly more complex group-extraction logic for an 82% reduction in regex engine invocations—line profiler confirms the total function time dropped from 16.6 ms to 6.2 ms. No accuracy regressions across test cases.
This commit is contained in:
parent
570f5171d2
commit
212f622c0d
1 changed files with 20 additions and 22 deletions
|
|
@ -37,6 +37,12 @@ from functools import lru_cache
|
|||
if TYPE_CHECKING:
|
||||
from aiservice.llm_models import LLM
|
||||
|
||||
_COMBINED_PATTERN = re.compile(
|
||||
r"(?:\bexport\s+default\s+(?:class|function)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b)"
|
||||
r"|(?:\bexport\s+(?:class|function|const)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b)"
|
||||
r"|(?:\bexport\s*\{[^}]*\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b[^{]*\})"
|
||||
)
|
||||
|
||||
_JS_RESERVED_WORDS = frozenset(
|
||||
{
|
||||
"module",
|
||||
|
|
@ -185,33 +191,25 @@ def _detect_export_style(source_code: str, identifier: str) -> str | None:
|
|||
"default" if exported as default, "named" if exported as named, None if not exported
|
||||
|
||||
"""
|
||||
# Normalize whitespace for easier pattern matching
|
||||
normalized = re.sub(r"\s+", " ", source_code)
|
||||
# Quick negative check to avoid expensive regex work when identifier is not present.
|
||||
if identifier not in source_code:
|
||||
return None
|
||||
|
||||
# Check for default export: export default class X or export default function X
|
||||
for match in _DEFAULT_CLASS_PATTERN.finditer(normalized):
|
||||
if match.group(1) == identifier:
|
||||
# Check for exports using a single combined regex to avoid multiple full-text scans.
|
||||
for match in _COMBINED_PATTERN.finditer(source_code):
|
||||
# Group 1 -> default export (class/function)
|
||||
g1 = match.group(1)
|
||||
if g1 is not None and g1 == identifier:
|
||||
return "default"
|
||||
|
||||
for match in _DEFAULT_FUNC_PATTERN.finditer(normalized):
|
||||
if match.group(1) == identifier:
|
||||
return "default"
|
||||
|
||||
# Check for named export: export class X, export function X, export const X, export { X }
|
||||
for match in _NAMED_CLASS_PATTERN.finditer(normalized):
|
||||
if match.group(1) == identifier:
|
||||
# Group 2 -> named export (class/function/const)
|
||||
g2 = match.group(2)
|
||||
if g2 is not None and g2 == identifier:
|
||||
return "named"
|
||||
|
||||
for match in _NAMED_FUNC_PATTERN.finditer(normalized):
|
||||
if match.group(1) == identifier:
|
||||
return "named"
|
||||
|
||||
for match in _NAMED_CONST_PATTERN.finditer(normalized):
|
||||
if match.group(1) == identifier:
|
||||
return "named"
|
||||
|
||||
for match in _NAMED_BRACES_PATTERN.finditer(normalized):
|
||||
if match.group(1) == identifier:
|
||||
# Group 3 -> named export via braces: export { X }
|
||||
g3 = match.group(3)
|
||||
if g3 is not None and g3 == identifier:
|
||||
return "named"
|
||||
|
||||
return None
|
||||
|
|
|
|||
Loading…
Reference in a new issue