# Multiple "require" calls -\> app slow to start

**URL:** <https://www.ruby-forum.com/t/multiple-require-calls-app-slow-to-start/84600>\
**Category:** Ruby\
**Created:** [February 6, 2007, 9:12am UTC](https://www.ruby-forum.com/t/multiple-require-calls-app-slow-to-start/84600 "2007-02-06T09:12:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 6, 2007, 9:12am UTC](https://www.ruby-forum.com/t/multiple-require-calls-app-slow-to-start/84600/1 "2007-02-06T09:12:09Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 6, 2007, 10:58am UTC](https://www.ruby-forum.com/t/multiple-require-calls-app-slow-to-start/84600/2 "2007-02-06T10:58:51Z")

</div>

On Tue, Feb 06, 2007 at 05:10:58PM +0900, Philippe L. wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![philpraxis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@philpraxis](https://www.ruby-forum.com/u/philpraxis)\
**Post date:** [February 7, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/multiple-require-calls-app-slow-to-start/84600/3 "2007-02-07T18:36:13Z")

</div>

> 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.
