aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
Commit message (Collapse)AuthorAgeFilesLines
...
* | | | | | | Wrong usage of 'a' in docs fixed [ci skip]Mehmet Emin İNAÇ2015-10-031-1/+1
| | | | | | |
* | | | | | | Regex fix for mattr_accessor validationAliaksandr Buhayeu2015-10-011-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Change ^ and $ operators to \A and \z to prevent code injection after the line breaks
* | | | | | | Improve readability of docs by using code tag [ci skip]Prakash Laxkar2015-09-293-3/+3
| | | | | | |
* | | | | | | Merge pull request #21807 from ronakjangir47/fix_formatted_offset_docsRafael Mendonça França2015-09-292-1/+4
|\ \ \ \ \ \ \ | |_|_|_|_|_|/ |/| | | | | | Update docs for `formatted_offset`
| * | | | | | Update docs for `formatted_offset` Ronak Jangir2015-09-292-1/+4
| | | | | | | | | | | | | | | | | | | | | Output of `formatted_offset` is depends on input so it’s not always in +HH:MM format. Possible outputs are “+5:30”, “+530” or provided alternate UTC string [ci skip]
* | | | | | | Merge pull request #21782 from ronakjangir47/transform_values_docsRafael Mendonça França2015-09-282-8/+17
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Updated docs for transform_keys & transform_values
| * | | | | | | Updated docs for transform_values [ci skip]Ronak Jangir2015-09-281-3/+8
| | | | | | | |
| * | | | | | | Updated docs for transform_keys [ci skip]Ronak Jangir2015-09-281-5/+9
| |/ / / / / /
* / / / / / / Speed up `Hash#transform_values` when emptySean Griffin2015-09-281-0/+1
|/ / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We're calling this function on an empty hash as part of copying the attribute set during dirty checking initialization. The new structure caused a performance regression on loading records from the database. This causes `User.all.to_a` to perform about 10% faster w/ 10k records. Calculating ------------------------------------- User.all - master 9.000 i/100ms User.all - sg-fix-ar-regression 8.000 i/100ms ------------------------------------------------- User.all - master 81.236 (± 7.4%) i/s - 405.000 User.all - sg-fix-ar-regression 89.716 (± 7.8%) i/s - 448.000
* | | | | | Improve readability of docs by using code tag [ci skip]amitkumarsuroliya2015-09-242-3/+3
| | | | | |
* | | | | | Merge pull request #21678 from ronakjangir47/array_to_formatted_s_docsYves Senn2015-09-221-1/+3
|\ \ \ \ \ \ | | | | | | | | | | | | | | Added Examples in docs for internal behavior of Array#to_formatted_s [ci skip]
| * | | | | | Added Examples in docs for internal behavior of Array#to_formatted_s [ci skip]Ronak Jangir2015-09-191-1/+3
| | | | | | |
* | | | | | | Short-circuit `blank?` on date and time valuesAndrew White2015-09-215-1/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The concept of a blank date or time doesn't make sense so we can short circuit the calls for `blank?` on these classes to gain small speed boost. Fixes #21657
* | | | | | | Applying right result of examples in ActiveSupport Multibyte [ci skip]amitkumarsuroliya2015-09-211-6/+4
| | | | | | |
* | | | | | | String#strip_heredocs doesn't need Object#tryVlado Cingel2015-09-191-2/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Call to Object#try was removed with this pull request https://github.com/rails/rails/pull/21596
* | | | | | | Merge pull request #21669 from amitsuroliya/improve_docsYves Senn2015-09-191-2/+2
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Correcting `NameError` error message in `mattr_reader` method. [ci skip]
| * | | | | | | Correcting `NameError` error message in `mattr_reader` method. Since this ↵amitkumarsuroliya2015-09-191-2/+2
| |/ / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | commit https://github.com/rails/rails/commit/7dfbd91b0780fbd6a1dd9bfbc176e10894871d2d, `NameError` includes attribute_name also in error message [ci skip]
* | | | | | | Corrected numeric conversions output [ci skip]amitkumarsuroliya2015-09-191-2/+2
| | | | | | |
* | | | | | | Replace `#=>` with `# =>` [ci skip]amitkumarsuroliya2015-09-191-2/+2
|/ / / / / / | | | | | | | | | | | | | | | | | | @rafaelfranca suggested in https://github.com/rails/rails/commit/f7c7bcd9c2a8b0e8c2840295d001d2d4dfd4cfae that code examples should display the result after `# =>` and not after `#=>`.
* | | | | | Merge pull request #21662 from amitsuroliya/improve_docsKasper Timm Hansen2015-09-181-3/+3
|\ \ \ \ \ \ | |/ / / / / |/| | | | | Corrected `to_time` output in ActiveSupport Date Conversion docs [ci …
| * | | | | Corrected `to_time` output in ActiveSupport Date Conversion docs.Since ↵amitkumarsuroliya2015-09-181-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | https://github.com/rails/rails/commit/48583f8bf74d1cefefea3cd6591bd546a9eaff6c, to_time returns times formatted as YYYY-MM-DD HH:MM:SS UTC [ci skip] `to_time` method now returns in `YYYY-MM-DD HH:MM:SS UTC` format.
* | | | | | Merge branch 'master' of github.com:rails/docrailsVijay Dev2015-09-181-1/+1
|\ \ \ \ \ \
| * | | | | | s/JQuery/jQuery/Akira Matsuda2015-09-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | [ci skip]
* | | | | | | File encoding is defaulted to utf-8 in Ruby >= 2.1Akira Matsuda2015-09-182-4/+0
| |/ / / / / |/| | | | |
* | | | | | 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.