Skip to content

⚡️ Speed up method JavaScriptSupport.normalize_code by 114% in PR #1780 (fix/normalizer)#1782

Closed
codeflash-ai[bot] wants to merge 2 commits into
fix/normalizerfrom
codeflash/optimize-pr1780-2026-03-06T16.27.27
Closed

⚡️ Speed up method JavaScriptSupport.normalize_code by 114% in PR #1780 (fix/normalizer)#1782
codeflash-ai[bot] wants to merge 2 commits into
fix/normalizerfrom
codeflash/optimize-pr1780-2026-03-06T16.27.27

Conversation

@codeflash-ai

@codeflash-ai codeflash-ai Bot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

⚡️ 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.

This PR will be automatically closed if the original PR is merged.


📄 114% (1.14x) speedup for JavaScriptSupport.normalize_code in codeflash/languages/javascript/support.py

⏱️ Runtime : 680 microseconds 317 microseconds (best of 5 runs)

📝 Explanation and details

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.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 104 Passed
🌀 Generated Regression Tests 🔘 None Found
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 66.7%
⚙️ Click to see Existing Unit Tests
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_languages/test_javascript_support.py::TestNormalizeCode.test_different_logic_not_equal 48.1μs 1.30μs 3594%✅
test_languages/test_javascript_support.py::TestNormalizeCode.test_removes_comments 78.0μs 3.35μs 2233%✅
test_languages/test_javascript_support.py::TestNormalizeCode.test_same_logic_different_vars_are_equal 106μs 1.57μs 6654%✅
test_languages/test_language_parity.py::TestNormalizeCodeParity.test_preserves_code_structure 196μs 128μs 53.0%✅
test_languages/test_language_parity.py::TestNormalizeCodeParity.test_removes_comments 251μs 182μs 37.5%✅

To edit these changes git checkout codeflash/optimize-pr1780-2026-03-06T16.27.27 and push.

Codeflash Static Badge

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.
@codeflash-ai codeflash-ai Bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Mar 6, 2026
@claude

claude Bot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Claude finished @codeflash-ai[bot]'s task in 2m 55s —— View job


PR Review Summary

  • Gather context and read changed files
  • Triage PR scope
  • Run lint/type checks
  • Check stale review threads
  • Code review
  • Duplicate detection
  • Test coverage
  • Post summary

Prek Checks

1 issue found and auto-fixed — the two new imports were added in the wrong position:

  • from functools import lru_cache (stdlib) was placed after all codeflash.* imports
  • from codeflash.languages.javascript.normalizer import normalize_js_code was not sorted alphabetically among sibling imports

Ruff auto-corrected the sort order. Fix committed and pushed: 347b0b7f.

Post-fix prek run: ✅ all checks pass.

Code Review

The optimization is correct and well-scoped. A few observations:

Cache key consistency_cached_normalize_js_code is always called as _cached_normalize_js_code(source, typescript=is_ts) using a keyword argument. lru_cache treats f(x, True) and f(x, typescript=True) as distinct cache keys, so callers should be consistent. The single call site in normalize_code always uses the keyword form, so this is fine in practice, but worth noting if this function is ever called directly elsewhere.

Module-level shared cache — The LRU cache is shared by both JavaScriptSupport (always typescript=False) and TypeScriptSupport (always typescript=True). This is correct and efficient.

Cache sizemaxsize=128 is appropriate for deduplication workflows where a bounded set of distinct snippets is repeatedly compared.

No bugs, security issues, or breaking API changes found.

Duplicate Detection

No duplicates detected. The Python normalizer (codeflash/languages/python/normalizer.py) has no caching layer, so this is not a case of reimplementing shared logic — it's a JS-specific optimization.

Test Coverage

Existing test suite passes (97 tests in test_javascript_support.py and test_language_parity.py). The PR reports 66.7% coverage for the changed method. The normalize_code path (both the caching hit and the fallback) is exercised by tests.


Last updated: 2026-03-06T16:29Z

@claude

claude Bot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Closing stale optimization PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants