mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Optimize the TruncateMessage filter with Proc selectors
If we have a proc, we can pass it directly to the `select!` method
instead of having to manually call the selector. This gives a
significant speedup for the common case of simple selectors:
require 'benchmark/ips'
Benchmark.ips do |x|
selector = ->(value) { value < 500 }
values = (1..1000).to_a
x.report("call") { values.select { |v| selector.call(v) } }
x.report("block") { values.select(&selector) }
x.compare!
end
Warming up --------------------------------------
call 663.000 i/100ms
block 1.598k i/100ms
Calculating -------------------------------------
call 6.897k (± 4.8%) i/s - 34.476k in 5.011617s
block 16.561k (± 4.2%) i/s - 83.096k in 5.026999s
Comparison:
block: 16560.7 i/s
call: 6896.5 i/s - 2.40x slower
This commit is contained in:
parent
3db9041ab4
commit
f8d8c040d5
@ -80,7 +80,11 @@ module Rackstash
|
||||
|
||||
@selectors.each do |selector|
|
||||
return event if overall_size_of(messages) <= @max_size || messages.size <= 1
|
||||
messages.select! { |message| selector.call(message) }
|
||||
if selector.is_a?(Proc)
|
||||
messages.select!(&selector)
|
||||
else
|
||||
messages.select! { |message| selector.call(message) }
|
||||
end
|
||||
end
|
||||
return event if messages.size <= 1
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user