Hash / HashWithIndifferentAccess speed improvements#35771
Conversation
|
Thank you for the pull request. For future reference, can you show the numbers on how fast this is comparing with the previous implementation? |
|
Some memory profiling would be interesting to see too |
|
@rafaelfranca I updated the summary |
1871542 to
90e9004
Compare
|
https://github.com/SamSaffron/memory_profiler is one way to generate those reports. Especially this new version seems like it generates lots of intermediate objects. |
|
@kaspth Do you think that because of |
|
I'm thinking about memory because |
|
I added the profile results based on the test methods from the benchmark. |
|
HashWithIndifferentAccess tests seems to be broken. |
|
I will take a look on that in the next days |
90e9004 to
4bffa41
Compare
4bffa41 to
9a2bbd0
Compare
9a2bbd0 to
a805d72
Compare
|
I fixed the specs and add a optimisation for |
491401e to
3fbad17
Compare
There was a problem hiding this comment.
It should be safe to call map! here, due to the splat on indices (i.e. it will be a newly-allocated array).
There was a problem hiding this comment.
Yes you can safe one object allocation, but map! needs significant more time.
There was a problem hiding this comment.
What makes you say that? On my machine map! is consistently 10% faster than map.
There was a problem hiding this comment.
Same as previous comment:
It should be safe to call map! here, due to the splat on keys.
3fbad17 to
9f41edb
Compare
This PR supports `Thor::CoreExt::HashWithIndifferentAccess#except`
and prevents breaking changes in Rails upgrades when using
`option.except(:key)` in Thor task.
When Thor is used with Rails (Active Support), the behavior changes as follows.
## With Rails 5.2 or lower
```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"bar"=>2}
```
## With Rails 6.0
```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"foo"=>1, "bar"=>2}
```
This difference behavior is due to the following changes in Rails 6.0.
rails/rails#35771
This PR makes the behavior between Rails 5.2 and Rails 6.0 compatible.
This PR supports `Thor::CoreExt::HashWithIndifferentAccess#except`
and prevents breaking changes in Rails upgrades when using
`options.except(:key)` in Thor task.
When Thor is used with Rails (Active Support), the behavior changes as follows.
## With Rails 5.2 or lower
```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"bar"=>2}
```
## With Rails 6.0
```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"foo"=>1, "bar"=>2}
```
This difference behavior is due to the following changes in Rails 6.0.
rails/rails#35771
This PR makes the behavior between Rails 5.2 and Rails 6.0 compatible.
This PR supports `Thor::CoreExt::HashWithIndifferentAccess#except`
and prevents breaking changes in Rails upgrades when using
`options.except(:key)` in Thor task.
When Thor is used with Rails (Active Support), the behavior changes as follows.
## With Rails 5.2 or lower
```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"bar"=>2}
```
## With Rails 6.0
```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"foo"=>1, "bar"=>2}
```
This difference behavior is due to the following changes in Rails 6.0.
rails/rails#35771
This PR makes the behavior between Rails 5.2 and Rails 6.0 compatible.
This PR supports `Thor::CoreExt::HashWithIndifferentAccess#except`
and prevents breaking changes in Rails upgrades when using
`options.except(:key)` in Thor task.
When Thor is used with Rails (Active Support), the behavior changes as follows.
## With Rails 5.2 or lower
```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"bar"=>2}
```
## With Rails 6.0
```ruby
h = Thor::CoreExt::HashWithIndifferentAccess.new(foo: 1, bar: 2)
h.except(:foo) #=> {"foo"=>1, "bar"=>2}
```
This difference behavior is due to the following changes in Rails 6.0.
rails/rails#35771
This PR makes the behavior between Rails 5.2 and Rails 6.0 compatible.
Summary
This changes improves the speed of
Hash#except,HashWithIndifferentAccess#except,HashWithIndifferentAccess#values_atandHashWithIndifferentAccess#fetch_valuesby using ruby native methods.Benchmarks
Source