1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

10X speed improvements for AS::Dependencies.loadable_constants_for_path

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
This commit is contained in:
Jean Boussier 2015-08-27 21:28:54 -04:00
parent f642208ae4
commit 2e0cd0f371

View file

@ -421,13 +421,13 @@ module ActiveSupport #:nodoc:
bases.each do |root| bases.each do |root|
expanded_root = File.expand_path(root) expanded_root = File.expand_path(root)
next unless %r{\A#{Regexp.escape(expanded_root)}(/|\\)} =~ expanded_path next unless expanded_path.start_with?(expanded_root)
nesting = expanded_path[(expanded_root.size)..-1] root_size = expanded_root.size
nesting = nesting[1..-1] if nesting && nesting[0] == ?/ next if expanded_path[root_size] != ?/.freeze
next if nesting.blank?
paths << nesting.camelize nesting = expanded_path[(root_size + 1)..-1]
paths << nesting.camelize unless nesting.blank?
end end
paths.uniq! paths.uniq!