diff options
author | schneems <richard.schneeman+foo@gmail.com> | 2018-10-03 21:00:26 -0500 |
---|---|---|
committer | schneems <richard.schneeman+foo@gmail.com> | 2018-10-03 21:01:27 -0500 |
commit | 81d43f6282e2582d70105365ba3aa56ba25d08b7 (patch) | |
tree | 2c5e1f6d4aea7aaf800e8802a1e666070e5ee869 /guides/source/generators.md | |
parent | bdd8d5898710e727c55b514804a221b6eddbda41 (diff) | |
download | rails-81d43f6282e2582d70105365ba3aa56ba25d08b7.tar.gz rails-81d43f6282e2582d70105365ba3aa56ba25d08b7.tar.bz2 rails-81d43f6282e2582d70105365ba3aa56ba25d08b7.zip |
This PR speeds up Nil#try by avoiding an allocation when only one argument is passed:
```ruby
class FooNew
def try(method_name = nil, *args)
nil
end
end
class FooOld
def try(*args)
nil
end
end
require 'benchmark/ips'
foo_new = FooNew.new
foo_old = FooOld.new
Benchmark.ips do |x|
x.report("new") { foo_new.try(:anything) }
x.report("old") { foo_old.try(:anything) }
x.compare!
end
# Warming up --------------------------------------
# new 250.633k i/100ms
# old 232.322k i/100ms
# Calculating -------------------------------------
# new 6.476M (± 4.8%) i/s - 32.332M in 5.005777s
# old 5.258M (± 3.2%) i/s - 26.485M in 5.042589s
# Comparison:
# new: 6476002.5 i/s
# old: 5257912.5 i/s - 1.23x slower
```
It's worth noting that checking for nil separately as in https://github.com/rails/rails/pull/34067 seems to be MUCH faster. It might be worth it to apply a blanket `&.` to every internal `try` call.
Diffstat (limited to 'guides/source/generators.md')
0 files changed, 0 insertions, 0 deletions