aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
Commit message (Collapse)AuthorAgeFilesLines
* Correct ArgumentError message in ActiveSupport conversions docs [ci skip]amitkumarsuroliya2015-09-181-1/+1
| | | This was happened after this commit https://github.com/rails/rails/commit/2ebf47aea21ff8ac10681e53e78dd7a0e5c31c6e
* Improving `in_time_zone` docs [ci skip]amitkumarsuroliya2015-09-131-2/+1
| | | `DateTime.utc` is not a valid method. It gives `NoMethodError: undefined method `utc` for DateTime:Class`. As we know that we can calculate `utc` time from `Time` Class, but we can’t calculate `utc` time from `DateTime` Class.
* Improve String#strip_heredocJuanito Fatas2015-09-121-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Saves about 6 MB, about 40% faster. **strip_heredoc.rb** ```ruby require "active_support/core_ext/object/try" require "get_process_mem" class String def strip_heredoc indent = scan(/^[ \t]*(?=\S)/).min.try(:size) || 0 gsub(/^[ \t]{#{indent}}/, '') end end if ENV["MEASURE_MEMORY"] == "yes" mem = GetProcessMem.new GC.start GC.disable 10000.times do <<-MSG.strip_heredoc xhr and xml_http_request methods are deprecated in favor of `get :index, xhr: true` and `post :create, xhr: true` MSG end before = mem.mb after = mem.mb GC.enable puts "Before: #{before} MiB" puts "After: #{after} MiB" puts "Diff: #{after - before} MiB" end ``` **patched_strip_heredoc.rb** ```ruby require "active_support/core_ext/object/try" require "get_process_mem" class String def patched_strip_heredoc gsub(/^#{scan(/^[ \t]*(?=\S)/).min}/, "".freeze) end end if ENV["MEASURE_MEMORY"] == "yes" mem = GetProcessMem.new GC.start GC.disable 10000.times do <<-MSG.patched_strip_heredoc xhr and xml_http_request methods are deprecated in favor of `get :index, xhr: true` and `post :create, xhr: true` MSG end before = mem.mb after = mem.mb GC.enable puts "Before: #{before} MiB" puts "After: #{after} MiB" puts "Diff: #{after - before} MiB" end ``` **Before** ``` $ MEASURE_MEMORY=yes ruby strip_heredoc.rb Before: 44.73828125 MiB After: 44.7734375 MiB Diff: 0.03515625 MiB ``` **After** ``` $ MEASURE_MEMORY=yes ruby patched_strip_heredoc.rb Before: 37.9765625 MiB After: 38.015625 MiB Diff: 0.0390625 MiB ``` `44.7734375 - 38.015625 = 6.75` => **Saves about 6.75 MiB** **benchmark.rb** ```ruby require "benchmark/ips" require_relative "./strip_heredoc" require_relative "./patched_strip_heredoc" def original <<-MSG.strip_heredoc xhr and xml_http_request methods are deprecated in favor of `get :index, xhr: true` and `post :create, xhr: true` MSG end def patched <<-MSG.patched_strip_heredoc xhr and xml_http_request methods are deprecated in favor of `get :index, xhr: true` and `post :create, xhr: true` MSG end Benchmark.ips do |x| x.report("original") { original } x.report(" patched") { patched } x.compare! end ``` ``` $ ruby -v benchmark.rb ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14] Calculating ------------------------------------- original 5.652k i/100ms patched 6.477k i/100ms ------------------------------------------------- original 54.076k (± 5.7%) i/s - 271.296k patched 74.557k (± 6.2%) i/s - 375.666k Comparison: patched: 74557.0 i/s original: 54076.4 i/s - 1.38x slower ``` => **About 38% faster** 1. Clone rails project `git clone git@github.com:rails/rails.git` 2. Apply this patch to `activesupport/lib/active_support/core_ext/string/strip.rb` 3. `cd activesupport` 4. run `rake` 5. And tests passed: ``` ➜ activesupport $ rake /Users/Juan/.rubies/ruby-2.2.2/bin/ruby -w -I"lib:test" "/Users/Juan/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_test_loader.rb" "test/**/*_test.rb" ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ........................................................................ ......................................................................S. SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS Finished in 15.343004s, 214.2344 runs/s, 24902.4898 assertions/s. 3287 runs, 382079 assertions, 0 failures, 0 errors, 48 skips You have skipped tests. Run with --verbose for details. ```
* Rails documentation use american english.[ci skip]ravindra kumar kumawat2015-09-091-1/+1
|
* Fixed Time conversion example for UTC time zone [ci skip]Ronak Jangir2015-09-081-1/+1
|
* Merge pull request #20534 from qnm/activesupport-require-issueYves Senn2015-09-071-0/+1
|\ | | | | Add require to ensure Time#advance works without implicit required
| * Add missing require to ensure #advance will work without being implicity ↵Rob Sharp2015-06-121-0/+1
| | | | | | | | required
* | Removed Extra ‘the’ [ci skip]Ronak Jangir2015-09-061-1/+1
| |
* | docs, make `blank?` behavior clear. Closes #21468. [ci skip]Yves Senn2015-09-021-2/+2
| |
* | minor documentation improvement [ci skip]Sam Auciello2015-08-241-2/+2
| |
* | temp files are no more requireGaurav Sharma2015-08-251-2/+0
| | | | | | `:nail_care:`
* | Merge pull request #16245 from byroot/more-atomic-writeMatthew Draper2015-08-211-27/+29
|\ \ | | | | | | File renaming should be the last operation of an atomic write
| * | File renaming should be the last operation of an atomic writeJean Boussier2015-08-201-27/+29
| | |
* | | 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.
* | Merge pull request #21217 from myrridin/myrridin-documentation-updatesZachary Scott2015-08-121-2/+2
|\ \ | | | | | | [ci skip] Documentation: Switch around a common phrase for readability
| * | [ci skip] Switch around a common idiom for readabilityThomas Hart II2015-08-051-2/+2
| | |
* | | Only invoke the default block for mattr_accessor once so that it does not ↵Lachlan Sylvester2015-08-071-1/+1
| | | | | | | | | | | | cause issues if it is not idempotent
* | | Merge pull request #21087 from vngrs/fix_hash_except_docKasper Timm Hansen2015-08-031-8/+9
|\ \ \ | | | | | | | | Fix the documentation of Hash#except method [ci skip]
| * | | Fix the documentation of Hash#except method [ci skip]Mehmet Emin İNAÇ2015-08-031-8/+9
| | | | | | | | | | | | | | | | fix minor problems
* | | | [ci skip] Update Time#advance documentation with examplesMichael Stock2015-08-021-0/+6
|/ / /
* / / String#freeze optimizationsschneems2015-07-301-1/+1
|/ /
* | Tiny documentation edits [ci skip]Robin Dupret2015-07-281-1/+1
| |
* | Freeze string literals when not mutated.schneems2015-07-192-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I wrote a utility that helps find areas where you could optimize your program using a frozen string instead of a string literal, it's called [let_it_go](https://github.com/schneems/let_it_go). After going through the output and adding `.freeze` I was able to eliminate the creation of 1,114 string objects on EVERY request to [codetriage](codetriage.com). How does this impact execution? To look at memory: ```ruby require 'get_process_mem' mem = GetProcessMem.new GC.start GC.disable 1_114.times { " " } before = mem.mb after = mem.mb GC.enable puts "Diff: #{after - before} mb" ``` Creating 1,114 string objects results in `Diff: 0.03125 mb` of RAM allocated on every request. Or 1mb every 32 requests. To look at raw speed: ```ruby require 'benchmark/ips' number_of_objects_reduced = 1_114 Benchmark.ips do |x| x.report("freeze") { number_of_objects_reduced.times { " ".freeze } } x.report("no-freeze") { number_of_objects_reduced.times { " " } } end ``` We get the results ``` Calculating ------------------------------------- freeze 1.428k i/100ms no-freeze 609.000 i/100ms ------------------------------------------------- freeze 14.363k (± 8.5%) i/s - 71.400k no-freeze 6.084k (± 8.1%) i/s - 30.450k ``` Now we can do some maths: ```ruby ips = 6_226k # iterations / 1 second call_time_before = 1.0 / ips # seconds per iteration ips = 15_254 # iterations / 1 second call_time_after = 1.0 / ips # seconds per iteration diff = call_time_before - call_time_after number_of_objects_reduced * diff * 100 # => 0.4530373333993266 miliseconds saved per request ``` So we're shaving off 1 second of execution time for every 220 requests. Is this going to be an insane speed boost to any Rails app: nope. Should we merge it: yep. p.s. If you know of a method call that doesn't modify a string input such as [String#gsub](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37) please [give me a pull request to the appropriate file](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37), or open an issue in LetItGo so we can track and freeze more strings. Keep those strings Frozen ![](https://www.dropbox.com/s/z4dj9fdsv213r4v/let-it-go.gif?dl=1)
* | Merge pull request #20839 from ↵Sean Griffin2015-07-181-9/+34
|\ \ | | | | | | | | | | | | TheBlasfem/added_examples_dateandtime_calculations Added examples to DateAndTime::Calculations [ci skip]
| * | added examples to DateAndTime::Calculations [ci skip]Julio Lopez2015-07-181-9/+34
| | |
* | | [skip ci] Lookup can be a noun but it is not a verbJon Atack2015-07-171-1/+2
| | | | | | | | | | | | Various grammar corrections and wrap to 80 characters.
* | | improve duplicable documentation [ci skip]Julio Lopez2015-07-101-1/+5
|/ /
* | adding brackets to array in docsJulio Lopez2015-07-071-1/+1
| |
* | Merge pull request #20575 from prathamesh-sonpatki/doc-xml-disallowed-typesZachary Scott2015-06-181-1/+19
|\ \ | | | | | | Added documentation about passing custom disallowed types to Hash#from_xml [ci skip]
| * | Added documentation about passing custom disallowed types to Hash#from_xml ↵Prathamesh Sonpatki2015-06-181-1/+19
| |/ | | | | | | [ci skip]
* | Merge pull request #18365 from pocke/fix_datatime_compareAaron Patterson2015-06-121-1/+1
|\ \ | |/ |/| DateTime#<=> return nil when compare to the invalid String as Time.
| * DateTime#<=> return nil when compare to the invalid String as Time.pocke2015-01-061-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | before: p Time.now == 'a' # => false p Time.now <=> 'a' # => nil require 'active_support' require 'active_support/core_ext' p Time.now == 'a' # => false p Time.now <=> 'a' # => invalid date (ArgumentError) and on ruby 2.2, Time.now == 'a' warning. warning: Comparable#== will no more rescue exceptions of #<=> in the next release. warning: Return nil in #<=> if the comparison is inappropriate or avoid such comparison. after: - Error handling. - Quiet warnings.
* | Use block variable instead of globalRoque Pinel2015-06-081-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ```ruby Benchmark.ips do |x| x.report("$&") { "foo".gsub(/f/) { $&.hex } } x.report("block var") { "foo".gsub(/f/) { |match| match.hex } } end ``` ``` Calculating ------------------------------------- $& 23.271k i/100ms block var 24.804k i/100ms ------------------------------------------------- $& 321.981k (± 7.4%) i/s - 1.606M block var 324.949k (± 9.2%) i/s - 1.612M ```
* | Merge pull request #20362 from kddeisz/enumerable_pluckRafael Mendonça França2015-06-011-2/+9
|\ \ | | | | | | Allow Enumerable#pluck to take a splat.
| * | Allow Enumerable#pluck to take a splat.Kevin Deisz2015-05-291-2/+9
| | | | | | | | | | | | | | | | | | This allows easier integration with ActiveRecord, such that AR#pluck will now use Enumerable#pluck if the relation is loaded, without needing to hit the database.
* | | Fix a range of values for parameters of the Time#changeNikolay Kondratyev2015-06-011-1/+1
| | | | | | | | | | | | | | | Passing 999999000 < `:nsec` < 999999999 and 999999 < `:usec` < 1000000 to change a time with utc_offset doesn't throw an `ArgumentError`.
* | | Revert "Replace use of alias chains with prepend at core_ext/date and ↵Roque Pinel2015-05-295-79/+89
|/ / | | | | | | core_ext/time"
* | Merge pull request #19878 from pabloh/replace_alias_chains_with_prependRafael Mendonça França2015-05-285-89/+79
|\ \ | | | | | | Replace use of alias chains with prepend at core_ext/date and core_ext/time
| * | Replace use of alias chains with prepend at core_ext/datePablo Herrero2015-05-045-89/+79
| | |
* | | Add Enumerable#pluck.Kevin Deisz2015-05-281-0/+8
| | | | | | | | | | | | Allows fetching the same values from arrays as from ActiveRecord associations.
* | | Remove `.superclass_delegating_accessor`. Refer #14271Akshay Vishnoi2015-05-242-46/+0
| | |
* | | [skip ci] Update documentation for Date classAnton Davydov2015-05-211-0/+6
| | |
* | | 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
* | | Only define #positive? and #negative? on Ruby 2.2Rafael Mendonça França2015-05-191-0/+7
| | | | | | | | | | | | | | | | | | | | | The feature was accepted and added to Ruby 2.3+ so we don't need to define it again. See https://bugs.ruby-lang.org/issues/11151
* | | Move Integer#positive? and Integer#negative? query methods to Numeric classMehmet Emin İNAÇ2015-05-133-2/+2
| | | | | | | | | | | | By this way Integer, Rational, Float, Fixnum, Bignum classes have the same behaviour
* | | Minor comment fix. [ci skip]Zoltan Kiss2015-05-131-4/+4
| | |
* | | Add Integer#positive? and Integer#negative? query methods in the vein of ↵David Heinemeier Hansson2015-05-132-0/+20
| | | | | | | | | | | | Fixnum#zero?
* | | Merge pull request #20049 from iamvery/patch-1Yves Senn2015-05-071-1/+13
|\ \ \ | | | | | | | | | | | | Amend `next_week` documentation [ci skip]
| * | | Add examples of Date and Time `next_week` usageJay Hayes2015-05-071-0/+7
|/ / / | | | | | | | | | [skip ci]