Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Created May 27, 2012 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d11wtq/2814849 to your computer and use it in GitHub Desktop.
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
@d11wtq
Copy link
Author

d11wtq commented May 27, 2012

Note that my raw numbers are 11.5 seconds down to 8 seconds. It's not scientific, but the difference is big enough that something is worth investigating further.

@snikch
Copy link

snikch commented May 27, 2012

15.75s avg before
13.61s avg after

13.6% decrease

@d11wtq
Copy link
Author

d11wtq commented May 27, 2012

I smell a Rails pull request coming up.

@d11wtq
Copy link
Author

d11wtq commented May 30, 2012

Ok, nobody use this. It's fine in production where cache_classes is om, but in dev class reloading causes issues. Bummer.

@snikch
Copy link

snikch commented May 30, 2012

That was my initial thought, but I've been using it since you put it up and haven't run into any issues. What specific case would cause a problem?

@snikch
Copy link

snikch commented May 30, 2012

I did it, then tested that changing classes works fine fyi

@d11wtq
Copy link
Author

d11wtq commented May 30, 2012

It seems to cause issues for us when modules are mixed into our models at runtime... on subsequent requests after the first one, we start getting "uninitialized constant ModuleName" for whatever is supposed to be mixed in, presumably since the 'require' for the module isn't happening correctly the second time around. Maybe there's another workaround.

@snikch
Copy link

snikch commented May 30, 2012

Are you using require or include?

@d11wtq
Copy link
Author

d11wtq commented May 30, 2012

require to get the module into scope, include to mix it into the model. Do you mean require or load?

@snikch
Copy link

snikch commented May 30, 2012

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
Copy link
Author

d11wtq commented May 30, 2012 via email

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