Skip to content

Commit 1a0ab57

Browse files
Optimize _get_parent_type_name
The optimized code achieves a **12% runtime improvement** by replacing the inline tuple `("ClassDef", "InterfaceDef", "EnumDef")` with a module-level `frozenset` constant `_PARENT_TYPE_NAMES`. **What changed:** - A `frozenset` containing the three parent type names is created once at module load time - The membership test `parent.type in _PARENT_TYPE_NAMES` now uses the frozenset instead of creating a tuple on each check **Why this is faster:** The key performance gain comes from two factors: 1. **Constant instantiation overhead eliminated**: The original code creates a new tuple object every time the membership check executes (513 hits in the profile). The optimized version creates the frozenset only once at module load. 2. **O(1) hash-based lookup**: While the difference is marginal for just 3 elements, `frozenset` uses hash-based membership testing (O(1) average case) versus tuple's linear scan (O(n)). This provides a small but measurable speedup per check. **Performance characteristics:** The line profiler shows the critical loop line (checking `parent.type in ...`) executes 513 times and accounts for ~51% of total runtime. Even small per-iteration improvements here compound significantly. The test results confirm this: - **Large-scale benefit**: The `test_large_scale_parents_last_element_matches` test shows a dramatic **27.2% speedup** (27.6μs → 21.7μs) when iterating through 500 parents, demonstrating the optimization scales well with larger parent lists - **Small overhead on fast paths**: Tests with early returns or no parent iteration show minor slowdowns (3-13%), likely due to cache effects or measurement noise on nanosecond-scale operations - **Overall win**: The aggregate 12% speedup indicates the optimization benefits the typical usage pattern where multiple parents are checked This optimization is particularly valuable if `_get_parent_type_name` is called frequently during Java code analysis, as the savings multiply across many invocations.
1 parent c1128eb commit 1a0ab57

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

codeflash/languages/java/context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
if TYPE_CHECKING:
2121
from tree_sitter import Node
2222

23+
_PARENT_TYPE_NAMES: frozenset[str] = frozenset(("ClassDef", "InterfaceDef", "EnumDef"))
24+
2325
logger = logging.getLogger(__name__)
2426

2527

@@ -138,7 +140,7 @@ def _get_parent_type_name(function: FunctionToOptimize) -> str | None:
138140
# Check parents for interface/enum
139141
if function.parents:
140142
for parent in function.parents:
141-
if parent.type in ("ClassDef", "InterfaceDef", "EnumDef"):
143+
if parent.type in _PARENT_TYPE_NAMES:
142144
return parent.name
143145

144146
return None

0 commit comments

Comments
 (0)