Sunday, May 15, 2011

The lines that save us days of work (Rails patch)

We do program Ruby On Rails over Windows OMG!.
Thanks to the RubyInstaller  project [*] the daily tasks run as smooth as if we were in a *nix based O.S.
There is however one thing that still make working over windows inferior than doing the same over linux: the full stack took longer to load. So, if you run tests too often you end up wasting a no little time at the end of the day.
There are a couple of things that you can do to cut the load time:
  1. Latests versions of Ruby (Ruby Gems) load faster than old ones.
  2. If you run an antivirus, exclude Ruby directory from analysis
  3. The king: http://gaertig.pl/blog/en/2010/06/how-to-speed-up-rails3-startup-on-windows-by-30-percent.html
It seems that the problem is in the Kernel::require method, that is unoptimized in windows. Fortunately you can patch it to increase the speed. Or more accurately, skip duplicated require invokations. Just add these lines at the beginning of your config/environment.rb .

unless ENV['RAILS_ENV'] == 'production'#Rails.env.production?
  #from http://gaertig.pl/blog/en/2010/06/how-to-speed-up-rails3-startup-on-windows-by-30-percent.html
  if not $SPEEDUP_LIBCACHE
    $SPEEDUP_LIBCACHE = {}
    module Kernel
      alias chained_require require
      def require(path)
        $SPEEDUP_LIBCACHE[path] ||= chained_require path
      end
      private :require
      private :chained_require
    end
  end
end


Thats all. BTW: I found no difference in linux: it already loads quickly.

[*] Remember installing DevKit ( https://github.com/oneclick/rubyinstaller/wiki/Development-Kit ) to automatically build gems with native extensions.

No comments:

Post a Comment