Joe Van Dyk
Joe Van Dyk

Reputation: 6950

Why is Rails class loading in development slow?

I've got a larger Rails 3.1 application. It's getting super slow in development mode. I've tracked down part of the problem to slow class autoloading. I have a couple hundred models in my system and am using about 130 gems.

If my controller does nothing other than reference class names (i.e

def index
  User
  Order
  Game
  LineItem
  # and so on...
  render :text => ''
end

The load of the page can take almost one second, just for this with some of my models. The log looks something like:

Completed 200 OK in 762ms (Views: 3.2ms | ActiveRecord: 3.9ms)

I've tested this with Ruby 1.9.2 and 1.9.3.

Upvotes: 1

Views: 494

Answers (4)

Diego
Diego

Reputation: 610

This is namely due to the automatic class reloading when working in development mode, which makes it easy for us, where we make changes to the code and hit page refresh. This behavior is governed by the config.cache_classes in your config/environment/development.rb file.

 # In the development environment your application's code is reloaded on
 # every request. This slows down response time but is perfect for development
 # since you don't have to restart the web server when you make code changes.
 config.cache_classes = false

When the config.cache_classes is set to false, Rails will user the load statement to its class loading. When set to true it will use the require statement instead.

This has to do with all the files you have in your app that needs to be loaded. You can check this by going into the rails console and entering $LOAD_PATH. I'm sure you have tons of it.

Upvotes: 1

Joe Van Dyk
Joe Van Dyk

Reputation: 6950

This was largely fixed in Rails 3.2.

Upvotes: 1

Nick Colgan
Nick Colgan

Reputation: 5508

This is the trade off you make when running in the development environment; you get autoloading (incredibly useful when developing) in exchange for poorer performance. In production, you get better performance at the expense of autoloading (which isn't all that useful in production, anyway).

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230396

Well, that's what development mode does, it reloads your code, so that you don't have to restart your server.

Upvotes: 0

Related Questions