Skip to content

perf: cache RuboCop team and filter cop registry to enabled cops#454

Open
ryanquanz wants to merge 1 commit intomainfrom
perf/cache-rubocop-team
Open

perf: cache RuboCop team and filter cop registry to enabled cops#454
ryanquanz wants to merge 1 commit intomainfrom
perf/cache-rubocop-team

Conversation

@ryanquanz
Copy link
Copy Markdown

Summary

Cache the RuboCop cop team and registry in initialize instead of rebuilding them for every ERB tag. Filter the registry to only enabled cops. Memoize target_ruby_version, the version check, and the global registry.

Problem

The old code called build_team (which calls cop_classesTeam.mobilize) inside inspect_content, which runs per ERB node. This created ~500 cop instances and a new Team for every <%= %> tag. The team and config are immutable across a single linter instance's lifetime — there was never a reason to rebuild per node.

Also, cop_classes included all registered cops (~569) regardless of whether they were enabled in the merged config. Now only enabled cops (~510 in a typical config) are included.

Impact

The improvement depends on ERB tag density — more tags per file means more avoided rebuilds.

ERB tags/file Before After Reduction
2 0.64s 0.47s −27%
10 3.25s 2.42s −26%
15 5.35s 3.49s −35%
25 8.94s 5.56s −38%

(50 synthetic files, all default linters enabled)

Changes

1 file changed, +21 −11. No new tests needed — existing rubocop_spec.rb covers all offense detection and autocorrect paths. The behavioral contract is identical.

Reproducible benchmark
# Save anywhere, run with: bundle exec ruby /tmp/erblint_benchmark.rb
require "erb_lint/all"; require "benchmark"; require "fileutils"
WORKDIR = Dir.mktmpdir("erblint_bench")
50.times do |i|
  lines = ['<div class="container">']
  15.times do |j|
    lines.concat(["  <div>", "    <%= helper_method_#{j}(arg1, arg2) %>",
      "    <% if condition_#{j} %>", "      <span><%= object_#{j}.name %></span>",
      "    <% end %>", "  </div>"])
  end
  File.write(File.join(WORKDIR, "template_#{i}.html.erb"), lines.push("</div>").join("\n"))
end
files = Dir.glob(File.join(WORKDIR, "*.html.erb"))
fl = ERBLint::FileLoader.new(WORKDIR)
cfg = ERBLint::RunnerConfig.default_for(ERBLint::RunnerConfig.new({
  "EnableDefaultLinters" => true, "linters" => { "ErbSafety" => { "enabled" => false },
  "Rubocop" => { "enabled" => true, "rubocop_config" => {
    "Layout/InitialIndentation" => { "Enabled" => false },
    "Layout/TrailingEmptyLines" => { "Enabled" => false },
    "Layout/TrailingWhitespace" => { "Enabled" => false },
    "Naming/FileName" => { "Enabled" => false },
    "Style/FrozenStringLiteralComment" => { "Enabled" => false },
    "Layout/LineLength" => { "Enabled" => false },
    "Lint/UselessAssignment" => { "Enabled" => false }}}}}, fl))
run = -> {
  r = ERBLint::Runner.new(fl, cfg)
  files.each { |f| r.clear_offenses; r.run(ERBLint::ProcessedSource.new(f, File.read(f, encoding: Encoding::UTF_8))) }
}
run.call # warmup
times = 3.times.map { GC.start; Benchmark.realtime { run.call } }.sort
puts "#{files.size} files, median: #{format("%.3f", times[1])}s"
FileUtils.rm_rf(WORKDIR)

Cache the RuboCop cop team and registry in initialize rather than
rebuilding them for every ERB tag. Previously, build_team and
cop_classes were called per-node, creating ~500 cop instances and a
new Team for each <%= %> tag.

Also filter the cop registry to only include cops that are actually
enabled in the merged config, and memoize target_ruby_version, the
version check, and the global registry.
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