⚡️ Speed up method JavaScriptSupport._extract_types_from_definition by 3,979% in PR #1561 (add/support_react)#1604
Merged
claude[bot] merged 2 commits intoFeb 20, 2026
Conversation
This optimization achieves a dramatic **~40x runtime improvement** (3979% speedup, from 11.4ms to 279μs) through two key changes: **1. Parser Instance Caching (Primary Optimization)** The original code accessed `self.parser` without any definition, likely triggering expensive parser initialization on every call. The optimized version adds a lazy-loading `@property` that creates and caches the parser instance once, then reuses it across all subsequent parse operations. This eliminates redundant parser/language initialization overhead, which the line profiler shows was consuming significant time in `analyzer.parse()` calls. **2. Frozenset for Primitive Type Lookups (Secondary Optimization)** Moved the primitive types tuple into a module-level `frozenset` constant (`_PRIMITIVE_TYPES`). While both tuples and frozensets provide O(1) membership testing via hashing, frozensets have slightly better performance characteristics for repeated lookups because they're explicitly designed for membership testing and have optimized hash table implementations. **Impact on Test Cases:** - The large-scale test with 1000 tokens shows the most dramatic improvement (324μs → 254μs, 27.4% faster), demonstrating how parser caching eliminates repeated initialization overhead - Smaller tests show modest improvements (1-16% faster) or remain similar, as the parser caching benefit is amortized over fewer operations - Empty/single-type tests are effectively unchanged, since they perform minimal work regardless **Why This Works:** The parser initialization in tree-sitter is expensive—it involves loading language grammars and setting up parsing state. By caching this once per `TreeSitterAnalyzer` instance rather than recreating it implicitly on every parse, we eliminate this repeated overhead. Combined with the optimized frozenset lookup for the inner recursive walk, these changes substantially reduce both setup and per-node costs.
Contributor
PR Review SummaryPrek Checks✅ All checks passing after fixes. Fixed issues:
Pre-existing mypy issues (not introduced by this PR):
Code Review✅ No critical issues found. This is a clean optimization PR with two changes:
The Test Coverage
Analysis:
Last updated: 2026-02-20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #1561
If you approve this dependent PR, these changes will be merged into the original PR branch
add/support_react.📄 3,979% (39.79x) speedup for
JavaScriptSupport._extract_types_from_definitionincodeflash/languages/javascript/support.py⏱️ Runtime :
11.4 milliseconds→279 microseconds(best of5runs)📝 Explanation and details
This optimization achieves a dramatic ~40x runtime improvement (3979% speedup, from 11.4ms to 279μs) through two key changes:
1. Parser Instance Caching (Primary Optimization)
The original code accessed
self.parserwithout any definition, likely triggering expensive parser initialization on every call. The optimized version adds a lazy-loading@propertythat creates and caches the parser instance once, then reuses it across all subsequent parse operations. This eliminates redundant parser/language initialization overhead, which the line profiler shows was consuming significant time inanalyzer.parse()calls.2. Frozenset for Primitive Type Lookups (Secondary Optimization)
Moved the primitive types tuple into a module-level
frozensetconstant (_PRIMITIVE_TYPES). While both tuples and frozensets provide O(1) membership testing via hashing, frozensets have slightly better performance characteristics for repeated lookups because they're explicitly designed for membership testing and have optimized hash table implementations.Impact on Test Cases:
Why This Works:
The parser initialization in tree-sitter is expensive—it involves loading language grammars and setting up parsing state. By caching this once per
TreeSitterAnalyzerinstance rather than recreating it implicitly on every parse, we eliminate this repeated overhead. Combined with the optimized frozenset lookup for the inner recursive walk, these changes substantially reduce both setup and per-node costs.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1561-2026-02-20T12.38.47and push.