Multiple "require" calls -> app slow to start

P

Philippe Lang

Hi,

More and more files are being added to my client ruby application, and
it is getting quite slow to start, even on a fast machine. It takes
about 15 seconds in some cases.

The problem is with the call to "require" for the application
"workspace" object. This "require" points to files which in turn call
"require" for other files, and so on.

Is there a way to speed up the application load time maybe?

Thanks,
 
M

Mauricio Fernandez

More and more files are being added to my client ruby application, and
it is getting quite slow to start, even on a fast machine. It takes
about 15 seconds in some cases.

The problem is with the call to "require" for the application
"workspace" object. This "require" points to files which in turn call
"require" for other files, and so on.

Is there a way to speed up the application load time maybe?

Are you loading files managed by RubyGems?
RubyGems' #require implementation is known to be very slow. Things playing
against you include:
* required files from many different gems
* lots of installed gems

You can try to uninstall some gems (for instance unused, old versions), or
hardcode the require paths:

prefix = "/usr/local"
%w[foo-0.1.1/lib bar-0.1/lib].each do |dir|
$:.unshift "#{prefix}/lib/ruby/gems/1.8/gems/#{dir}"
end

Another thing that can help (in general, not only when you're using RubyGems)
is lazy loading using Kernel#autoload.
 
G

gga

Another thing that can help (in general, not only when you're using RubyGems)
is lazy loading using Kernel#autoload.

Similar to autoload, but manually doing it (keeping all of rubygems
functionality) is to do your requires within functions that use the
library.
Thus, rather than doing:

require 'mylib'

def use_it
MyLib.new
end

Write:

def use_it
require 'mylib'
MyLib.new
end

Only when use_it is called will the require be executed. If your code
never calls use_it, the mylib library is never loaded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top