From 8ebe36c766eec1349ae964b7d654ed2539396bae Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Wed, 24 Aug 2022 12:38:38 +0900 Subject: [PATCH] [Fix #10919] Fix a huge performance regression between 1.32.0 and 1.33.0 Fix: #10919 Before: - 1.32.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 2.7.2 x86_64-darwin21) `bundle exec rubocop --cache false --only 16.83s user 1.30s system 93% cpu 19.320 total` - 1.35.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 2.7.2 x86_64-darwin21) `bundle exec rubocop --cache false --only 1255.21s user 5.32s system 71% cpu 29:17.76 total` After: `bundle exec rubocop --cache false --only 16.64s user 1.25s system 94% cpu 19.002 total` --- .../fix_fix_a_huge_performance_regression.md | 1 + lib/rubocop/cop/mixin/allowed_methods.rb | 15 +++++--- lib/rubocop/cop/mixin/allowed_pattern.rb | 18 ++++++--- spec/rubocop/cli/auto_gen_config_spec.rb | 37 +++++++++++-------- 4 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 changelog/fix_fix_a_huge_performance_regression.md diff --git a/changelog/fix_fix_a_huge_performance_regression.md b/changelog/fix_fix_a_huge_performance_regression.md new file mode 100644 index 00000000000..638ca8a1848 --- /dev/null +++ b/changelog/fix_fix_a_huge_performance_regression.md @@ -0,0 +1 @@ +* [#10919](https://github.com/rubocop/rubocop/issues/10919): Fix a huge performance regression between 1.32.0 and 1.33.0. ([@ydah][]) diff --git a/lib/rubocop/cop/mixin/allowed_methods.rb b/lib/rubocop/cop/mixin/allowed_methods.rb index 64829d79c90..0aaeec4d1b7 100644 --- a/lib/rubocop/cop/mixin/allowed_methods.rb +++ b/lib/rubocop/cop/mixin/allowed_methods.rb @@ -17,16 +17,21 @@ def allowed_method?(name) # @api public def allowed_methods - deprecated_values = cop_config_deprecated_values - if deprecated_values.any?(Regexp) - cop_config.fetch('AllowedMethods', []) + if cop_config_deprecated_values.any?(Regexp) + cop_config_allowed_methods else - Array(cop_config['AllowedMethods']).concat(deprecated_values) + cop_config_allowed_methods + cop_config_deprecated_values end end + def cop_config_allowed_methods + @cop_config_allowed_methods ||= Array(cop_config.fetch('AllowedMethods', [])) + end + def cop_config_deprecated_values - Array(cop_config['IgnoredMethods']).concat(Array(cop_config['ExcludedMethods'])) + @cop_config_deprecated_values ||= + Array(cop_config.fetch('IgnoredMethods', [])) + + Array(cop_config.fetch('ExcludedMethods', [])) end end # @deprecated IgnoredMethods class has been replaced with AllowedMethods. diff --git a/lib/rubocop/cop/mixin/allowed_pattern.rb b/lib/rubocop/cop/mixin/allowed_pattern.rb index f3d47fb3ce9..8714fc4b02d 100644 --- a/lib/rubocop/cop/mixin/allowed_pattern.rb +++ b/lib/rubocop/cop/mixin/allowed_pattern.rb @@ -30,15 +30,23 @@ def matches_allowed_pattern?(line) def allowed_patterns # Since there could be a pattern specified in the default config, merge the two # arrays together. - patterns = Array(cop_config['AllowedPatterns']).concat(Array(cop_config['IgnoredPatterns'])) - deprecated_values = cop_config_deprecated_methods_values - return patterns unless deprecated_values.any?(Regexp) + if cop_config_deprecated_methods_values.any?(Regexp) + cop_config_patterns_values + cop_config_deprecated_methods_values + else + cop_config_patterns_values + end + end - Array(patterns.concat(deprecated_values)) + def cop_config_patterns_values + @cop_config_patterns_values ||= + Array(cop_config.fetch('AllowedPatterns', [])) + + Array(cop_config.fetch('IgnoredPatterns', [])) end def cop_config_deprecated_methods_values - Array(cop_config['IgnoredMethods']).concat(Array(cop_config['ExcludedMethods'])) + @cop_config_deprecated_methods_values ||= + Array(cop_config.fetch('IgnoredMethods', [])) + + Array(cop_config.fetch('ExcludedMethods', [])) end end diff --git a/spec/rubocop/cli/auto_gen_config_spec.rb b/spec/rubocop/cli/auto_gen_config_spec.rb index 41762f3a546..5b645972735 100644 --- a/spec/rubocop/cli/auto_gen_config_spec.rb +++ b/spec/rubocop/cli/auto_gen_config_spec.rb @@ -951,29 +951,34 @@ def a; end YAML end - context 'when configurable parameter is an obsolete parameter' do - it 'displayed as a new parameter setting value without duplication' do + context 'when duplicated default configuration parameter' do + before do + RuboCop::ConfigLoader.default_configuration['Naming/MethodParameterName'] + .merge!('AllowedNames' => %w[at by at]) + end + + it 'parameters are displayed without duplication' do create_file('.rubocop.yml', <<~YAML) - AllCops: - NewCops: enable - Naming/MethodName: - # `IgnoredPatterns` is obsolete. `AllowedPatterns` is newly used. - IgnoredPatterns: - - change - - not_change + Naming/VariableName: + Enabled: false YAML - create_file('example1.rb', ['# frozen_string_literal: true', '', 'def fooBar; end']) - create_file('example2.rb', ['# frozen_string_literal: true', '', 'def fooBar; end']) + create_file('example1.rb', <<~TEXT) + # frozen_string_literal: true + + def bar(varOne, varTwo) + varOne + varTwo + end + TEXT expect(cli.run(['--auto-gen-config'])).to eq(0) File.readlines('.rubocop_todo.yml') expect(File.readlines('.rubocop_todo.yml')[9..].join) .to eq(<<~YAML) - # Configuration parameters: AllowedPatterns, IgnoredPatterns. - # SupportedStyles: snake_case, camelCase - # AllowedPatterns: change, not_change - Naming/MethodName: - EnforcedStyle: camelCase + # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. + # AllowedNames: at, by + Naming/MethodParameterName: + Exclude: + - 'example1.rb' YAML end end