Skip to content

Commit 020ae70

Browse files
authored
Merge pull request #1587 from codeflash-ai/codeflash/optimize-pr1199-2026-02-20T06.40.01
⚡️ Speed up function `_should_include_method` by 22% in PR #1199 (`omni-java`)
2 parents 14961b0 + 94a773a commit 020ae70

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

codeflash/languages/base.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from __future__ import annotations
99

10+
import fnmatch
11+
import re
1012
from dataclasses import dataclass, field
1113
from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable
1214

@@ -172,6 +174,23 @@ class FunctionFilterCriteria:
172174
min_lines: int | None = None
173175
max_lines: int | None = None
174176

177+
def __post_init__(self) -> None:
178+
"""Pre-compile regex patterns from glob patterns for faster matching."""
179+
self._include_regexes = [re.compile(fnmatch.translate(p)) for p in self.include_patterns]
180+
self._exclude_regexes = [re.compile(fnmatch.translate(p)) for p in self.exclude_patterns]
181+
182+
def matches_include_patterns(self, name: str) -> bool:
183+
"""Check if name matches any include pattern."""
184+
if not self._include_regexes:
185+
return True
186+
return any(regex.match(name) for regex in self._include_regexes)
187+
188+
def matches_exclude_patterns(self, name: str) -> bool:
189+
"""Check if name matches any exclude pattern."""
190+
if not self._exclude_regexes:
191+
return False
192+
return any(regex.match(name) for regex in self._exclude_regexes)
193+
175194

176195
@dataclass
177196
class ReferenceInfo:

codeflash/languages/java/discovery.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from codeflash.models.function_types import FunctionParent
1717

1818
if TYPE_CHECKING:
19+
from tree_sitter import Node
20+
1921
from codeflash.languages.java.parser import JavaAnalyzer, JavaMethodNode
2022

2123
logger = logging.getLogger(__name__)
@@ -136,18 +138,14 @@ def _should_include_method(
136138
return False
137139

138140
# Check include patterns
139-
if criteria.include_patterns:
140-
import fnmatch
141-
142-
if not any(fnmatch.fnmatch(method.name, pattern) for pattern in criteria.include_patterns):
143-
return False
141+
if not criteria.matches_include_patterns(method.name):
142+
return False
144143

145144
# Check exclude patterns
146-
if criteria.exclude_patterns:
147-
import fnmatch
145+
if criteria.matches_exclude_patterns(method.name):
146+
return False
148147

149-
if any(fnmatch.fnmatch(method.name, pattern) for pattern in criteria.exclude_patterns):
150-
return False
148+
# Check require_return - void methods don't have return values
151149

152150
# Check require_return - void methods don't have return values
153151
if criteria.require_return:
@@ -203,7 +201,7 @@ def discover_test_methods(file_path: Path, analyzer: JavaAnalyzer | None = None)
203201

204202

205203
def _walk_tree_for_test_methods(
206-
node,
204+
node: Node,
207205
source_bytes: bytes,
208206
file_path: Path,
209207
test_methods: list[FunctionToOptimize],

0 commit comments

Comments
 (0)