⚡️ Speed up function get_analyzer_for_file by 45% in PR #1318 (fix/js-jest30-loop-runner)#1392
Open
codeflash-ai[bot] wants to merge 1 commit intofix/js-jest30-loop-runnerfrom
Conversation
The optimization achieves a **45% runtime improvement** (from 538μs to 370μs) by eliminating repeated `TreeSitterAnalyzer` object instantiation through **singleton pattern caching**. **Key optimization**: Instead of creating a new `TreeSitterAnalyzer` instance on every call to `get_analyzer_for_file()`, the optimized code pre-instantiates three singleton analyzers (`_TYPESCRIPT_ANALYZER`, `_TSX_ANALYZER`, `_JAVASCRIPT_ANALYZER`) at module load time and returns references to these cached instances. **Why this improves runtime**: 1. **Eliminates constructor overhead**: The original code calls `TreeSitterAnalyzer.__init__()` on every invocation (4,237 times in profiling), which involves `isinstance()` checks, attribute assignments, and object allocation. Line profiler shows `__init__` took 3.83ms total in the original vs just 6.9μs for the 3 singleton instances in the optimized version. 2. **Removes enum conversion**: The original creates `TreeSitterLanguage` enum values repeatedly. Pre-creating analyzers with enum values eliminates this redundant work. 3. **Reduces memory churn**: Fewer object allocations means less work for Python's memory allocator and garbage collector. **Impact on existing workloads**: Based on the `function_references`, this function is called extensively in JavaScript test discovery code paths (from `test_javascript_support.py` and `test_javascript_test_discovery.py`). The test files show it's called: - Once per test file being analyzed (20+ test cases shown) - In loops processing multiple test files - Within nested test discovery operations Since these are test discovery hot paths, the **45% speedup directly accelerates CI/CD pipelines** and developer workflows that scan JavaScript/TypeScript projects. **Test results validation**: All test cases show consistent speedups (40-66% faster), with particularly strong improvements for: - Batch processing scenarios (447μs → 308μs, 45% faster) - Repeated calls with same extension (50-66% faster on subsequent calls) - Large-scale consistency tests processing 500+ files The optimization maintains correctness by ensuring all callers receive valid analyzer instances with proper language configuration, just served from a reusable cache rather than created fresh each time.
3 tasks
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 #1318
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/js-jest30-loop-runner.📄 45% (0.45x) speedup for
get_analyzer_for_fileincodeflash/languages/treesitter_utils.py⏱️ Runtime :
538 microseconds→370 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 45% runtime improvement (from 538μs to 370μs) by eliminating repeated
TreeSitterAnalyzerobject instantiation through singleton pattern caching.Key optimization: Instead of creating a new
TreeSitterAnalyzerinstance on every call toget_analyzer_for_file(), the optimized code pre-instantiates three singleton analyzers (_TYPESCRIPT_ANALYZER,_TSX_ANALYZER,_JAVASCRIPT_ANALYZER) at module load time and returns references to these cached instances.Why this improves runtime:
Eliminates constructor overhead: The original code calls
TreeSitterAnalyzer.__init__()on every invocation (4,237 times in profiling), which involvesisinstance()checks, attribute assignments, and object allocation. Line profiler shows__init__took 3.83ms total in the original vs just 6.9μs for the 3 singleton instances in the optimized version.Removes enum conversion: The original creates
TreeSitterLanguageenum values repeatedly. Pre-creating analyzers with enum values eliminates this redundant work.Reduces memory churn: Fewer object allocations means less work for Python's memory allocator and garbage collector.
Impact on existing workloads:
Based on the
function_references, this function is called extensively in JavaScript test discovery code paths (fromtest_javascript_support.pyandtest_javascript_test_discovery.py). The test files show it's called:Since these are test discovery hot paths, the 45% speedup directly accelerates CI/CD pipelines and developer workflows that scan JavaScript/TypeScript projects.
Test results validation: All test cases show consistent speedups (40-66% faster), with particularly strong improvements for:
The optimization maintains correctness by ensuring all callers receive valid analyzer instances with proper language configuration, just served from a reusable cache rather than created fresh each time.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1318-2026-02-05T12.35.19and push.