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

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” somewhere

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 load time in windows. There may be some inefficiency in there. For now, this is a work-around that helps.

Also it helps with SSD’s even, on windows, as it avoids some cpu used at require time.

How to use ==

In general, you can either use it by installing the gem, then adding require ‘rubygems’ require ‘faster_require’ to the top of your script.

However this doesn’t speedup the loading of rubygems, so you can do this to get best performance:

G:>gem which faster_require C:/installs/Ruby187/lib/ruby/gems/1.8/gems/faster_require-0.7.4/lib/faster_require.rb

now before doing require rubygems in your script, do this:

require ‘C:/installs/Ruby187/lib/ruby/gems/1.8/gems/faster_require-0.7.4/lib/faster_require.rb’

Then rubygems load will be cached too. Faster.

How to use in Rails ==

One way is to install the gem, then add a

require ‘rubygems’ require ‘faster_require’

in your config/environment.rb, or (the better 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 though.

add that before this other (pre-existing) line:

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

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

Ping me if it’s still too slow.

Clearing the 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 any new load paths. Also if you moves files around to new directories, 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/globally ==

You can install it to be used “always” (well, for anything that loads rubygems, at least, which is most things, 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.

See also ==

Spork can help speedup rails tests. I “should” work well when used in conjunction with faster_require, as well.

Enjoy.