Why are Rails 3.1 apps so slow in development mode?

It's not quite this bad, but it's not far off! Thanks to "XKCD":http://xkcd.com It’s not quite this bad, but it’s not far off! Thanks to XKCD

So Rails 3.1 was released a couple of months ago. Along with various other changes, it introduced the asset pipeline to give more organisational structure to other things like images, css and javascript. Here at Foxsoft we’re still not completely sold on the asset pipeline. Although some of the advantages it brings are quite useful it has slowed us down in development somewhat because as the number of assets has increased it begins to take longer and longer to load a page.

Fortunately, the guys over at Wavii have released the rails-dev-tweaks gem which can really improve things.

The reason Rails starts getting slower and slower is because for each asset request it is running all of the to_prepare hooks, even if the asset itself hasn’t changed and Sprockets decides it hasn’t been modified.

The rails-dev-tweaks gem thus disables to_prepare & reloading on any asset request and is really easy to add to your project.

Simply add the gem to your Gemfile:
group :development do gem 'rails-dev-tweaks', '~> 0.5.2' end

By default, it will stop your application’s code from being reloaded so if you’re using custom sass functions or referencing your app from with assets you might have problems. Also, xhr requests don’t reload the code until you make a regular content request, on the assumption that you don’t debug xhr requests without reloading the main page first.

If these defaults don’t work for you, then it’s easy to customise what is and isn’t reloaded. You specify rules via a configuration block in your environment configuration, likely just config/environments/development.rb.
config.dev_tweaks.autoload_rules do keep :all skip '/favicon.ico' skip :assets skip :xhr keep :forced end
The default ruleset

Later rules override those that were defined earlier and you choose whether to keep or skip a given request. Any request which skips the hooks generates a line in the log file so you know whether something is being reloaded or not. The README file quite comprehensively covers all the options you can pass.

If you’re finding that your Rails projects are slowing down during development then I highly recommend you checkout this gem.