Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Created May 27, 2012 15:49
Show Gist options
  • Select an option

  • Save d11wtq/2814849 to your computer and use it in GitHub Desktop.

Select an option

Save d11wtq/2814849 to your computer and use it in GitHub Desktop.
Rails startup time optimization
# Instructions for use
#
# 1. Execute "time rails runner nil" a few times in your shell, see how long rails takes to boot
# 2. Add this monkey patch to the top of config/boot.rb
# 3. Repeat step 1. What's the difference?
#
# This is just an experiment, but it gains me 30% on Rails' start time, which suggests there is something
# in Rails that is breaking the "run only once" behaviour of #require.
#
# Note: You can't override Kernel#require, since Rails by-passes it most of the time.
class Object
def require_with_dirty_filthy_hack(path)
return false if (@@__seen ||= {})[path]
@@__seen[path] = true
require_without_dirty_filthy_hack(path)
end
alias_method :require_without_dirty_filthy_hack, :require
alias_method :require, :require_with_dirty_filthy_hack
end
@snikch

snikch commented May 30, 2012

Copy link
Copy Markdown

I meant require, wondered why you mentioned it since we're using Rails' autoload and don't require it at all - possibly why it still works.

@d11wtq

d11wtq commented May 30, 2012 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment