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

output: use Hash.new {|hash, key| ... } for default value #4764

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Watson1978
Copy link
Contributor

Which issue(s) this PR fixes:
Fixes #

What this PR does / why we need it:
This will remove evaluating meta_and_data[meta] ||= [] in the iterator every time to improves performance slightly.

The results of actually reading a 10 GB file are as follows.

  • Before: 67.50262675 sec
  • After: 63.862033033 sec

Here is micro benchmark code.

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips'
end

Benchmark.ips do |x|
  h1 = {}
  h2 = Hash.new { |h, k| h[k] = [] }
  key = :foo

  x.report("1") {
    h1[key] ||= []
    h1[key][0] = 1
  }

  x.report("2") {
    h2[key][0] = 1
  }

  x.compare!
end
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
                   1     1.776M i/100ms
                   2     2.239M i/100ms
Calculating -------------------------------------
                   1     18.426M (± 1.5%) i/s   (54.27 ns/i) -     92.366M in   5.013927s
                   2     23.545M (± 0.8%) i/s   (42.47 ns/i) -    118.653M in   5.039795s

Comparison:
                   2: 23544712.1 i/s
                   1: 18426072.0 i/s - 1.28x  slower

Docs Changes:

Release Note:

@Watson1978 Watson1978 requested a review from daipom January 9, 2025 08:43
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.

1 participant