aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/object/try.rb
Commit message (Collapse)AuthorAgeFilesLines
* This PR speeds up Nil#try by avoiding an allocation when only one argument ↵schneems2018-10-031-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* 20% faster `try`Sean Griffin2018-08-291-8/+7
| | | | | | | | | | | | | | | | | | | | | | | | Following up on #33747, this takes things a step further by pulling out the method name from the arguments array, letting us skip an allocation in the case where there are no arguments -- notably, this also no longer *requires* the splat to be an array, allowing us to benefit from optimizations in Jruby (and maybe MRI in the future) of skipping the array allocation entirely. Benchmark results: ``` Warming up -------------------------------------- old 179.987k i/100ms new 199.201k i/100ms Calculating ------------------------------------- old 3.029M (± 1.6%) i/s - 15.299M in 5.052417s new 3.657M (± 1.2%) i/s - 18.326M in 5.012648s Comparison: new: 3656620.7 i/s old: 3028848.3 i/s - 1.21x slower ```
* 32% Faster Object#tryschneems2018-08-291-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Here’s the micro benchmark: ```ruby module ActiveSupport module NewTryable #:nodoc: def try(*a, &b) return unless a.empty? || respond_to?(a.first) return public_send(*a, &b) unless a.empty? return nil unless block_given? return instance_eval(&b) if b.arity == 0 yield self end def try!(*a, &b) return public_send(*a, &b) if !a.empty? return nil unless block_given? return instance_eval(&b) if b.arity == 0 yield self end end end module ActiveSupport module OldTryable #:nodoc: def try(*a, &b) try!(*a, &b) if a.empty? || respond_to?(a.first) end def try!(*a, &b) if a.empty? && block_given? if b.arity == 0 instance_eval(&b) else yield self end else public_send(*a, &b) end end end end class FooNew include ActiveSupport::NewTryable def foo end end class FooOld include ActiveSupport::OldTryable def foo end end foo_new = FooNew.new foo_old = FooOld.new require 'benchmark/ips' Benchmark.ips do |x| x.report("old") { foo_old.try(:foo) } x.report("new") { foo_new.try(:foo) } x.compare! end # Warming up -------------------------------------- # old 144.178k i/100ms # new 172.371k i/100ms # Calculating ------------------------------------- # old 2.181M (± 8.0%) i/s - 10.813M in 5.001419s # new 2.889M (± 7.7%) i/s - 14.479M in 5.051760s # Comparison: # new: 2888691.7 i/s # old: 2180740.7 i/s - 1.32x slower ``` Also reduces memory. On https://www.codetriage.com i’m seeing 1.5% fewer object allocations per request (in object count). Before: Total allocated: 1014475 bytes (8525 objects) After: Total allocated: 1015499 bytes (8389 objects)
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-111-0/+1
|
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-091-0/+1
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* applies new string literal convention in activesupport/libXavier Noria2016-08-061-1/+1
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Support for unified Integer class in Ruby 2.4+Jeremy Daer2016-05-181-1/+1
| | | | | | | | Ruby 2.4 unifies Fixnum and Bignum into Integer: https://bugs.ruby-lang.org/issues/12005 * Forward compat with new unified Integer class in Ruby 2.4+. * Backward compat with separate Fixnum/Bignum in Ruby 2.2 & 2.3. * Drops needless Fixnum distinction in docs, preferring Integer.
* Improve readability of docs by using code tag [ci skip]amitkumarsuroliya2015-09-241-1/+1
|
* Use == 0 instead of .zero? in #tryJean Boussier2015-08-171-1/+1
| | | | | | | | | | | | | | | | | | | The perf gain is relatively minor but consistent: ``` Calculating ------------------------------------- 0.zero? 137.091k i/100ms 1.zero? 137.350k i/100ms 0 == 0 142.207k i/100ms 1 == 0 144.724k i/100ms ------------------------------------------------- 0.zero? 8.893M (± 6.5%) i/s - 44.280M 1.zero? 8.751M (± 6.4%) i/s - 43.677M 0 == 0 10.033M (± 7.0%) i/s - 49.915M 1 == 0 9.814M (± 8.0%) i/s - 48.772M ``` And try! is quite a big hotspot for us so every little gain is appreciable.
* Small stylistic tweaks for `Delegator#try` patchGodfrey Chan2015-05-191-5/+5
| | | | | | * Rename `ActiveSupport::Try` => `ActiveSupport::Tryable` * Include the modules inline * `private` indentation
* Patch `Delegator` to work with `#try`Nate Smith2015-05-191-17/+57
| | | | | | | | | | | | `Delegator` inherits from `BasicObject`, which means that it will not have `Object#try` defined. It will then delegate the call to the underlying object, which will not (necessarily) respond to the method defined in the enclosing `Delegator`. This patches `Delegator` with the `#try` method to work around the surprising behaviour. Fixes #5790
* Fix a few typos [ci skip]Robin Dupret2015-01-031-1/+1
|
* Add docs for `Object.nil!`claudiob2014-12-221-3/+9
| | | | | | Also add doc examples for `Object.nil`. [ci skip]
* Replace `#=>` with `# =>` [ci skip]claudiob2014-12-171-2/+2
| | | | | | | | @rafaelfranca suggested in f7c7bcd9 that code examples should display the result after `# =>` and not after `#=>`. This commit replaces *all* the occurrences of `#=>` in the code documentation (mostly added by me :sob:) with the suggested `# =>`.
* Revert a change made to the example in 1ac4525Godfrey Chan2014-10-241-1/+1
| | | | | | | @carlosantoniodasilva pointed out that when `@person` is nil then this would blow up when you ended up calling `#first`on `nil`. > "there’s no way to break a try chain when you enter it :D" [ci skip]
* Include return value in examples added in #17378 [ci skip]Zachary Scott2014-10-241-2/+2
|
* Improved try documentation [ci skip]Eugene Gilburg2014-10-241-8/+25
| | | | | | | | | | - better `if` example - Added chaining example to the try method description - Documented the `respond_to?` check to the try method description - Clearer wording to explain that argument error is raised on argument mismatch to responding method, rather than to non-responding method (which is handled without exception by `try`) - `.any?` is more precise than `! .blank?` - Don't need to use `try` on `children` as (for regular associations) they will always be a collection or array that responds to `first` - Fix typos/grammar
* DRY up try/try!Ari Pollak2014-10-231-10/+6
|
* Bring try! into parity with try.Ari Pollak2014-10-221-1/+1
| | | | Based on commit 5e51bdda.
* We tenderized the wrong method! Object#try already had the yield option, ↵David Heinemeier Hansson2014-08-291-1/+10
| | | | just needed some tenderloving instance_eval to fit the bill
* Fixes typo in Object#try!Jay Hayes2013-09-101-1/+1
|
* Revert "Merge branch 'master' of github.com:rails/docrails"Vijay Dev2013-08-171-1/+1
| | | | | | | This reverts commit 70d6e16fbad75b89dd1798ed697e7732b8606fa3, reversing changes made to ea4db3bc078fb3093ecdddffdf4f2f4ff3e1e8f9. Seems to be a code merge done by mistake.
* Fixes typo in Object#try!Jay Hayes2013-07-261-1/+1
|
* revamps the RDoc of Object#tryXavier Noria2013-01-281-22/+30
|
* Bring back changelog entries for Active SupportCarlos Antonio da Silva2012-08-111-2/+2
| | | | Removed in 0228a73b1094a3e19ad291d2ce4789890c09578a, pull request #7310.
* Add Object#try! with the old NoMethodError raising behaviorDavid Heinemeier Hansson2012-07-271-0/+14
|
* will now return nil instead of raise a NoMethodError if the receiving ↵David Heinemeier Hansson2012-07-271-1/+4
| | | | object does not implement the method
* remove double hyphen that doesn't allow properly parsingFrancesco Rodriguez2012-06-041-5/+1
|
* Improve docs for `try` by adding note on `BasicObject`Piotr Sarnacki2012-05-201-0/+4
| | | | [ci skip] closes #5790
* Update docs to public_send for Object#tryOscar Del Ben2012-05-131-1/+1
|
* update docs on Object#tryVasiliy Ermolovich2012-05-131-1/+1
|
* Object#try can't call private methodsVasiliy Ermolovich2012-05-121-2/+2
|
* Revert "don't raise NoMethodError the tried method doesn't exists"José Valim2011-10-061-2/+0
| | | | This reverts commit 29a5aeaae976bf8432d57ec996c7c81932a39de6.
* don't raise NoMethodError the tried method doesn't existsdmathieu2011-05-211-0/+2
|
* NilClass is a singletonAkira Matsuda2011-04-201-1/+1
|
* oops fixed typoSebastian Martinez2011-04-181-1/+1
|
* Fixed docs for NilClass#trySebastian Martinez2011-04-181-8/+8
|
* Formatting docsSebastian Martinez2011-04-171-1/+1
|
* Docs for NilClass#trySebastian Martinez2011-04-171-1/+13
|
* Active Support typos.R.T. Lechow2011-03-051-1/+1
|
* :method: is not needed when RDoc can detect itSantiago Pastorino2010-12-021-5/+0
|
* Add support for try to just yield the object to a block if no method is to ↵raggi2010-11-141-1/+13
| | | | be called. Kind of like a tap_if_present.
* Remove dummy method definition in favor of RDoc hints:Aaron Patterson2010-09-111-7/+5
| | | | http://rdoc.rubyforge.org/RDoc/Parser/Ruby.html
* try is an object extensionJeremy Kemper2009-03-211-0/+36