perf: cache RuboCop team and filter cop registry to enabled cops#454
Open
perf: cache RuboCop team and filter cop registry to enabled cops#454
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cache the RuboCop cop team and registry in
initializeinstead of rebuilding them for every ERB tag. Filter the registry to only enabled cops. Memoizetarget_ruby_version, the version check, and the global registry.Problem
The old code called
build_team(which callscop_classes→Team.mobilize) insideinspect_content, which runs per ERB node. This created ~500 cop instances and a newTeamfor 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_classesincluded 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.
(50 synthetic files, all default linters enabled)
Changes
1 file changed, +21 −11. No new tests needed — existing
rubocop_spec.rbcovers all offense detection and autocorrect paths. The behavioral contract is identical.Reproducible benchmark