Skip to content

perf: memoize erb_nodes and tag_nodes in ProcessedSource#455

Open
ryanquanz wants to merge 1 commit into
mainfrom
perf/memoize-node-traversals
Open

perf: memoize erb_nodes and tag_nodes in ProcessedSource#455
ryanquanz wants to merge 1 commit into
mainfrom
perf/memoize-node-traversals

Conversation

@ryanquanz
Copy link
Copy Markdown

Summary

Add memoized erb_nodes and tag_nodes methods to ProcessedSource so that multiple linters share a single AST traversal per file instead of each traversing independently. Update all linters to use the memoized methods.

Problem

Every linter that processes ERB or HTML tags calls ast.descendants(:erb) or parser.nodes_with_type(:tag) independently. With 15+ linters enabled, the same AST is traversed 10+ times per file.

Impact

The improvement is modest in isolation (~1–11% depending on workload), but compounds with other linter-level optimizations and eliminates redundant work across all linters.

Changes

15 files changed, +81 −23.

  • Adds erb_nodes and tag_nodes memoized methods to ProcessedSource
  • Updates 13 linters with mechanical s/ast.descendants(:erb)/erb_nodes/ replacements
  • Private method signatures in RequireInputAutocomplete and RequireScriptNonce change from receiving parser to processed_source (internal only)
  • 6 new tests in processed_source_spec.rb verifying memoization (object identity) and correctness (equivalence with non-memoized traversal)

Add memoized erb_nodes and tag_nodes methods to ProcessedSource so
that multiple linters share a single AST traversal per file instead
of each traversing independently. Update all linters to use the
memoized methods.
@ryanquanz ryanquanz force-pushed the perf/memoize-node-traversals branch from f6c3ce5 to d19ef1d Compare March 30, 2026 14:24
@ryanquanz ryanquanz marked this pull request as ready for review March 30, 2026 14:45
@ryanquanz ryanquanz requested a review from etiennebarrie March 31, 2026 20:44
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