Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor I18n.normalize_keys improvement #616

Merged
merged 1 commit into from Feb 3, 2022

Conversation

codealchemy
Copy link
Contributor

Minor performance bump, as well as fewer objects allocated, when using * v. concat for normalizing keys -

📝 Last updated in 6425f55

Measurements
# Ruby 2.7.5

require 'benchmark/ips'

report = Benchmark.ips do |x|
  x.report("concat") do
    ex = []
    ex.concat ['foo']
    ex.concat ['bar', 'baz']
    ex.concat ['quix']
    ex
  end
  x.report("splat") do
    [
      *['foo'],
      *['bar', 'baz'],
      *['quix']
    ]
  end
  x.compare!
Warming up --------------------------------------
              concat   185.470k i/100ms
               splat   467.395k i/100ms
Calculating -------------------------------------
              concat      2.176M (± 1.5%) i/s -     10.943M in   5.030233s
               splat      4.678M (± 2.0%) i/s -     23.837M in   5.098138s

Comparison:
               splat:  4677698.6 i/s
              concat:  2175917.9 i/s - 2.15x  (± 0.00) slower
require 'memory_profiler'
n = 100

report1 = MemoryProfiler.report do
  n.times do
    ex = []
    ex.concat ['foo']
    ex.concat ['bar', 'baz']
    ex.concat ['quix']
    ex
  end
end

report1.pretty_print(scale_bytes: true, detailed_report: false)
# Total allocated: 48.00 kB (800 objects)
# Total retained:  0 B (0 objects)

report2 = MemoryProfiler.report do
  n.times do
    [
      *['foo'],
      *['bar', 'baz'],
      *['quix']
    ]
  end
end

report2.pretty_print(scale_bytes: true, detailed_report: false)
# Total allocated: 23.20 kB (500 objects)
# Total retained:  0 B (0 objects)

This results in a performance increase, and reduced object allocation, when normalizing keys -

```ruby
report = Benchmark.ips do |x|
  x.report("concat") do
    ex = []
    ex.concat ['foo']
    ex.concat ['bar', 'baz']
    ex.concat ['quix']
    ex
  end
  x.report("splat") do
    [
      *['foo'],
      *['bar', 'baz'],
      *['quix']
    ]
  end
  x.compare!
Warming up --------------------------------------
              concat   129.103k i/100ms
               splat   243.963k i/100ms
Calculating -------------------------------------
              concat      1.176M (± 9.7%) i/s -      5.810M in   5.009251s
               splat      2.435M (±11.8%) i/s -     11.954M in   5.019257s

Comparison:
               splat:  2434746.6 i/s
              concat:  1176432.3 i/s - 2.07x  (± 0.00) slower
```
@radar radar merged commit 00fc810 into ruby-i18n:master Feb 3, 2022
@radar
Copy link
Collaborator

radar commented Feb 3, 2022

Thank you!

@codealchemy codealchemy deleted the normalize-keys/splat branch February 3, 2022 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants