aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/object
Commit message (Collapse)AuthorAgeFilesLines
* Implement UnboundMethod#duplicable?Jean Boussier2019-07-121-1/+10
|
* Remove dead code in duplicable.rbJean Boussier2019-07-111-120/+1
|
* 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.
* Revert falsey with example reorderedTakumasa Ochi2018-09-141-2/+2
|
* Improve API document on Object#blank?Takumasa Ochi2018-09-061-2/+2
| | | | | * Introduce `falsey` to represent both `nil` and `false`. * Keep consistent order between abstract description and examples.
* 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)
* Remove unused `require "active_support/core_ext/regexp"`Ryuta Kamizono2018-07-291-1/+0
| | | | | | | | Ruby 2.4 has native `Regexp#match?`. https://ruby-doc.org/core-2.4.0/Regexp.html#method-i-match-3F Related #32034.
* Update with_options.rbDaniel Amireh2018-07-251-1/+1
| | | explicit mapping for enum accepts a Hash not an Array, plus the example is using `.keys` which also exists on hash
* e4e1b62 broke `to_param` handling:Edouard CHIN2018-07-121-2/+5
| | | | | | | | | | | | | | | | | | | - There was an issue inside controller tests where order params were not respected, the reason was because we were calling `Hash#to_query` which sorts the results lexicographically. 1e4e1b62 fixed that issue by not using `to_query` but instead a utility function provided by rack. - However with the fix came another issue where it's now no longer possible to do this ``` post :foo, params: { user: User.first } # Prior to the patch the controller will receive { "user" => "1" } # Whereas now you get { "user": "#<User: ...>" } ``` The fix in this PR is to modify `Hash#to_query` to sort only when it doesn't contain an array structure that looks something like "bar[]" Ref https://github.com/rails/rails/pull/33341#issuecomment-404039396
* [ci skip] Hide internal docs from root AS moduleAshe Connor2018-03-061-0/+1
| | | | | | | | | We have a bunch of documentation in lib/active_support/core_ext/object/json.rb which is currently appearing as documentation for the top-level ActiveSupport module. We hide it from rdoc here. Signed-off-by: Ashe Connor <ashe@kivikakk.ee>
* Rails 6 requires Ruby 2.4.1+Jeremy Daer2018-02-171-2/+5
| | | | | | Skipping over 2.4.0 to sidestep the `"symbol_from_string".to_sym.dup` bug. References #32028
* Merge pull request #31049 from gwincr11/cg-blankRyuta Kamizono2018-01-041-1/+10
|\ | | | | Add support for multiple encodings in String.blank?
| * Add support for multiple encodings in String.blank?Cory Gwin @gwincr112017-11-171-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Motivation: - When strings are encoded with `.encode("UTF-16LE")` `.blank?` throws an `Encoding::CompatibilityError` exception. - We tested multiple implementation to see what the fastest implementation was, rescueing the execption seems to be the fastest option we could find. Related Issues: - #28953 Changes: - Add a rescue to catch the exception. - Added a `Concurrent::Map` to store a cache of encoded regex objects for requested encoding types. - Use the new `Concurrent::Map` cache to return the correct regex for the string being checked.
* | Suppress `warning: BigDecimal.new is deprecated`Yasuo Honda2017-12-151-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `BigDecimal.new` has been deprecated in BigDecimal 1.3.3 which will be a default for Ruby 2.5. Refer https://github.com/ruby/bigdecimal/commit/533737338db915b00dc7168c3602e4b462b23503 * This commit has been made as follows: ``` cd rails git grep -l BigDecimal.new | grep -v guides/source/5_0_release_notes.md | grep -v activesupport/test/xml_mini_test.rb | xargs sed -i -e "s/BigDecimal.new/BigDecimal/g" ``` - `activesupport/test/xml_mini_test.rb` Editmanually to remove `.new` and `::` - guides/source/5_0_release_notes.md This is a Rails 5.0 release notes.
* | Update incorrect backtick usage in RDoc to teletypeT.J. Schuck2017-11-221-1/+1
|/ | | [ci skip]
* Merge pull request #30961 from q-centrix/performance-improvement-acts-likeRafael França2017-10-231-1/+10
|\ | | | | Performance improvements for acts_like? method
| * Performance improvements for acts_like? method.Dillon Welch2017-10-231-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | activesupport/lib/active_support/core_ext/object/acts_like.rb acts_like? Add a case statement to use direct symbols instead of string interpolation for the three scenarios I found in the Rails codebase: time, date, and string. For time/date/string, this change prevents two string allocations for each time the method is called and speeds up the method by ~2.7x. For other arguments, there is no memory difference and performance difference is within margin of error. begin require "bundler/inline" rescue LoadError => e $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" raise e end gemfile(true) do source "https://rubygems.org" gem "rails", github: "rails/rails" gem "arel", github: "rails/arel" gem "benchmark-ips" end def allocate_count GC.disable before = ObjectSpace.count_objects yield after = ObjectSpace.count_objects after.each { |k,v| after[k] = v - before[k] } after[:T_HASH] -= 1 # probe effect - we created the before hash. GC.enable result = after.reject { |k,v| v == 0 } GC.start result end class Object def fast_acts_like?(duck) case duck when :time respond_to? :acts_like_time? when :date respond_to? :acts_like_date? when :string respond_to? :acts_like_string? else respond_to? :"acts_like_#{duck}?" end end end puts puts " acts_like? ".center(80, '=') puts obj = ''.freeze %i(time date string super_hacka).each do |type| puts " #{type} ".center(80, '=') puts " Memory Usage ".center(80, "=") puts puts "value.acts_like?" puts allocate_count { 1000.times { obj.acts_like?(type) } } puts "value.fast_acts_like?" puts allocate_count { 1000.times { obj.fast_acts_like?(type) } } puts puts " Benchmark.ips ".center(80, "=") puts Benchmark.ips do |x| x.report("acts_like?") { obj.acts_like?(type) } x.report("fast_acts_like?") { obj.fast_acts_like?(type) } x.compare! end end ================================== acts_like? ================================== ===================================== time ===================================== ================================= Memory Usage ================================= value.acts_like? {:FREE=>-1983, :T_STRING=>2052, :T_IMEMO=>1} value.fast_acts_like? {:FREE=>-1} ================================ Benchmark.ips ================================= Warming up -------------------------------------- acts_like? 104.281k i/100ms fast_acts_like? 155.523k i/100ms Calculating ------------------------------------- acts_like? 1.688M (±10.7%) i/s - 8.342M in 5.003804s fast_acts_like? 4.596M (±12.1%) i/s - 22.551M in 5.000124s Comparison: fast_acts_like?: 4596162.4 i/s acts_like?: 1688163.8 i/s - 2.72x slower ===================================== date ===================================== ================================= Memory Usage ================================= value.acts_like? {:FREE=>-2001, :T_STRING=>2000} value.fast_acts_like? {:FREE=>-1} ================================ Benchmark.ips ================================= Warming up -------------------------------------- acts_like? 85.372k i/100ms fast_acts_like? 166.097k i/100ms Calculating ------------------------------------- acts_like? 1.720M (± 8.3%) i/s - 8.537M in 5.001003s fast_acts_like? 4.695M (±10.1%) i/s - 23.254M in 5.010734s Comparison: fast_acts_like?: 4695493.1 i/s acts_like?: 1719637.9 i/s - 2.73x slower ==================================== string ==================================== ================================= Memory Usage ================================= value.acts_like? {:FREE=>-2001, :T_STRING=>2000} value.fast_acts_like? {:FREE=>-1} ================================ Benchmark.ips ================================= Warming up -------------------------------------- acts_like? 100.221k i/100ms fast_acts_like? 182.841k i/100ms Calculating ------------------------------------- acts_like? 1.706M (± 7.3%) i/s - 8.519M in 5.022331s fast_acts_like? 3.968M (±22.8%) i/s - 18.650M in 5.006762s Comparison: fast_acts_like?: 3967972.9 i/s acts_like?: 1705773.7 i/s - 2.33x slower ================================= super_hacka ================================== ================================= Memory Usage ================================= value.acts_like? {:FREE=>-2004, :T_STRING=>2002, :T_SYMBOL=>1} value.fast_acts_like? {:FREE=>-2003, :T_STRING=>2001, :T_SYMBOL=>1} ================================ Benchmark.ips ================================= Warming up -------------------------------------- acts_like? 100.344k i/100ms fast_acts_like? 101.690k i/100ms Calculating ------------------------------------- acts_like? 1.617M (± 7.5%) i/s - 8.128M in 5.055285s fast_acts_like? 1.534M (±10.1%) i/s - 7.627M in 5.031052s Comparison: acts_like?: 1617390.7 i/s fast_acts_like?: 1533897.3 i/s - same-ish: difference falls within error
* | Fix #to_json for unreadable IO objects, fixes #26132Paul Kuruvilla2017-10-231-0/+6
| |
* | [Active Support] require_relative => requireAkira Matsuda2017-10-216-15/+15
|/ | | | This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-1112-0/+12
|
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-0912-0/+12
|
* [Active Support] require => require_relativeAkira Matsuda2017-07-016-15/+15
|
* Fix doc format for `duplicable?` [ci skip]yuuji.yaginuma2017-03-251-6/+6
|
* Remove unnecessary ruby version comments [ci skip]Vipul A M2017-03-241-2/+2
|
* Fix duplicable? for Ratiional and Complex on ruby master, since they are now ↵Vipul A M2017-03-231-12/+22
| | | | duplicable
* Cleanup documentation fixes (#28460)Vipul A M2017-03-171-2/+2
|
* Add documentation to use with_options in the same class [ci skip]Dennis Zelada2017-03-161-0/+11
|
* Revert "Merge pull request #27686 from koic/friendly_bigdecimal_inspect"Kasper Timm Hansen2017-01-151-7/+0
| | | | | | | | | | | | | The exact inspect output of a BigDecimal is out of scope for what we're trying to communicate about `dup` and `duplicable?` here. Adding two examples distracts is disctracting, so keep the docs from before since our minimal version is Ruby 2.2.2. [ Koichi ITO, Jon Moss, Kasper Timm Hansen ] This reverts commit 2163874dedaf83e67599c2930c2686caa165fbad, reversing changes made to 46fdbc5290335ed38fa9fe2b6b0ef8abe4eccb1b.
* Several representation of BigDecimal has changed in Ruby 2.4.0+ [ci skip]Koichi ITO2017-01-151-0/+7
| | | | cf. https://github.com/ruby/bigdecimal/pull/42
* Fix style guide violationsRafael Mendonça França2017-01-051-1/+1
|
* Fix Symbol#duplicable? for Ruby 2.4.0.Kasper Timm Hansen2017-01-011-1/+2
| | | | | | | | | | | | | Ruby 2.4.0 has trouble duplicating certain symbols created from strings via `to_sym`. It didn't happen with `'symbol'.to_sym.dup` for some reason, but works fine with the longer string sample. Once a newer Ruby version with a fix is released we'll get have a failing test case we can fix. Ref: #27532
* Fix Complex and Rational are duplicable?utilum2016-12-211-0/+20
| | | | See [this test](https://gist.github.com/utilum/78918f1b64f8b61ee732cb266db7c43a).
* change return value of `duplicable?` with Ruby 2.4+yuuji.yaginuma2016-12-131-33/+58
| | | | | | | `NilClass`, `FalseClass`, `TrueClass`, `Symbol` and `Numeric` can dup with Ruby 2.4+. Ref: https://bugs.ruby-lang.org/issues/12979
* code gardening: removes redundant selfsXavier Noria2016-08-081-1/+1
| | | | | | | | | A few have been left for aesthetic reasons, but have made a pass and removed most of them. Note that if the method `foo` returns an array, `foo << 1` is a regular push, nothing to do with assignments, so no self required.
* modernizes hash syntax in activesupportXavier Noria2016-08-061-1/+1
|
* applies new string literal convention in activesupport/libXavier Noria2016-08-069-27/+27
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Revert "Adds `not_in?` onto Object"David Heinemeier Hansson2016-07-291-15/+0
|
* Merge pull request #25914 from jmccartie/jm/not_inRafael França2016-07-221-0/+15
|\ | | | | Adds `not_in?` onto Object
| * Adds `not_in?` onto ObjectJon McCartie2016-07-211-0/+15
| |
* | adds require for Regexp#match?Xavier Noria2016-07-221-0/+2
|/
* performance boost for String#blank? in Ruby 2.4Xavier Noria2016-07-221-1/+1
| | | | | | Some casual benchmarks showed a 2x factor. All credit goes to @nurse.
* Define `Pathname#as_json`Ryunosuke Sato2016-06-251-0/+7
| | | | | | | | | | | | | | | | | When the Pathname object is converted as JSON, it should be a string that means itself. Expected: ``` >> Pathname.new('/path/to/somewhere.txt').as_json "/path/to/somewhere.txt" ``` Actual: ``` >> Pathname.new('/path/to/somewhere.txt').as_json {"path"=>"/path/to/somewhere.txt"} ```
* Define `URI::Generic#as_json`Ryunosuke Sato2016-06-251-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When the URI object is converted as JSON, it is expected that it is a string that means its URI. Expected: ``` >> URI.parse('http://example.com').as_json "http://example.com" ``` Actual: ``` >> URI.parse('http://example.com').as_json {"scheme"=>"http", "user"=>nil, "password"=>nil, "host"=>"example.com", "port"=>80, "path"=>"", "query"=>nil, "opaque"=>nil, "fragment"=>nil, "parser"=> {"regexp"=> {"SCHEME"=>"(?-mix:\\A[A-Za-z][A-Za-z0-9+\\-.]*\\z)", "USERINFO"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=A-Z_a-z~])*\\z)", "HOST"=> "(?-mix:\\A(?:(?<IP-literal>\\[(?:(?<IPv6address>(?:\\h{1,4}:){6}(?<ls32>\\h{1,4}:\\h{1,4}|(?<IPv4address>(?<dec-octet>[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]|\\d)\\.\\g<dec-octet>\\.\\g<dec-octet>\\.\\g<dec-octet>))|::(?:\\h{1,4}:){5}\\g<ls32>|\\h{,4}::(?:\\h{1,4}:){4}\\g<ls32>|(?:(?:\\h{1,4}:)?\\h{1,4})?::(?:\\h{1,4}:){3}\\g<ls32>|(?:(?:\\h{1,4}:){,2}\\h{1,4})?::(?:\\h{1,4}:){2}\\g<ls32>|(?:(?:\\h{1,4}:){,3}\\h{1,4})?::\\h{1,4}:\\g<ls32>|(?:(?:\\h{1,4}:){,4}\\h{1,4})?::\\g<ls32>|(?:(?:\\h{1,4}:){,5}\\h{1,4})?::\\h{1,4}|(?:(?:\\h{1,4}:){,6}\\h{1,4})?::)|(?<IPvFuture>v\\h+\\.[!$&-.0-;=A-Z_a-z~]+))\\])|\\g<IPv4address>|(?<reg-name>(?:%\\h\\h|[!$&-.0-9;=A-Z_a-z~])*))\\z)", "ABS_PATH"=> "(?-mix:\\A\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*(?:\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*)*\\z)", "REL_PATH"=> "(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])+(?:\\/(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~])*)*\\z)", "QUERY"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~\\/?])*\\z)", "FRAGMENT"=>"(?-mix:\\A(?:%\\h\\h|[!$&-.0-;=@-Z_a-z~\\/?])*\\z)", "OPAQUE"=>"(?-mix:\\A(?:[^\\/].*)?\\z)", "PORT"=> "(?-mix:\\A[\\x09\\x0a\\x0c\\x0d ]*\\d*[\\x09\\x0a\\x0c\\x0d ]*\\z)"}}} ```
* Support for unified Integer class in Ruby 2.4+Jeremy Daer2016-05-182-2/+2
| | | | | | | | 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.
* restores the regexp used in String#blank?Xavier Noria2016-04-291-4/+3
| | | | | | | | | | This commit undoes 54243fe. Reason: Further investigation has shown the benefit is not so clear generally speaking. There is a long discussion and several benchmarks in the PR #24658 if you are interested in the details.
* rewords code comment [ci skip]Xavier Noria2016-04-241-3/+3
| | | | | | This alternative flows better. [Richard Schneeman & Xavier Noria]
* just say nothing about why this regexp is slower [ci skip]Xavier Noria2016-04-211-1/+1
| | | | | | | | | Further investigation seems to disprove that backtracking is the reason why the positive variant is slower, see https://github.com/rails/rails/pull/24658#issuecomment-213079710 so, just say nothing about it, only assert it is slower.
* restores code comments in String#blank? [ci skip]Xavier Noria2016-04-211-2/+6
| | | | | | | | | | | | When you come here without context, it is important to hightlight that checking the predicate is worthwhile due to the observation that blank strings are often empty. So you complicate the code (which has a cost in terms of readability and aesthetics), but statistically makes sense. Then, you also need to explain why the second operand is so convoluted. Otherwise, you wonder why this line is written precisely this way. That is what code comments are for.
* Remove unused `BLANK_RE`Ryuta Kamizono2016-04-211-2/+0
| | | | Follow up to #24658.