Skip to content

Instantly share code, notes, and snippets.

@pmahoney
Created November 1, 2012 21:30
Show Gist options
  • Save pmahoney/3996724 to your computer and use it in GitHub Desktop.
Save pmahoney/3996724 to your computer and use it in GitHub Desktop.
JRuby $LOAD_PATH index speeds up Bundler.require in a Rails app (at top of application.rb)
Rails 3.1.8 app in Ruby 1.8 compatibility mode with ~65 dependencies
(not sure how many in total including
transient deps) was timed up to the Bundler.require line in config/application.rb
(JRuby 1.7.0)
Before: 18.8 seconds
After: 15.9 seconds
This likely seriously breaks the semantics of 'require', and maybe isn't faster
enough to be worth it.
$LOAD_PATH_INDEX = {}
alias :__orig_require__ :require
def require(name)
# Underlying $LOADED_FEATURES is actually an StringArraySet, so should
# have O(1) lookup.
feature = name + '.rb'
if !$LOADED_FEATURES.include?(feature) && abs = $LOAD_PATH_INDEX[feature]
$LOADED_FEATURES << feature
__orig_require__(abs)
else
__orig_require__(name)
end
end
start = Time.now
$LOAD_PATH.each do |path|
next if path == '.'
Dir[path + '/**/*.rb'].each do |rb|
# Strip off the path portion plus extra /.
# What remains is what one would pass as an arg to a require with
# an extra '.rb'.
feature = rb.slice((path.length+1)..(-1))
# first in load path order wins
$LOAD_PATH_INDEX[feature] ||= rb
end
end
puts "INDEXED ALL #{$LOAD_PATH_INDEX.size} THINGS! #{Time.now - start}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment