Skip to content

Commit edc5cb5

Browse files
committed
rubocop: reduce redundant block.call for performance
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>
1 parent 1f39f41 commit edc5cb5

File tree

19 files changed

+69
-69
lines changed

19 files changed

+69
-69
lines changed

lib/fluent/compat/output.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,14 @@ def start
404404
end
405405
end
406406

407-
def detach_process(&block)
407+
def detach_process
408408
log.warn "detach_process is not supported in this version. ignored."
409-
block.call
409+
yield
410410
end
411411

412-
def detach_multi_process(&block)
412+
def detach_multi_process
413413
log.warn "detach_process is not supported in this version. ignored."
414-
block.call
414+
yield
415415
end
416416
end
417417

@@ -541,14 +541,14 @@ def start
541541
end
542542
end
543543

544-
def detach_process(&block)
544+
def detach_process
545545
log.warn "detach_process is not supported in this version. ignored."
546-
block.call
546+
yield
547547
end
548548

549-
def detach_multi_process(&block)
549+
def detach_multi_process
550550
log.warn "detach_process is not supported in this version. ignored."
551-
block.call
551+
yield
552552
end
553553
end
554554

@@ -698,14 +698,14 @@ def start
698698
end
699699
end
700700

701-
def detach_process(&block)
701+
def detach_process
702702
log.warn "detach_process is not supported in this version. ignored."
703-
block.call
703+
yield
704704
end
705705

706-
def detach_multi_process(&block)
706+
def detach_multi_process
707707
log.warn "detach_process is not supported in this version. ignored."
708-
block.call
708+
yield
709709
end
710710

711711
# Original TimeSlicedOutput#emit doesn't call #format_stream

lib/fluent/config/element.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ def [](key)
127127
super
128128
end
129129

130-
def check_not_fetched(&block)
130+
def check_not_fetched
131131
each_key { |key|
132132
if @unused.include?(key)
133-
block.call(key, self)
133+
yield(key, self) if block_given?
134134
end
135135
}
136136
@elements.each { |e|

lib/fluent/event.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def slice(index, num)
106106
end
107107
end
108108

109-
def each(unpacker: nil, &block)
110-
block.call(@time, @record)
109+
def each(unpacker: nil)
110+
yield(@time, @record)
111111
nil
112112
end
113113
end
@@ -189,11 +189,11 @@ def slice(index, num)
189189
MultiEventStream.new(@time_array.slice(index, num), @record_array.slice(index, num))
190190
end
191191

192-
def each(unpacker: nil, &block)
192+
def each(unpacker: nil)
193193
time_array = @time_array
194194
record_array = @record_array
195195
for i in 0..time_array.length-1
196-
block.call(time_array[i], record_array[i])
196+
yield(time_array[i], record_array[i])
197197
end
198198
nil
199199
end
@@ -253,10 +253,10 @@ def slice(index, num)
253253
MultiEventStream.new(@unpacked_times.slice(index, num), @unpacked_records.slice(index, num))
254254
end
255255

256-
def each(unpacker: nil, &block)
256+
def each(unpacker: nil)
257257
ensure_unpacked!(unpacker: unpacker)
258258
@unpacked_times.each_with_index do |time, i|
259-
block.call(time, @unpacked_records[i])
259+
yield(time, @unpacked_records[i])
260260
end
261261
nil
262262
end

lib/fluent/log.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,11 @@ def on_trace
309309
yield
310310
end
311311

312-
def trace(*args, &block)
312+
def trace(*args)
313313
return if @level > LEVEL_TRACE
314314
type = log_type(args)
315315
return if skipped_type?(type)
316-
args << block.call if block
316+
args << yield if block_given?
317317
time, msg = event(:trace, args)
318318
return if time.nil?
319319
puts [@color_trace, @formatter.call(type, time, LEVEL_TRACE, msg), @color_reset].join
@@ -331,11 +331,11 @@ def on_debug
331331
yield
332332
end
333333

334-
def debug(*args, &block)
334+
def debug(*args)
335335
return if @level > LEVEL_DEBUG
336336
type = log_type(args)
337337
return if skipped_type?(type)
338-
args << block.call if block
338+
args << yield if block_given?
339339
time, msg = event(:debug, args)
340340
return if time.nil?
341341
puts [@color_debug, @formatter.call(type, time, LEVEL_DEBUG, msg), @color_reset].join
@@ -352,11 +352,11 @@ def on_info
352352
yield
353353
end
354354

355-
def info(*args, &block)
355+
def info(*args)
356356
return if @level > LEVEL_INFO
357357
type = log_type(args)
358358
return if skipped_type?(type)
359-
args << block.call if block
359+
args << yield if block_given?
360360
time, msg = event(:info, args)
361361
return if time.nil?
362362
puts [@color_info, @formatter.call(type, time, LEVEL_INFO, msg), @color_reset].join
@@ -373,11 +373,11 @@ def on_warn
373373
yield
374374
end
375375

376-
def warn(*args, &block)
376+
def warn(*args)
377377
return if @level > LEVEL_WARN
378378
type = log_type(args)
379379
return if skipped_type?(type)
380-
args << block.call if block
380+
args << yield if block_given?
381381
time, msg = event(:warn, args)
382382
return if time.nil?
383383
puts [@color_warn, @formatter.call(type, time, LEVEL_WARN, msg), @color_reset].join
@@ -394,11 +394,11 @@ def on_error
394394
yield
395395
end
396396

397-
def error(*args, &block)
397+
def error(*args)
398398
return if @level > LEVEL_ERROR
399399
type = log_type(args)
400400
return if skipped_type?(type)
401-
args << block.call if block
401+
args << yield if block_given?
402402
time, msg = event(:error, args)
403403
return if time.nil?
404404
puts [@color_error, @formatter.call(type, time, LEVEL_ERROR, msg), @color_reset].join
@@ -415,11 +415,11 @@ def on_fatal
415415
yield
416416
end
417417

418-
def fatal(*args, &block)
418+
def fatal(*args)
419419
return if @level > LEVEL_FATAL
420420
type = log_type(args)
421421
return if skipped_type?(type)
422-
args << block.call if block
422+
args << yield if block_given?
423423
time, msg = event(:fatal, args)
424424
return if time.nil?
425425
puts [@color_fatal, @formatter.call(type, time, LEVEL_FATAL, msg), @color_reset].join

lib/fluent/plugin/buffer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ class ShouldRetry < StandardError; end
650650
# 3. enqueue existing chunk & retry whole method if chunk was not empty
651651
# 4. go to step_by_step writing
652652

653-
def write_once(metadata, data, format: nil, size: nil, &block)
653+
def write_once(metadata, data, format: nil, size: nil)
654654
return if data.empty?
655655

656656
stored = false
@@ -701,7 +701,7 @@ def write_once(metadata, data, format: nil, size: nil, &block)
701701
end
702702

703703
if stored
704-
block.call(chunk, adding_bytesize)
704+
yield(chunk, adding_bytesize)
705705
end
706706
end
707707

@@ -726,7 +726,7 @@ def write_once(metadata, data, format: nil, size: nil, &block)
726726
# 2. append splits into the staged chunks as much as possible
727727
# 3. create unstaged chunk and append rest splits -> repeat it for all splits
728728

729-
def write_step_by_step(metadata, data, format, splits_count, &block)
729+
def write_step_by_step(metadata, data, format, splits_count)
730730
splits = []
731731
if splits_count > data.size
732732
splits_count = data.size
@@ -855,7 +855,7 @@ def write_step_by_step(metadata, data, format, splits_count, &block)
855855
modified_chunks.last[:adding_bytesize] = chunk.bytesize - original_bytesize
856856
end
857857
modified_chunks.each do |data|
858-
block.call(data[:chunk], data[:adding_bytesize], data[:errors])
858+
yield(data[:chunk], data[:adding_bytesize], data[:errors])
859859
end
860860
rescue ShouldRetry
861861
modified_chunks.each do |data|

lib/fluent/plugin/in_forward.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def handle_connection(conn)
239239
end
240240
end
241241

242-
def read_messages(conn, &block)
242+
def read_messages(conn)
243243
feeder = nil
244244
serializer = nil
245245
bytes = 0
@@ -250,7 +250,7 @@ def read_messages(conn, &block)
250250
if first == '{' || first == '[' # json
251251
parser = Yajl::Parser.new
252252
parser.on_parse_complete = ->(obj){
253-
block.call(obj, bytes, serializer)
253+
yield(obj, bytes, serializer)
254254
bytes = 0
255255
}
256256
serializer = :to_json.to_proc
@@ -260,7 +260,7 @@ def read_messages(conn, &block)
260260
serializer = :to_msgpack.to_proc
261261
feeder = ->(d){
262262
parser.feed_each(d){|obj|
263-
block.call(obj, bytes, serializer)
263+
yield(obj, bytes, serializer)
264264
bytes = 0
265265
}
266266
}

lib/fluent/plugin/output.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,9 +972,9 @@ def execute_chunking(tag, es, enqueue: false)
972972
end
973973
end
974974

975-
def write_guard(&block)
975+
def write_guard
976976
begin
977-
block.call
977+
yield
978978
rescue Fluent::Plugin::Buffer::BufferOverflowError
979979
log.warn "failed to write data into buffer by buffer overflow", action: @buffer_config.overflow_action
980980
case @buffer_config.overflow_action

lib/fluent/plugin/parser_json.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ def parser_type
9393
:text
9494
end
9595

96-
def parse_io(io, &block)
96+
def parse_io(io)
9797
y = Yajl::Parser.new
9898
y.on_parse_complete = ->(record){
99-
block.call(parse_time(record), record)
99+
yield(parse_time(record), record)
100100
}
101101
y.parse(io, @stream_buffer_size)
102102
end

lib/fluent/plugin/storage_local.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ def delete(key)
158158
@store.delete(key.to_s)
159159
end
160160

161-
def update(key, &block)
162-
@store[key.to_s] = block.call(@store[key.to_s])
161+
def update(key)
162+
@store[key.to_s] = yield(@store[key.to_s])
163163
end
164164
end
165165
end

lib/fluent/plugin_helper/compat_parameters.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ def compat_parameters_formatter(conf)
324324
conf
325325
end
326326

327-
def compat_parameters_copy_to_subsection_attributes(conf, params, &block)
327+
def compat_parameters_copy_to_subsection_attributes(conf, params)
328328
hash = {}
329329
params.each do |compat, current|
330330
next unless current
331331
if conf.has_key?(compat)
332332
if block_given?
333-
hash[current] = block.call(compat, conf[compat])
333+
hash[current] = yield(compat, conf[compat])
334334
else
335335
hash[current] = conf[compat]
336336
end

0 commit comments

Comments
 (0)