Skip to content

Optimize/iterative traversal and scope caching#9

Merged
itamarga merged 8 commits intomainfrom
optimize/iterative-traversal-and-scope-caching
Mar 13, 2026
Merged

Optimize/iterative traversal and scope caching#9
itamarga merged 8 commits intomainfrom
optimize/iterative-traversal-and-scope-caching

Conversation

@itamarga
Copy link
Copy Markdown
Collaborator

No description provided.

itamarga and others added 7 commits March 13, 2026 10:25
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Single quotes for strings, double quotes for docstrings
- Type hints on all function signatures
- X | Y unions, X | None instead of Optional
- Native list, dict, tuple, set typing
- StrEnum where applicable (DecoderType, _JSType)
- Expanded abbreviated variable names
- Imports at top of files, never inside functions
- Reduced nesting via early returns and helper extraction
- match statements replacing long if/elif chains

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The iterative-only traversal caused a 16% regression due to Python tuple
allocation overhead. This adds recursive fast paths for enter-only visitors
(all 46 call sites) and scope tree building, with automatic fallback to
iterative traversal at depth > 500 to prevent stack overflow on deep ASTs.

Benchmark: 2.95s mean (was 3.67s iterative-only, 3.17s original recursive).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Avoid redundant build_scope_tree() calls by caching the result in the
deobfuscator loop and passing it to scope-using transforms. The cache is
invalidated when any transform modifies the AST. Also add a lazy parent
map (build_parent_map) to the base Transform class, replacing O(n)
find_parent() tree walks with O(1) lookups.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…and parent map fix

- Replace isinstance(x, dict/list) with type(x) is dict/list in hot paths
  of traverser.py and scope.py (pointer comparison vs MRO lookup)
- Convert node.get('type') to node['type'] where callers guarantee key exists
- Cache len() in _traverse_enter_only list iteration loop
- Fix parent map thrashing in class_static_resolver: build once before
  traversal, pass parent info from enter callback, invalidate once after

Benchmark: 3.03s → 2.71s mean on sample.js. Output is byte-identical.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@itamarga itamarga force-pushed the optimize/iterative-traversal-and-scope-caching branch from 018e8f6 to 4cc3a9c Compare March 13, 2026 09:49
@itamarga itamarga closed this Mar 13, 2026
@itamarga itamarga reopened this Mar 13, 2026
@itamarga itamarga merged commit 3190899 into main Mar 13, 2026
11 checks passed
@itamarga itamarga deleted the optimize/iterative-traversal-and-scope-caching branch March 14, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant