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

Add new Performance/BlockGivenWithExplicitBlock cop #173

Merged
merged 1 commit into from
Oct 17, 2020

Conversation

fatkodima
Copy link
Contributor

This cop identifies unnecessary use of a block_given? where explicit check of block argument would suffice.

# bad
def method(&block)
  do_something if block_given?
end

# good
def method(&block)
  do_something if block
end

Benchmark

# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
  gem 'benchmark-ips'
end

def if_block(&block)
  if block
  end
end

def if_block_given(&block)
  if block_given?
  end
end

Benchmark.ips do |x|
  x.report('if block')         { if_block }
  x.report('if block_given?')  { if_block_given }
  x.compare!
end

Results

Warming up --------------------------------------
            if block     1.039M i/100ms
     if block_given?   844.838k i/100ms
Calculating -------------------------------------
            if block     10.382M (± 1.1%) i/s -     51.960M in   5.005227s
     if block_given?      8.443M (± 1.1%) i/s -     42.242M in   5.003794s

Comparison:
            if block: 10382417.4 i/s
     if block_given?:  8443073.9 i/s - 1.23x  (± 0.00) slower

@marcandre
Copy link
Contributor

marcandre commented Oct 2, 2020

Oh, cool, I had to do just that for ostruct and was wondering if that would trigger block capturing or not (and be slower). Glad it doesn't.

I'd mark the autocorrection as unsafe, even it's quite unlikely to be:

def foo(&block)
  block ||= ->(x) { ...}
  warn "Using default ..." unless block_given?
  foo(&block)
end

Or you bail of the autocorrection if the variable is reassigned?

@fatkodima fatkodima force-pushed the block_given_with_explicit_block branch from 3885841 to 8756498 Compare October 2, 2020 07:52
@fatkodima
Copy link
Contributor Author

Good point. Updated to not trigger an offense when the block arg is reassigned.

@marcandre
Copy link
Contributor

Sorry I forgot to check this. Don't hesitate to ping me after 2-3 days.

@fatkodima fatkodima force-pushed the block_given_with_explicit_block branch from 8756498 to 032993d Compare October 16, 2020 11:46
@fatkodima
Copy link
Contributor Author

Updated.

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.

LGTM 👍

@koic koic merged commit 91bd849 into rubocop:master Oct 17, 2020
@koic
Copy link
Member

koic commented Oct 17, 2020

Thanks!

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