-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Faster and more readable implementation of Hash#deep_merge #30275
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
Can you use benchmark-ips in your benchmark? |
def deep_merge!(other, &block) | ||
merge!(other) do |_key, old_val, new_val| | ||
if old_val.is_a?(Hash) && new_val.is_a?(Hash) | ||
old_val.dup.deep_merge!(new_val, &block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not call deep_merge
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I'll fix that in a new commit
Fixed that LOC per your comment and changed benchmarks to |
|
||
self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash) | ||
this_value.deep_merge(other_value, &block) | ||
merge!(other_hash) do |_key, this_val, other_val| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be key
, not _key
since it's used in L28.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you squash down your commits?
I ran the benchmark on my machine (Ruby 2.2), and here are the numbers I got:
Which is still an improvement. 👍 Also, a while back, I wrote a gem (benchmark-inputs) to help with benchmarking multiple use cases, particularly when mutation is involved. Using it to benchmark the bang method, here are the numbers I got:
And here's the script to reproduce that: require 'active_support'
require 'benchmark/inputs'
class Hash
def new_deep_merge(other_hash, &block)
dup.new_deep_merge!(other_hash, &block)
end
def new_deep_merge!(other, &block)
merge!(other) do |key, old_val, new_val|
if old_val.is_a?(Hash) && new_val.is_a?(Hash)
old_val.new_deep_merge(new_val, &block)
elsif block_given?
block.call(key, old_val, new_val)
else
new_val
end
end
end
end
HASHES = [
{ a: false, b: { c: [3, 4, 5] }, d: 2 },
{ a: false, b: { x: [3, 4, 5] }, e: 2, f: { y: false } },
{ e: 2, f: { y: false }, g: 'hi', h: true },
]
OTHER = { a: true, b: { c: [1, 2, 3] }, d: 1 }
Benchmark.inputs(HASHES) do |job|
job.dup_inputs = true
job.report('OLD deep_merge!'){|h| h.deep_merge!(OTHER) }
job.report('NEW deep_merge!'){|h| h.new_deep_merge!(OTHER) }
job.compare!
end |
add missing newline call #deep_merge instead of #dup.deep_merge! make variable and parameter naming more consistent change `_key` to `key` faster implementation of Hash#deep_merge
Rebased and squashed. Thanks @lugray ! |
Summary
This is a re-implementation of
Hash#deep_merge
. Using Ruby's built-in optional block formerge
to handle duplicate keys makes the code shorter and more readable, and since the block is only run when a duplicate key is encountered it also improves performance. Performance benefits fluctuate case by case and are felt more strongly when merging hashes with fewer duplicate keys. All tests pass after this change.Benchmark (MacOS 10.12.6, ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16])
In a gist