Skip to content

Disable Style/ExplicitBlockArgument cop rule

What does this MR do and why?

This MR disables Style/ExplicitBlockArgument.

Reason: Using yield is ~20% faster than &block. See gitlab-org/gitlab!94090 (comment 1452639709)

Impact on gitlab-org/gitlab

After disabling this rule we can remove pending TODOs. See https://gitlab.com/gitlab-org/gitlab/-/blob/88ce34b9f414d06d3f8eb96bb630289281e92f16/.rubocop_todo/style/explicit_block_argument.yml.

Follow-ups

Note that there's no to enforce the use of yield.

Style/BlockArgument

  1. Create 🆕 Style/BlockArgument
  2. Configure it with `EnforcedStyle'

OR

Performance/NoBlockArgument

  1. Create 🆕 :cop Performance/NoBlockArgument to flag the use of def x(&block) and suggest yield instead.
  2. Mention the conflict with Style/ExplicitBlockArgument

Benchmarks

From gitlab-org/gitlab!94090 (comment 1452639709)

Ruby: 3.1.4

# frozen_string_literal: true

require 'benchmark/ips'

def calling
  :ok
end

def explicit(&block)
  calling(&block)
end

# https://bugs.ruby-lang.org/issues/11256
def implicit(&)
  calling(&)
end

def yielding
  calling { yield }
end

Benchmark.ips do |x|
  x.report('explicit') { explicit { :ok } }
  x.report('implicit') { implicit { :ok } }
  x.report('yield') { yielding { :ok } }
  x.compare!
end
Warming up --------------------------------------
            explicit     1.594M i/100ms
            implicit     1.652M i/100ms
               yield     2.208M i/100ms
Calculating -------------------------------------
            explicit     16.135M (± 2.4%) i/s -     81.270M in   5.040211s
            implicit     16.168M (± 4.2%) i/s -     80.945M in   5.015849s
               yield     19.618M (± 0.9%) i/s -     99.350M in   5.064509s

Comparison:
               yield: 19618467.3 i/s
            implicit: 16168012.7 i/s - 1.21x  slower
            explicit: 16135291.8 i/s - 1.22x  slower

Same results for Ruby 3.2.2.

Edited by Peter Leitzen

Merge request reports