Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10X speed improvements for AS::Dependencies.loadable_constants_for_path #21411

Conversation

byroot
Copy link
Member

@byroot byroot commented Aug 28, 2015

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

cc @rafaelfranca

@@ -421,13 +421,13 @@ def loadable_constants_for_path(path, bases = autoload_paths)

bases.each do |root|
expanded_root = File.expand_path(root)
next unless %r{\A#{Regexp.escape(expanded_root)}(/|\\)} =~ expanded_path
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the \\ was for Windows support. But then nesting[0] == ?/ was kinda breaking it. Not sure what to do here, but I can test for both.

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
@byroot byroot force-pushed the perf-improvement-in-dependencies-loadable-constants branch from 629f2da to 2e0cd0f Compare August 28, 2015 01:49
@byroot
Copy link
Member Author

byroot commented Aug 28, 2015

After fixing the test failure it's slightly slower, still about 9 times faster than current implementation though:

Calculating -------------------------------------
             patched   792.000  i/100ms
-------------------------------------------------
             patched      9.664k (±19.9%) i/s -     44.352k

@rafaelfranca rafaelfranca reopened this Aug 28, 2015
rafaelfranca added a commit that referenced this pull request Aug 28, 2015
…s-loadable-constants

10X speed improvements for AS::Dependencies.loadable_constants_for_path
@rafaelfranca rafaelfranca merged commit 1abbd3e into rails:master Aug 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants