Skip to content

Commit

Permalink
Fix compatibility with ruby version 2.3.X
Browse files Browse the repository at this point in the history
`Enumerable#sum` appears only in ruby version 2.4.0

`.inject(:+)` seems to be the most performant:

workers = {}
1_000.times { |i| workers[i] = rand(1000) }

Benchmark.ips do |x|
  x.report("         sum") { workers.values.sum }
  x.report("  inject(:+)") { workers.values.inject(:+) || 0 }
  x.report(" inject(&:+)") { workers.values.inject(&:+) || 0 }
  x.compare!
end

Warming up --------------------------------------
                 sum    27.656k i/100ms
          inject(:+)    28.784k i/100ms
         inject(&:+)     2.200k i/100ms
Calculating -------------------------------------
                 sum    287.239k (±15.9%) i/s -      1.410M in   5.042619s
          inject(:+)    318.193k (±16.0%) i/s -      1.583M in   5.109662s
         inject(&:+)     21.475k (± 2.7%) i/s -    107.800k in   5.023639s

Comparison:
          inject(:+):   318193.1 i/s
                 sum:   287238.8 i/s - same-ish: difference falls within error
         inject(&:+):    21475.3 i/s - 14.82x  (± 0.00) slower
  • Loading branch information
adelnabiullin committed Jun 15, 2020
1 parent 7dc7683 commit 2d187a4
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/puma_worker_killer/puma_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def largest_worker_memory
# Will refresh @workers
def get_total(workers = set_workers)
master_memory = GetProcessMem.new(Process.pid).mb
worker_memory = workers.values.sum
worker_memory = workers.values.inject(:+) || 0
worker_memory + master_memory
end
alias :get_total_memory :get_total
Expand Down

0 comments on commit 2d187a4

Please sign in to comment.