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

Small Lint cops perf tweaks #8358

Merged
merged 1 commit into from Jul 18, 2020
Merged

Conversation

fatkodima
Copy link
Contributor

Ran on larger discourse codebase (3.5k files).

$ bin/rubocop-profile --force-default-config --cache false --only Lint ../discourse

Before

 1   965  (    7.3%)  RuboCop::Cop::Lint::DeprecatedClassMethods#on_send
 2   952  (    7.2%)  RuboCop::Cop::Lint::Debugger#on_send
 3   806  (    6.1%)  RuboCop::Cop::Lint::AmbiguousBlockAssociation#on_send
     642  (    4.8%)  RuboCop::Cop::Lint::ParenthesesAsGroupedExpression#on_send
     623  (    4.7%)  RuboCop::Cop::Lint::SafeNavigationChain#on_send
 4   605  (    4.6%)  RuboCop::Cop::Lint::FormatParameterMismatch#on_send
 5   533  (    4.4%)  RuboCop::Cop::Lint::DuplicateMethods#on_send
 6   500  (    3.8%)  RuboCop::Cop::Lint::InterpolationCheck#on_str
 7   493  (    3.7%)  RuboCop::Cop::Lint::IneffectiveAccessModifier#on_class
 8   473  (    3.6%)  RuboCop::Cop::Lint::BigDecimalNew#on_send
     467  (    3.5%)  RuboCop::Cop::Lint::RequireParentheses#on_send
 9   464  (    3.5%)  RuboCop::Cop::Lint::UriRegexp#on_send
     403  (    3.0%)  RuboCop::Cop::Lint::MultipleComparison#on_send
     376  (    2.8%)  RuboCop::Cop::Lint::Void#on_begin
     332  (    2.5%)  RuboCop::Cop::Lint::UriEscapeUnescape#on_send
     313  (    2.4%)  RuboCop::Cop::Lint::UnreachableCode#on_begin
     312  (    2.4%)  RuboCop::Cop::Lint::DuplicateMethods#on_def
10   312  (    2.4%)  RuboCop::Cop::Lint::InheritException#on_send
     299  (    2.3%)  RuboCop::Cop::Lint::UselessComparison#on_send
     296  (    2.2%)  RuboCop::Cop::Lint::DuplicateHashKey#on_hash
     259  (    2.0%)  RuboCop::Cop::Lint::EachWithObjectArgument#on_send
11   252  (    1.9%)  RuboCop::Cop::Lint::RandOne#on_send
     228  (    1.7%)  RuboCop::Cop::Lint::LiteralAsCondition#on_send

After

     635  (    7.3%)  RuboCop::Cop::Lint::SafeNavigationChain#on_send
     597  (    6.9%)  RuboCop::Cop::Lint::ParenthesesAsGroupedExpression#on_send
     451  (    5.2%)  RuboCop::Cop::Lint::RequireParentheses#on_send
3    404  (    4.7%)  RuboCop::Cop::Lint::AmbiguousBlockAssociation#on_send
     346  (    4.0%)  RuboCop::Cop::Lint::Void#on_begin
5    320  (    3.7%)  RuboCop::Cop::Lint::DuplicateMethods#on_def
     312  (    3.6%)  RuboCop::Cop::Lint::UriEscapeUnescape#on_send
     303  (    3.5%)  RuboCop::Cop::Lint::UselessComparison#on_send
     291  (    3.4%)  RuboCop::Cop::Lint::UnreachableCode#on_begin
     280  (    3.2%)  RuboCop::Cop::Lint::MultipleComparison#on_send
     275  (    3.2%)  RuboCop::Cop::Lint::EachWithObjectArgument#on_send
     263  (    3.0%)  RuboCop::Cop::Lint::DuplicateHashKey#on_hash
     250  (    2.9%)  RuboCop::Cop::Lint::SendWithMixinArgument#on_send
7    240  (    2.8%)  RuboCop::Cop::Lint::IneffectiveAccessModifier#on_class
6    239  (    2.5%)  RuboCop::Cop::Lint::InterpolationCheck#on_str
     188  (    2.2%)  RuboCop::Cop::Lint::ImplicitStringConcatenation#on_dstr
     187  (    2.2%)  RuboCop::Cop::Lint::RedundantRequireStatement#on_send
8    184  (    2.1%)  RuboCop::Cop::Lint::BigDecimalNew#on_send
2    182  (    2.1%)  RuboCop::Cop::Lint::Debugger#on_send
     176  (    2.0%)  RuboCop::Cop::Lint::DuplicateMethods#on_send
     168  (    1.9%)  RuboCop::Cop::Lint::UselessAccessModifier#on_class
     166  (    1.9%)  RuboCop::Cop::Lint::LiteralAsCondition#on_send
 9   159  (    1.8%)  RuboCop::Cop::Lint::UriRegexp#on_send
 1   143  (    1.7%)  RuboCop::Cop::Lint::DeprecatedClassMethods#on_send
 4   140  (    1.6%)  RuboCop::Cop::Lint::FormatParameterMismatch#on_send
     113  (    1.3%)  RuboCop::Cop::Lint::AssignmentInCondition#on_if
      91  (    1.1%)  RuboCop::Cop::Lint::UselessAccessModifier#on_block

debugger byebug remote_byebug pry remote_pry pry_remote console rescue
save_and_open_page save_and_open_screenshot save_screenshot irb
].to_set.freeze

def_node_matcher :kernel?, <<~PATTERN
Copy link
Contributor

@marcandre marcandre Jul 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make a release of rubocop-ast soon so we can use #global_const? :-)

Should we also add #kernel_call? == {#global_const?(:Kernel) nil?} or similar to rubocop-ast?

@bbatsov bbatsov merged commit cb57295 into rubocop:master Jul 18, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 18, 2020

Nicely done! 🙇‍♂️

@@ -30,10 +30,11 @@ class AmbiguousBlockAssociation < Cop
'call.'

def on_send(node)
return if !node.arguments? || node.parenthesized? ||
node.last_argument.lambda? || allowed_method?(node)
return unless node.arguments?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was surprised to see that this line was still taking non-negligible time, so I investigated a bit and this led to a bunch of fixes and a bunch of optimizations in rubocop-ast 🎉

@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'set'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to require 'set' here, it's required by parser and (just to be explicit) by rubocop-ast too.

@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'set'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need

def on_send(node)
return unless METHOD_DEF_METHODS.include?(node.method_name)

if (name = alias_method?(node))
return unless name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is redundant. Apparently we don't have a cop for this 😅

@@ -21,11 +21,10 @@ class InterpolationCheck < Cop
'Use double quoted strings if you need interpolation.'

def on_str(node)
return if heredoc?(node)

parent = node.parent
return if parent && (parent.dstr_type? || parent.regexp_type?)
return unless /(?<!\\)#\{.*\}/.match?(node.source.scrub)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, parser only handles valid utf-8 sequences, so scrub should not be needed at all here, or am I missing something?

Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work 💪

A few minor comments for you

@marcandre
Copy link
Contributor

Oh, merged already 😅

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 18, 2020

raise ConcurrentReviewException 😉

@marcandre marcandre mentioned this pull request Jul 20, 2020
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.

None yet

3 participants