⚡️ Speed up method JavaScriptSupport.normalize_code by 114% in PR #1780 (fix/normalizer)#1782
Conversation
The optimization moves the `normalize_js_code` import from inside the method to module-level and wraps it with a 128-entry LRU cache (`functools.lru_cache`), achieving a 114% speedup by eliminating repeated parsing and normalization of duplicate code snippets. Line profiler shows the original spent 96.9% of wall time calling `normalize_js_code` (which internally spends ~74% on `normalize_tree` and AST traversal); caching this result avoids re-parsing identical source strings, common during deduplication workflows. The smaller cache size (128 vs. 1024) balances memory usage with hit-rate for typical workloads where a modest number of distinct snippets are repeatedly normalized.
|
Claude finished @codeflash-ai[bot]'s task in 2m 55s —— View job PR Review Summary
Prek Checks1 issue found and auto-fixed — the two new imports were added in the wrong position:
Ruff auto-corrected the sort order. Fix committed and pushed: Post-fix prek run: ✅ all checks pass. Code ReviewThe optimization is correct and well-scoped. A few observations: Cache key consistency — Module-level shared cache — The LRU cache is shared by both Cache size — No bugs, security issues, or breaking API changes found. Duplicate DetectionNo duplicates detected. The Python normalizer ( Test CoverageExisting test suite passes (97 tests in Last updated: 2026-03-06T16:29Z |
|
Closing stale optimization PR. |
⚡️ This pull request contains optimizations for PR #1780
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/normalizer.📄 114% (1.14x) speedup for
JavaScriptSupport.normalize_codeincodeflash/languages/javascript/support.py⏱️ Runtime :
680 microseconds→317 microseconds(best of5runs)📝 Explanation and details
The optimization moves the
normalize_js_codeimport from inside the method to module-level and wraps it with a 128-entry LRU cache (functools.lru_cache), achieving a 114% speedup by eliminating repeated parsing and normalization of duplicate code snippets. Line profiler shows the original spent 96.9% of wall time callingnormalize_js_code(which internally spends ~74% onnormalize_treeand AST traversal); caching this result avoids re-parsing identical source strings, common during deduplication workflows. The smaller cache size (128 vs. 1024) balances memory usage with hit-rate for typical workloads where a modest number of distinct snippets are repeatedly normalized.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_languages/test_javascript_support.py::TestNormalizeCode.test_different_logic_not_equaltest_languages/test_javascript_support.py::TestNormalizeCode.test_removes_commentstest_languages/test_javascript_support.py::TestNormalizeCode.test_same_logic_different_vars_are_equaltest_languages/test_language_parity.py::TestNormalizeCodeParity.test_preserves_code_structuretest_languages/test_language_parity.py::TestNormalizeCodeParity.test_removes_commentsTo edit these changes
git checkout codeflash/optimize-pr1780-2026-03-06T16.27.27and push.