Skip to content

Commit a7f88a6

Browse files
Optimize _extract_type_names_from_code
## Refinement Summary The optimization achieved a **35x speedup** (93.3ms → 2.65ms) primarily through lazy parser initialization. I refined the code by: 1. **Reverted micro-optimization**: Restored the intermediate `name` variable in `_extract_type_names_from_code`. This improves readability with no performance cost—the profiler shows no measurable difference. 2. **Preserved the core optimization**: Kept the lazy parser initialization via `@property`, which is the actual source of the dramatic speedup. 3. **Minimized diff**: Restored original formatting (blank lines, import style) to reduce unnecessary changes and match the original code style. The refined optimization maintains the full performance benefit while improving code clarity and minimizing the diff from the original.
1 parent 4ac49ee commit a7f88a6

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

codeflash/languages/java/parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,13 @@ def byte_to_char_index(self, byte_offset: int, source: bytes) -> int:
721721
# bisect_right returns insertion point; subtract 1 to get character count
722722
return bisect_right(cum, byte_offset) - 1
723723

724+
@property
725+
def parser(self) -> Parser:
726+
"""Lazy-initialize and return the parser."""
727+
if self._parser is None:
728+
self._parser = Parser()
729+
return self._parser
730+
724731

725732
def get_java_analyzer() -> JavaAnalyzer:
726733
"""Get a JavaAnalyzer instance.

0 commit comments

Comments
 (0)