A little utility to make

require ‘xxx’

take much less time. As in much less. Well, on windows at least. And to speedup how long it takes things to load in windows, like rails apps.

Well, mostly on windows–on linux it’s a speedup of only 0.41 to 0.45s, or so. [1]

If you’ve ever wondered why ruby feels slow on doze…sometimes it’s just the startup time. This really helps.

Benchmarks:

loading a (blank) rspec file:

1.9.1   
  without 3.20s
  with 0.34s (10x improvement)
1.8.6
  without 3.6s
  with 1.25s

rails app, running a unit test file

1.8.6
  without:
     23s
  with:
     14s

rails app, running $ script/console “puts 333”

1.9.1
  without:
     20s
   with:
     10s

1.8.6 
  without:
      9s
  with:
      6s

running “rake -T”

1.9.1
  without: 3.75s
  with: 1.5s       

1.8.6
  without: 1.37s
  with:    1.25s

Note: in reality what we should do is fix core so that it doesn’t have such awful I/O time in windows. There may be some gross inefficiency in there. For now, this is a work-around.

How to use in Rails ==

You can either install the gem, then add a

require ‘rubygems’ require ‘faster_require’

in your config/environment.rb, or (the best way is as follows):

Unpack it somewhere, like lib $ cd my_rails_app/lib $ gem unpack faster_require

Now add this line to your config/environment.rb:

require File.dirname(__FILE__) + “/../lib/faster_require-0.7.0/lib/faster_require” # faster speeds all around…make sure to update it to whatever version number you fetched.

add it before this other (existing) line:

require File.join(File.dirname(__FILE__), ‘boot’)

Now it will speedup loading rubygems and everything. Happiness. (NB that setting it this will also run in production mode code, so be careful here, though it does work for me in production).

clearing cache ==

If you use bundler to change bundled gems, you’ll want to run the command $ faster_require –clear-cache so that it will pick up the new load paths. Also if you moves files around to new paths, you may want to do the same. As you install any new gems, it should clear the paths automatically for you.

How to use generally ==

You can install it to be used “always” (well, for anything that loads rubygems, at least, via something like the following):

$ gem which rubygems d:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems.rb

$ gem which faster_require d:/Ruby192/lib/ruby/gems/1.9.1/gems/faster_require-0.6.0/lib/faster_require.rb

Now edit that rubygems.rb file, and add a require line to the top of it like this:

require ‘d:/Ruby192/lib/ruby/gems/1.9.1/gems/faster_require-0.6.0/lib/faster_require.rb’

update the path for your own, obviously. You’ll also have to change that added line if you ever install a newer version of faster_require gem, or if you update your version of rubygems, as the rubygems.rb file will be wiped clean at that point.

Enjoy.