Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion codeflash/code_utils/code_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ast
import time
from dataclasses import dataclass
from functools import lru_cache
from importlib.util import find_spec
from itertools import chain
from pathlib import Path
Expand Down Expand Up @@ -513,7 +514,7 @@ def visit_Try(self, node: cst.Try) -> None:

def extract_global_statements(source_code: str) -> tuple[cst.Module, list[cst.SimpleStatementLine]]:
"""Extract global statements from source code."""
module = cst.parse_module(source_code)
module = _parse_module_cached(source_code)
collector = GlobalStatementCollector()
module.visit(collector)
return module, collector.global_statements
Expand Down Expand Up @@ -1581,3 +1582,15 @@ def get_opt_review_metrics(
end_time = time.perf_counter()
logger.debug(f"Got function references in {end_time - start_time:.2f} seconds")
return calling_fns_details


@lru_cache(maxsize=128)
def _parse_module_cached(source_code: str) -> cst.Module:
"""Cache parsed modules to avoid re-parsing the same source code."""
return cst.parse_module(source_code)


@lru_cache(maxsize=128)
def _parse_module_cached(source_code: str) -> cst.Module:
"""Cache parsed modules to avoid re-parsing the same source code."""
return cst.parse_module(source_code)
Loading