Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove memoization in Config#for_all_cops #3726

Closed
wants to merge 1 commit into from
Closed

Remove memoization in Config#for_all_cops #3726

wants to merge 1 commit into from

Conversation

volmer
Copy link
Contributor

@volmer volmer commented Nov 17, 2016

Sometimes a Config instance has its internal hash mutated, and changes on AllCops don't get reflected in #for_all_cops due to its memoization.

This problem doesn't happen when AllCops is set initially, but when AllCops is set afterwards @for_all_cops already got instantiated with an empty hash.

This issue is noticeable in ConfigLoaderResolver#resolve_inheritance, which mutates the config to merge the values coming from the parent configuration. When the parent config defines AllCops the values don't get used in places that rely on #for_all_cops.

I checked the git history and I didn't find any reason to have had that method memoized. I also run this change in a fairly large codebase and I didn't notice any noticeable performance decrease.

Sometimes a `Config` instance has its hash mutated, and changes
on `AllCops` don't get reflected in `#for_all_cops` due to its
memoization.

This problem doesn't happen when `AllCops` is set initially, but
when `AllCops` is set afterwards `@for_all_cops` already got
instantiated with an empty hash.

This issue is noticeable in `ConfigLoaderResolver#resolve_inheritance`,
which mutates the config to merge the values coming from the parent
configuration. When the parent config defines `AllCops` the values
don't get used in places that rely on `#for_all_cops`.

I checked the git history and I didn't find any reason to have had
that method memoized.
@etiennebarrie
Copy link
Contributor

It can be reproduced quite easily:

$ mkdir rubocop-3726
$ cd rubocop-3726
$ cat <<EOS >.rubocop-disabled.yml
AllCops:
  DisabledByDefault: true
EOS
$ echo inherit_from: .rubocop-disabled.yml > .rubocop.yml
$ echo self.puts 42 > offense.rb
$ rubocop
Inspecting 1 file
C

Offenses:

offense.rb:1:1: C: Redundant self detected.
self.puts 42
^^^^^^^^^^^^

1 file inspected, 1 offense detected

It worked up to 0.42.0, started failing at 0.43.0:

$ rubocop _0.42.0_
Inspecting 1 file
.

1 file inspected, no offenses detected
$ rubocop _0.43.0_
Inspecting 1 file
C

Offenses:

offense.rb:1:1: C: Redundant self detected.
self.puts 42
^^^^^^^^^^^^

1 file inspected, 1 offense detected

@volmer
Copy link
Contributor Author

volmer commented Dec 1, 2016

@jonas054 @bbatsov any inputs on this? Maybe a different approach?

@jonas054
Copy link
Collaborator

jonas054 commented Dec 2, 2016

The problem seems to be fixed in 0.46.0 by #3713. Can you confirm?

@volmer
Copy link
Contributor Author

volmer commented Dec 2, 2016

It is indeed! Thank you.

@volmer volmer closed this Dec 2, 2016
@volmer volmer deleted the for-all-cops-memoization branch December 2, 2016 03: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.

3 participants