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

HashWithIndifferentAccess#initialize performance improvement #34642

Conversation

Projects
None yet
2 participants
@azimux
Copy link
Contributor

commented Dec 6, 2018

Rails 4 -> Rails 5 introduced a #to_hash call in HashWithIndifferentAccess#initialize. I am assuming that the intention of this #to_hash call is to guarantee we have something that responds to :default and :default_proc. This can be an expensive operation for very large HashWithIndifferentAccess objects. This was introduced here: 6e574e8

This commit bypasses this #to_hash call if it is already a Hash, giving a performance boost to the copy constructor of huge HWIAs.

HashWithIndifferentAccess#initialize performance improvement
Rails 4 -> Rails 5 introduced a #to_hash call in
HashWithIndifferentAccess#initialize to guarantee access to
the #default and #default_proc methods.  This can be a very
expensive operation for very large HashWithIndifferentAccess
objects.  This commit bypasses this #to_hash call if it is
already a Hash.

@rails-bot rails-bot bot added the activesupport label Dec 6, 2018

@rafaelfranca

This comment has been minimized.

Copy link
Member

commented Dec 7, 2018

Can you provide number showing this indeed improves performance?

@azimux

This comment has been minimized.

Copy link
Contributor Author

commented Dec 7, 2018

Yeah, here's a quick/dirty benchmark program for it:

https://gist.github.com/azimux/be20b8bcc3c4b880c31d2df5e602e9b7

I get the following output:

miles@marlin:~/tmp$ ruby hwia_perf.rb 
  3.550000   0.320000   3.870000 (  3.865559)
  3.610000   0.000000   3.610000 (  3.609835)
  4.310000   0.010000   4.320000 (  4.327402)
  4.430000   0.060000   4.490000 (  4.496456)
  3.950000   0.000000   3.950000 (  3.953131)
miles@marlin:~/tmp$ APPLY_PATCH=true ruby hwia_perf.rb 
  1.440000   0.080000   1.520000 (  1.525470)
  1.560000   0.010000   1.570000 (  1.563784)
  1.460000   0.000000   1.460000 (  1.461005)
  1.490000   0.000000   1.490000 (  1.494797)
  1.420000   0.000000   1.420000 (  1.423135)
miles@marlin:~/tmp$ 
@azimux

This comment has been minimized.

Copy link
Contributor Author

commented May 1, 2019

bump!

@rafaelfranca rafaelfranca merged commit b8f88b9 into rails:master May 1, 2019

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

jonathanhefner pushed a commit to jonathanhefner/rails that referenced this pull request May 4, 2019

Merge pull request rails#34642 from azimux/improve-hwia-initialize-by…
…-skipping-to_h-if-already-a-hash

HashWithIndifferentAccess#initialize performance improvement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.