Skip to content

Use a more succinct syntax to count by queues

Tan Le requested to merge tle-ruby-tally into master

What does this MR do?

Use a more succinct syntax to count by queues.

https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-tally

Benchmark

Let's have a look at the difference between the old and new implementation.

require "benchmark/ips"

ARRAY = ["a", "a", "b", "b", "c", "a"]

def fast
  ARRAY.tally
end

def slow
  ARRAY.each_with_object(Hash.new(0)) { |item, hash| hash[item] += 1 }
end

Benchmark.ips do |x|
  x.report("fast code description") { fast }
  x.report("slow code description") { slow }
  x.compare!
end
fast code description
                        19.331k i/100ms
slow code description
                        12.886k i/100ms
-------------------------------------------------
fast code description
                        268.880k (± 4.2%) i/s -      1.353M
slow code description
                        161.221k (± 3.7%) i/s -    811.818k

Comparison:
fast code description:   268879.9 i/s
slow code description:   161221.2 i/s - 1.67x slower

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • [-] Label as security and @ mention @gitlab-com/gl-security/appsec
  • [-] The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • [-] Security reports checked/validated by a reviewer from the AppSec team
Edited by Tan Le

Merge request reports

Loading