Rails 3 Initializes Extremely Slow on Ruby 1.9.2

slow rails stack

That is bothering me also, since I have switched to Rails 3.

To your second question: I found by digging through the framework that the initializers take about half the time of a simple rake or rails call before it actually starts doing its task.

If you put these simple timing lines into the loop of initializer calls in $GEM_PATH/gems/railties-3.0.3/lib/rails/initializable.rb (or piggy-back it if you like):

def run_initializers(*args)
return if instance_variable_defined?(:@ran)
t0 = Time.now
initializers.tsort.each do |initializer|
t = Time.now
initializer.run(*args)
puts("%60s: %.3f sec" % [initializer.name, Time.now - t])
end
puts "%60s: %.3f sec" % ["for all", Time.now - t0]
@ran = true
end

EDIT: Or, for railties 4.2.1:

def run_initializers(group=:default, *args)
return if instance_variable_defined?(:@ran)
t0 = Time.now
initializers.tsort.each do |initializer|
t = Time.now
initializer.run(*args) if initializer.belongs_to?(group)
puts("%60s: %.3f sec" % [initializer.name, Time.now - t])
end
puts "%60s: %.3f sec" % ["for all", Time.now - t0]
@ran = true
end

... you can follow up what happens. On my system, which is a 2.4 Core 2 Duo MacBook the initializers take about 7 seconds.

There are a few that are especially slow on my system. When I filter all out below a second, I get this result on my system:

                load_active_support: 1.123 sec
active_support.initialize_time_zone: 1.579 sec
load_init_rb: 1.118 sec
set_routes_reloader: 1.291 sec

I am sure somebody (is it me?) will take some time to start there and optimize.

Should I use Ruby 1.9.2 for my Rails 2.3.10 app?

I would recommend doing both, but the order in which you do them, or if you do them both at once is really a personal preference. If you have a strong test-suite with good coverage this is a great first step in making the transition. The main road-blocks you'll run into are the following:

  • Many newer gem versions only support Rails 3, so if you are only doing one step at a time, make sure that you're gems are supported. For example you don't want to get stuck in a situation where a gem requires you to upgrade because you are using Ruby 1.9.2 but the new version of the gem is only available in Rails 3.
  • There are some syntax changes in Ruby 1.9.2 and compiled C extensions need to be re-complied or the gems re-installed.
  • There are major application configuration changes from Rails 2.3 to Rails 3.0. They take some time to complete but there is lots of support.

In general Ruby 1.9.2 will be faster than Ruby 1.8.7 and will provide some cool new syntax. If you are getting opposite results then I would benchmark your code and make sure that this is actually the case and that it's not just failing tests that are slowing your suite down.

How do I force my Rails 3 app to use 1.9.2

You are probably executing an old 1.8.7 rails binary that is being found first in your UNIX search path.

You can type which rails at the command line to see which rails executable you are running. On my machine I get:

/Users/scott/.rvm/gems/ruby-1.9.2-p136/bin/rails

You can also check your binary search path by typing echo $PATH

If you get a result from which rails that looks something like:

/blah/blah/gems/ruby-1.8.7/bin/rails

... then you need to remove the old rails binary from your path, or use the old 1.8.7 ruby to uninstall the rails gems, so that they are not found and executed.

Once you have done this, the next time you try to execute rails s, the 1.9.2 version of the rails binary should be executed.

Extremely slow bundler 1.0.3 (Bundler.require) with around 100 gems in Rails 3.0.1 and Ruby 1.9.2

The best I've come up is to utilize spork. It works great and makes up for the slow startup. Spork starts up a DRB Server to which rspec / cucumber can connect and use preloaded ruby processes to run the tests. It reloads certain files (Tests, Classes, ...) for each run, so it is almost as fresh as a new process.

https://github.com/timcharper/spork



Related Topics



Leave a reply



Submit