aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/dependencies.rb
Commit message (Collapse)AuthorAgeFilesLines
* Performance improvement for `String#to`Ryuta Kamizono2019-07-281-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ```ruby class String def to1(position) position = [position + length, -1].max if position < 0 self[0, position + 1] end def to2(position) position += size if position < 0 self[0, position + 1] || +"" end end Benchmark.ips do |x| x.report("'foo'.to(1)") { 'foo'.to(1) } x.report("'foo'.to1(1)") { 'foo'.to1(1) } x.report("'foo'.to2(1)") { 'foo'.to2(1) } x.report("'foo'.to(-1)") { 'foo'.to(-1) } x.report("'foo'.to1(-1)") { 'foo'.to1(-1) } x.report("'foo'.to2(-1)") { 'foo'.to2(-1) } x.report("'foo'.to(-10)") { 'foo'.to(-10) } x.report("'foo'.to1(-10)") { 'foo'.to1(-10) } x.report("'foo'.to2(-10)") { 'foo'.to2(-10) } end ``` Result: ``` Warming up -------------------------------------- 'foo'.to(1) 199.859k i/100ms 'foo'.to1(1) 220.293k i/100ms 'foo'.to2(1) 221.522k i/100ms 'foo'.to(-1) 205.032k i/100ms 'foo'.to1(-1) 195.837k i/100ms 'foo'.to2(-1) 214.975k i/100ms 'foo'.to(-10) 214.331k i/100ms 'foo'.to1(-10) 182.666k i/100ms 'foo'.to2(-10) 224.696k i/100ms Calculating ------------------------------------- 'foo'.to(1) 4.685M (± 4.2%) i/s - 23.583M in 5.042568s 'foo'.to1(1) 5.233M (± 5.8%) i/s - 26.215M in 5.026778s 'foo'.to2(1) 5.180M (± 5.7%) i/s - 25.918M in 5.020735s 'foo'.to(-1) 4.253M (± 7.0%) i/s - 21.323M in 5.043133s 'foo'.to1(-1) 4.438M (±11.2%) i/s - 21.934M in 5.025751s 'foo'.to2(-1) 4.716M (± 9.8%) i/s - 23.432M in 5.028088s 'foo'.to(-10) 4.678M (± 9.5%) i/s - 23.148M in 5.007379s 'foo'.to1(-10) 4.428M (± 5.1%) i/s - 22.103M in 5.005155s 'foo'.to2(-10) 5.243M (± 4.6%) i/s - 26.289M in 5.024695s ```
* let autoloaded? support modules with overridden names [closes #36757]Xavier Noria2019-07-251-2/+13
|
* Fix Loadable.exclude_from to also reset Kernel#requireJean Boussier2019-07-161-3/+23
|
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-1/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* fixes eager loading edge case in :zeitwerk modeXavier Noria2019-03-301-0/+5
|
* Improve the logic that detects non-autoloaded constantsJan Habermann2018-10-281-1/+5
| | | | | | | If you require `nokogiri` from `app/models/user.rb`, dependencies.rb does not mark `Nokogiri` as an autoloaded constant, as expected. But the logic to detect these non-autoloaded constants is incomplete. See the tests defined in the patch for some cases incorrectly handled.
* Fix call sitesGannon McGibbon2018-10-021-2/+2
|
* Add `Style/RedundantFreeze` to remove redudant `.freeze`Yasuo Honda2018-09-291-3/+3
| | | | | | | | | | | | | | | | | | | | | Since Rails 6.0 will support Ruby 2.4.1 or higher `# frozen_string_literal: true` magic comment is enough to make string object frozen. This magic comment is enabled by `Style/FrozenStringLiteralComment` cop. * Exclude these files not to auto correct false positive `Regexp#freeze` - 'actionpack/lib/action_dispatch/journey/router/utils.rb' - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb' It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333 Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed. * Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required - 'actionpack/test/controller/test_case_test.rb' - 'activemodel/test/cases/type/string_test.rb' - 'activesupport/lib/active_support/core_ext/string/strip.rb' - 'activesupport/test/core_ext/string_ext_test.rb' - 'railties/test/generators/actions_test.rb'
* trace autoloads, and document hints for troubleshootingXavier Noria2018-09-071-4/+21
| | | | Closes #32885.
* Chomp will work without checking for end of the stringBart de Water2018-07-291-2/+2
|
* Work around Performance/EndWith false positiveBart de Water2018-07-281-2/+2
| | | | | Rubocop warns about "Use String#end_with? instead of a regex match anchored to the end of the string", it doesn't seem aware of the $` special variable like Performance/RegexpMatch
* Document require_dependency [ci skip]Yoshiyuki Kinjo2018-05-091-0/+4
|
* Remove duplicates after autoloading modulesRobert Mosolgo2018-01-261-0/+1
|
* Enable autocorrect for `Lint/EndAlignment` copKoichi ITO2018-01-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ### Summary This PR changes .rubocop.yml. Regarding the code using `if ... else ... end`, I think the coding style that Rails expects is as follows. ```ruby var = if cond a else b end ``` However, the current .rubocop.yml setting does not offense for the following code. ```ruby var = if cond a else b end ``` I think that the above code expects offense to be warned. Moreover, the layout by autocorrect is unnatural. ```ruby var = if cond a else b end ``` This PR adds a setting to .rubocop.yml to make an offense warning and autocorrect as expected by the coding style. And this change also fixes `case ... when ... end` together. Also this PR itself is an example that arranges the layout using `rubocop -a`. ### Other Information Autocorrect of `Lint/EndAlignment` cop is `false` by default. https://github.com/bbatsov/rubocop/blob/v0.51.0/config/default.yml#L1443 This PR changes this value to `true`. Also this PR has changed it together as it is necessary to enable `Layout/ElseAlignment` cop to make this behavior.
* removed unnecessary returnsShuhei Kitagawa2017-10-281-1/+1
|
* [Active Support] require_relative => requireAkira Matsuda2017-10-211-11/+11
| | | | This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
* Use tt in doc for railties [skip ci]Yoshiyuki Hirano2017-08-271-1/+1
|
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-111-0/+1
|
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-091-0/+1
|
* [Active Support] require => require_relativeAkira Matsuda2017-07-011-11/+11
|
* Use mattr_accessor default: option throughout the projectGenadi Samokovarov2017-06-031-22/+11
|
* Fix `require_dependency` message formatRyuta Kamizono2016-11-251-1/+1
| | | | | | | | | | | | `depend_on` message format is `"No such file to load -- %s.rb"`. But `require_dependency` message is missing `.rb` suffix. ``` % git grep -n 'No such file to load' actionview/test/actionpack/abstract/helper_test.rb:112: assert_equal "No such file to load -- very_invalid_file_name.rb", e.message activesupport/lib/active_support/dependencies.rb:245: def require_dependency(file_name, message = "No such file to load -- %s.rb") activesupport/lib/active_support/dependencies.rb:333: def depend_on(file_name, message = "No such file to load -- %s.rb") ```
* Remove deprecated Module.qualified_const_get/set/defined?Andrew White2016-11-141-1/+0
|
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-1/+1
|
* fixes remaining RuboCop issues [Vipul A M, Xavier Noria]Xavier Noria2016-09-011-1/+1
|
* Add three new rubocop rulesRafael Mendonça França2016-08-161-1/+1
| | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* 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.
* applies remaining conventions across the projectXavier Noria2016-08-061-5/+5
|
* normalizes indentation and whitespace across the projectXavier Noria2016-08-061-13/+13
|
* applies new string literal convention in activesupport/libXavier Noria2016-08-061-24/+24
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Improve code readability in ActiveSupport::DependenciesAaron Ang2016-03-161-5/+6
|
* Add comments to ActiveSupport::Dependencies to help understandingAaron Ang2016-03-161-0/+2
| | | | | | | | In a previous patch, all log-related stuff was removed. However, some logs are still useful to understand the code. Therefore, in this patch, I put those log messages back as comments. [ci skip]
* no need to clear an unusued collectionXavier Noria2016-03-161-1/+1
| | | | | | | | | | If the code reaches that line new_constants is no longer needed. We only need here to iterate over it to discard stuff and done. Note that constant_watch_stack.new_constants returns a new reference each time it is invoked, so that #clear call was not cleaning state in some internal structure (which would have been a bit dirty as well at this level of coupling).
* removes unreachable codeXavier Noria2016-03-161-2/+0
| | | | | | | | | | | | This array literal cannot be reached. The previous begin either returns to the caller via the explicit return in the ensure block if all goes well, or else propagates whatever make the begin block abort execution. I have investigated the origin of this a bit. In the past the ensure block didn't have a return call, see for example c08547d. Later on the return was added in 4da4506, but the trailing literal was left there.
* Remove log-related stuff from ActiveSupport::DependenciesAaron Ang2016-03-151-44/+0
| | | | | In this patch, all log-related stuff in `ActiveSupport::Dependencies` is removed because the logging is no longer useful.
* Deprecate `Module.local_constants`yui-knk2016-03-011-2/+2
| | | | | After Ruby 1.9, we can easily get the constants that have been defined locally by `Module.constants(false)`.
* Dependencies clean upSruli Rapps2016-02-191-3/+3
| | | | | | | | | | | | | | | | | | | | Cleans up four items I came across in ActiveSupport::Dependencies: - DependenciesTest# test_dependency_which_raises_exception_isnt_added_to_loaded_set: Fixes current implementation which will pass no matter what since the filepath is never added to "loaded" or "history" without being expanded first. - Remove DependenciesTest#test_unhook. Seems leftover from when alias_method_chain was used in Loadable and ModuleConstMissing. The test will always pass since Module never responds to those methods - WatchStack#new_constants documentation: update self to @stack. Looks like self was leftover from when WatchStack inherited from Hash - Remove ActiveSupport namespace from call to Dependencies.constant_watch_stack.watching? since the namespace is not needed, Dependencies is called two other times in the same method without it (even on the same line) and it brings the line to within 80 characters
* Require only necessary concurrent-ruby classes.Jerry D'Antonio2015-11-041-1/+1
|
* Add missing punctuation mark in `ActiveSupport` docs [ci skip]amitkumarsuroliya2015-10-111-3/+3
| | | It improves readability of docs
* Replaced `ThreadSafe::Map` with successor `Concurrent::Map`.Jerry D'Antonio2015-09-191-2/+2
| | | | | | | The thread_safe gem is being deprecated and all its code has been merged into the concurrent-ruby gem. The new class, Concurrent::Map, is exactly the same as its predecessor except for fixes to two bugs discovered during the merge.
* 10X speed improvements for AS::Dependencies.loadable_constants_for_pathJean Boussier2015-08-271-5/+5
| | | | | | | | | | | | | | | | | | | | When the autoload_paths start to grows, this methods is quite a hotspot >> ActiveSupport::Dependencies.autoload_paths.size => 49 >> Benchmark.ips { |x| x.report('baseline') { ActiveSupport::Dependencies.loadable_constants_for_path(File.expand_path('app/models/shop')) }} Calculating ------------------------------------- baseline 90.000 i/100ms ------------------------------------------------- baseline 1.073k (±20.2%) i/s - 4.950k After the patch Calculating ------------------------------------- patched 883.000 i/100ms ------------------------------------------------- patched 11.050k (±19.7%) i/s - 50.331k
* Merge pull request #20928 from matthewd/unload-interlockMatthew Draper2015-07-241-1/+8
|\ | | | | We need stricter locking before we can unload
| * We need stricter locking before we can unloadMatthew Draper2015-07-201-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | Specifically, the "loose upgrades" behaviour that allows us to obtain an exclusive right to load things while other requests are in progress (but waiting on the exclusive lock for themselves) prevents us from treating load & unload interchangeably: new things appearing is fine, but they do *not* expect previously-present constants to vanish. We can still use loose upgrades for unloading -- once someone has decided to unload, they don't really care if someone else gets there first -- it just needs to be tracked separately.
* | Don't apply locking around basic #load / #requireMatthew Draper2015-07-231-6/+4
|/ | | | | | That's outside our remit, and dangerous... if a caller has their own locking to protect against the natural race danger, we'll deadlock against it.
* Freeze string literals when not mutated.schneems2015-07-191-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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)
* Document ShareLock and the InterlockMatthew Draper2015-07-091-3/+20
|
* Soften the lock requirements when eager_load is disabledMatthew Draper2015-07-091-34/+47
| | | | | We don't need to fully disable concurrent requests: just ensure that loads are performed in isolation.
* let dependencies use Module#const_defined?Xavier Noria2015-01-281-2/+2
| | | | | | | Module#const_defined? accepts constant paths in modern Ruby, we no longer need our qualified_* extensions. References #17845.
* Remove some comments about Ruby 1.9 behaviorsRafael Mendonça França2015-01-041-1/+1
|
* Pass symbol as an argument instead of a blockErik Michaels-Ober2014-11-291-1/+1
|