rubocop: reduce redundant block.call for performance#4935
Closed
kenhys wants to merge 1 commit intofluent:masterfrom
Closed
rubocop: reduce redundant block.call for performance#4935kenhys wants to merge 1 commit intofluent:masterfrom
kenhys wants to merge 1 commit intofluent:masterfrom
Conversation
This is cosmetic change, it does not change behavior at all. The following rubocop configuration detects it.
```
Performance/RedundantBlockCall:
Enable: true
```
Benchmark result:
```
ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux]
Warming up --------------------------------------
block call 1.296M i/100ms
yield with &block 1.708M i/100ms
yield without &block 1.687M i/100ms
Calculating -------------------------------------
block call 12.964M (± 0.8%) i/s (77.14 ns/i) - 66.097M in 5.098814s
yield with &block 17.186M (± 0.5%) i/s (58.19 ns/i) - 87.092M in 5.067659s
yield without &block 17.162M (± 0.3%) i/s (58.27 ns/i) - 86.062M in 5.014579s
Comparison:
yield with &block: 17186340.4 i/s
yield without &block: 17162477.2 i/s - same-ish: difference falls within error
block call: 12964086.6 i/s - 1.33x slower
```
yield with/without &block is same-ish. For simplicity, use without &block.
Appendix: benchmark script
```ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'benchmark-memory'
end
def block_call(s, &block)
block.call(s) if block_given?
end
def yield_with_block(s, &block)
yield(s) if block_given?
end
def yield_without_block(s)
yield(s) if block_given?
end
Benchmark.ips do |x|
x.report("block call") {
block_call("dummy") do |message|
message
end
}
x.report("yield with &block") {
yield_with_block("dummy") do |message|
message
end
}
x.report("yield without &block") {
yield_without_block("dummy") do |message|
message
end
}
x.compare!
end
```
Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue(s) this PR fixes:
Fixes #
What this PR does / why we need it:
This is cosmetic change, it does not change behavior at all. The following rubocop configuration detects it.
Benchmark result:
yield with/without &block is same-ish. For simplicity, use without &block.
Appendix: benchmark script
Docs Changes:
N/A
Release Note:
N/A