# Options for optimizing a large Ruby system

**URL:** <https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569>\
**Category:** ruby-talk\
**Created:** [4 September 2002 22:25 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569 "2002-09-04T22:25:33Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francis\_Hwang1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Francis\_Hwang1](https://rubytalk.org/u/Francis_Hwang1)\
**Post date:** [4 September 2002 22:25 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/1 "2002-09-04T22:25:33Z")

</div>

Hi everybody:

I’ve written a large e-commerce system in Ruby, and it works fine but  
it’s slow. After poking around the code a bit, I think that the big  
bottleneck comes from loading all the required files. The system is  
extremely OO – more than 100 separate classes – and there’re a lot  
of classes that need to be loaded in with “require” statements before  
a single line is executed.

So now I’m looking around for ways to speed things up. Using modruby  
is probably an option, but I’m unsure as to whether that’ll do what I  
need … I know it keeps the Ruby interpreter in memory, but I really  
need something that might keep loaded classes in memory. Does modruby  
do that, or is that too much to ask? And failing that, what do people  
think would be the next best option for me to pursue?

Thanks in advance,

Francis

---

<div class="post-metadata">

**Author:** ![Dave\_Thomas](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dave\_Thomas](https://rubytalk.org/u/Dave_Thomas)\
**Post date:** [4 September 2002 23:33 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/2 "2002-09-04T23:33:58Z")

</div>

[sera@fhwang.net](mailto:sera@fhwang.net) (Francis Hwang) writes:

> So now I’m looking around for ways to speed things up. Using modruby  
> is probably an option, but I’m unsure as to whether that’ll do what I  
> need … I know it keeps the Ruby interpreter in memory, but I really  
> need something that might keep loaded classes in memory. Does modruby  
> do that, or is that too much to ask? And failing that, what do people  
> think would be the next best option for me to pursue?

I have that same problem, and mod\_ruby does help. There’s still the  
lag the first time the code is loaded, but from then on it runs just  
fine.

Breaking the app up slightly helps, but unfortunately I made the  
mistake of persisting state using instances of my own classes: this  
leads to a problem when running multiple mod\_ruby processes and the  
one that picks up a session doesn’t have the same set of classes  
loaded as the one the previously saved that session: it raises  
exceptions during the deserialization.

I’m still working on the start-up overhead, though, so if you come up  
with any wonderful ideas please share them

Dave

---

<div class="post-metadata">

**Author:** ![Joseph\_McDonald](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Joseph\_McDonald](https://rubytalk.org/u/Joseph_McDonald)\
**Post date:** [4 September 2002 23:59 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/3 "2002-09-04T23:59:49Z")

</div>

> So now I'm looking around for ways to speed things up. Using modruby  
> is probably an option, but I'm unsure as to whether that'll do what I

Take a look at fastcgi: [http://www.fastcgi.com/](http://www.fastcgi.com/)  
Basically your ruby code stays in memory and talks to your web server  
(apache, iis, etc) via a socket. This way you have clean separation  
between the web serving process and the ruby cgi process (i.e. no need  
for a bloated httpd just to serve up images or static text).

A fastcgi program will generally get its database connections and  
other initialization tasks and then just go into a while loop  
accepting requests from the server.

We use fastcgi with ruby and are quite happy with it.

Also see this: [http://www.dmst.aueb.gr/dds/pubs/conf/2002-SANE-DynCont/html/dyncont.html](http://www.dmst.aueb.gr/dds/pubs/conf/2002-SANE-DynCont/html/dyncont.html)  
fastcgi does very well.

regards,  
-joe

---

<div class="post-metadata">

**Author:** ![Patrick\_May](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_May](https://rubytalk.org/u/Patrick_May)\
**Post date:** [5 September 2002 02:46 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/4 "2002-09-05T02:46:15Z")

</div>

[sera@fhwang.net](mailto:sera@fhwang.net) (Francis Hwang) wrote in message news:[7b561b93.0209041416.73b9be3a@posting.google.com](mailto:7b561b93.0209041416.73b9be3a@posting.google.com)…

> Hi everybody:
> 
> I’ve written a large e-commerce system in Ruby, and it works fine but  
> it’s slow. After poking around the code a bit, I think that the big  
> bottleneck comes from loading all the required files. The system is  
> extremely OO – more than 100 separate classes – and there’re a lot  
> of classes that need to be loaded in with “require” statements before  
> a single line is executed.
> 
> So now I’m looking around for ways to speed things up. Using modruby  
> is probably an option, but I’m unsure as to whether that’ll do what I  
> need … I know it keeps the Ruby interpreter in memory, but I really  
> need something that might keep loaded classes in memory. Does modruby  
> do that, or is that too much to ask? And failing that, what do people  
> think would be the next best option for me to pursue?

Since the interpreter loads a given “require ‘file’” once, I believe a  
mod\_ruby process will also load the file once.

I’d suggest trying mod\_ruby, and be sure to keep “before” and “after”  
numbers!

Cheers,

Patrick

---

<div class="post-metadata">

**Author:** ![Web2ed](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@Web2ed](https://rubytalk.org/u/Web2ed)\
**Post date:** [5 September 2002 03:46 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/5 "2002-09-05T03:46:18Z")

</div>

If you receive off-line responses, could you please post them to the  
group. I need to know how to hadle this situation also. Thank you.

//ed

---

<div class="post-metadata">

**Author:** ![Steven\_Stanfield](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@Steven\_Stanfield](https://rubytalk.org/u/Steven_Stanfield)\
**Post date:** [5 September 2002 07:27 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/6 "2002-09-05T07:27:47Z")

</div>

You could try a web application server. I just released ocelot (I will  
be changing the name with the next release to avoid the conflict pointed  
out by Peter Gulutzan a few days ago, see [ocelot.scarecrowtech.com](http://ocelot.scarecrowtech.com)). It  
(and I assume other app servers) work by starting up a Ruby instance and  
loading up your project which stays in memory for the life of the server  
instance (you will not have to reload your classes on each request). You  
can use the proxy module of Apache and a couple of rules to use it as a  
front end and let it handle all your static content. It does  
essentially what was described in an earlier post for fastcgi (I have  
not used fastcgi, just going on what someone else posted). The downside  
is you will have to modify your app to use the servlet API instead of  
CGI, this is not hard but will be extra work (you do pick up session  
management for free though).

If you do not use something like fastcgi or an app server you can also  
run your own copy of Ruby with your app and listen for requests on a  
port. Then use a CGI stub app to send request to it and return them to  
the browser (I used this method for my first prototypes of ocelot and it  
works pretty well, but you are going to add overhead with all the extra  
connections). This is basically what fastcgi does for you so I guess  
all it buys is a finer level of control (fastcgi is probably more  
optimized for this job as well). I am pretty sure that this is the  
method used by Red Hat’s Interchange server, so there is some precedent  
for using it in your situation (you should also be able to get the C  
code to their CGI stub app, using a C app for the stub would be a good  
idea since it will have to be loaded for each request).

Steven Stanfield

> **···**
>
> On Thu, 5 Sep 2002 07:25:33 +0900 [sera@fhwang.net](mailto:sera@fhwang.net) (Francis Hwang) wrote:
> 
> > Hi everybody:
> > 
> > I’ve written a large e-commerce system in Ruby, and it works fine but  
> > it’s slow. After poking around the code a bit, I think that the big  
> > bottleneck comes from loading all the required files. The system is  
> > extremely OO – more than 100 separate classes – and there’re a lot  
> > of classes that need to be loaded in with “require” statements before  
> > a single line is executed.
> > 
> > So now I’m looking around for ways to speed things up. Using modruby  
> > is probably an option, but I’m unsure as to whether that’ll do what I  
> > need … I know it keeps the Ruby interpreter in memory, but I really  
> > need something that might keep loaded classes in memory. Does modruby  
> > do that, or is that too much to ask? And failing that, what do people  
> > think would be the next best option for me to pursue?
> > 
> > Thanks in advance,
> > 
> > Francis

---

<div class="post-metadata">

**Author:** ![Nikodemus\_Siivola](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nikodemus\_Siivola](https://rubytalk.org/u/Nikodemus_Siivola)\
**Post date:** [5 September 2002 09:17 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/7 "2002-09-05T09:17:48Z")

</div>

Is there a reason why autoload just won’t cut it?

– Nikodemus

> **···**
>
> On Thu, 5 Sep 2002, Dave Thomas wrote:
> 
> > I’m still working on the start-up overhead, though, so if you come up

---

<div class="post-metadata">

**Author:** ![Patrick\_May](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_May](https://rubytalk.org/u/Patrick_May)\
**Post date:** [5 September 2002 13:27 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/8 "2002-09-05T13:27:34Z")

</div>

Joseph McDonald [joe@vpop.net](mailto:joe@vpop.net) wrote in message news:[155365737642.20020904165944@vpop.net](mailto:155365737642.20020904165944@vpop.net)…

> > So now I’m looking around for ways to speed things up. Using modruby  
> > is probably an option, but I’m unsure as to whether that’ll do what I
> 
> Take a look at fastcgi: [http://www.fastcgi.com/](http://www.fastcgi.com/)

Do you have a mirror of the ruby fast cgi bindings? Eli Green’s site  
has been down for some time…

thanks,

Patrick

---

<div class="post-metadata">

**Author:** ![Dave\_Thomas](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@Dave\_Thomas](https://rubytalk.org/u/Dave_Thomas)\
**Post date:** [5 September 2002 13:07 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/9 "2002-09-05T13:07:12Z")

</div>

Nikodemus Siivola [tsiivola@cc.hut.fi](mailto:tsiivola@cc.hut.fi) writes:

> > I’m still working on the start-up overhead, though, so if you come up
> 
> Is there a reason why autoload just won’t cut it?

That is a very good idea… It never occurred to be that autoload would  
work with Marshal.load, but of course it does.

Many thanks

Dave

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [5 September 2002 13:47 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/10 "2002-09-05T13:47:43Z")

</div>

When we are dealing with a “large” Ruby system, with a lot of live  
objects, doesn’t the Ruby standard mark-and-sweep GC create a potential  
overhead problem?

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![Brandt\_Kurowski1](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@Brandt\_Kurowski1](https://rubytalk.org/u/Brandt_Kurowski1)\
**Post date:** [5 September 2002 14:19 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/11 "2002-09-05T14:19:32Z")

</div>

i’ve got a copy at [FastCGI/Ruby](http://liber.sf.net/fastcgi.rb)  
but i’m not sure if it’s the most recent version.

> **···**
>
> On Thu, Sep 05, 2002 at 10:27:34PM +0900, Patrick May wrote:
> 
> > Joseph McDonald [joe@vpop.net](mailto:joe@vpop.net) wrote in message news:[155365737642.20020904165944@vpop.net](mailto:155365737642.20020904165944@vpop.net)…
> > 
> > > Take a look at fastcgi: [http://www.fastcgi.com/](http://www.fastcgi.com/)
> > 
> > Do you have a mirror of the ruby fast cgi bindings? Eli Green’s site  
> > has been down for some time…
> 
> –  
> Brandt

---

<div class="post-metadata">

**Author:** ![Matt\_Gushee](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@Matt\_Gushee](https://rubytalk.org/u/Matt_Gushee)\
**Post date:** [5 September 2002 14:38 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/12 "2002-09-05T14:38:14Z")

</div>

Does anyone know how much FastCGI actually boosts performance?

I’m working on an Apache-based application, and started out using  
FastCGI. But I switched to mod\_ruby because:

- my code becomes simpler
- installation and configuration are easier
- my application needs to control the HTTP response codes; and
- developers are starting to switch to Apache 2.0; mod\_ruby is  
available for Apache 2.0 now, but FastCGI isn’t

Nonetheless, I’d be interested in learning more about the performance  
difference.

> **···**
>
> On Thu, Sep 05, 2002 at 10:27:34PM +0900, Patrick May wrote:
> 
> > Joseph McDonald [joe@vpop.net](mailto:joe@vpop.net) wrote in message news:[155365737642.20020904165944@vpop.net](mailto:155365737642.20020904165944@vpop.net)…
> > 
> > > > So now I’m looking around for ways to speed things up. Using modruby  
> > > > is probably an option, but I’m unsure as to whether that’ll do what I
> > > 
> > > Take a look at fastcgi: [http://www.fastcgi.com/](http://www.fastcgi.com/)
> 
> –  
> Matt Gushee  
> Englewood, Colorado, USA  
> [mgushee@havenrock.com](mailto:mgushee@havenrock.com)
> 
> > **[Home - Havenrock](http://www.havenrock.com/)**

---

<div class="post-metadata">

**Author:** ![Patrick\_May](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_May](https://rubytalk.org/u/Patrick_May)\
**Post date:** [6 September 2002 00:49 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/13 "2002-09-06T00:49:32Z")

</div>

Matt Gushee [mgushee@havenrock.com](mailto:mgushee@havenrock.com) wrote in message news:[20020905143015.GA4536@swordfish](mailto:20020905143015.GA4536@swordfish)…

> But I switched to mod\_ruby because:
> 
> - my code becomes simpler

I am looking into integrating FastCGI support with the Narf web  
libraries. When I do, one will be able to use the same API for cgi  
scripts, mod\_ruby, and fastcgi. I.E. from the perspective of one  
using Narf using fastcgi will be as simple (the same) as writing a  
cgi-script.

This integration is currently vaporware, but is on my to do list.

~ Patrick May

---

<div class="post-metadata">

**Author:** ![Patrick\_May](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_May](https://rubytalk.org/u/Patrick_May)\
**Post date:** [6 September 2002 00:49 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/14 "2002-09-06T00:49:34Z")

</div>

Brandt Kurowski [brandt@kurowski.net](mailto:brandt@kurowski.net) wrote in message news:[20020905141354.GH1517@highway.kurowski.net](mailto:20020905141354.GH1517@highway.kurowski.net)…

> i’ve got a copy at [FastCGI/Ruby](http://liber.sf.net/fastcgi.rb)  
> but i’m not sure if it’s the most recent version.

Thanks! When I integrate fastcgi into narf, I will throw this into  
our project base so that we have a place to accept patches, etc.

~ Patrick

---

<div class="post-metadata">

**Author:** ![Matt\_Gushee](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@Matt\_Gushee](https://rubytalk.org/u/Matt_Gushee)\
**Post date:** [6 September 2002 01:01 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/15 "2002-09-06T01:01:17Z")

</div>

Interesting. What’s Narf?

> **···**
>
> On Fri, Sep 06, 2002 at 09:49:32AM +0900, Patrick May wrote:
> 
> > Matt Gushee [mgushee@havenrock.com](mailto:mgushee@havenrock.com) wrote in message news:[20020905143015.GA4536@swordfish](mailto:20020905143015.GA4536@swordfish)…
> > 
> > > But I switched to mod\_ruby because:
> > > 
> > > - my code becomes simpler
> > 
> > I am looking into integrating FastCGI support with the Narf web  
> > libraries. When I do, one will be able to use the same API for cgi  
> > scripts, mod\_ruby, and fastcgi. I.E. from the perspective of one  
> > using Narf using fastcgi will be as simple (the same) as writing a  
> > cgi-script.
> 
> –  
> Matt Gushee  
> Englewood, Colorado, USA  
> [mgushee@havenrock.com](mailto:mgushee@havenrock.com)
> 
> > **[Home - Havenrock](http://www.havenrock.com/)**

---

<div class="post-metadata">

**Author:** ![Patrick\_May](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_May](https://rubytalk.org/u/Patrick_May)\
**Post date:** [6 September 2002 18:31 UTC](https://rubytalk.org/t/options-for-optimizing-a-large-ruby-system/1569/16 "2002-09-06T18:31:59Z")

</div>

Matt Gushee [mgushee@havenrock.com](mailto:mgushee@havenrock.com) wrote in message news:[20020906005316.GH4604@swordfish](mailto:20020906005316.GH4604@swordfish)…

> > Matt Gushee [mgushee@havenrock.com](mailto:mgushee@havenrock.com) wrote in message news:[20020905143015.GA4536@swordfish](mailto:20020905143015.GA4536@swordfish)…
> > 
> > > But I switched to mod\_ruby because:
> > > 
> > > - my code becomes simpler
> > 
> > I am looking into integrating FastCGI support with the Narf web  
> > libraries. When I do, one will be able to use the same API for cgi  
> > scripts, mod\_ruby, and fastcgi. I.E. from the perspective of one  
> > using Narf using fastcgi will be as simple (the same) as writing a  
> > cgi-script.
> 
> Interesting. What’s Narf?

[http://narf-lib.sourceforge.net](http://narf-lib.sourceforge.net)

Narf is an alternative CGI api that builds off of Wakou Aoyama’s  
standard cgi.rb. There were a number of changes we wanted to see in  
the CGI api, but it didn’t seem to make sense to try and push all  
those changes onto the standard api. So we created an experimental  
fork of CGI.

Narf is alpha right now. There is code, it does work, but there are  
still bugs and we are doing alot of refactoring as we flesh out the  
organization of the code. For example, recently we renamed the  
modules to Web instead of CGI, so that they can co-exist more  
peacefully alongside Wakou’s code.

There should be another alpha release soon; we am sorting out some  
build dependencies, and when that is done will be another release.

~ Patrick (and perhaps by proxy, Tom Clarke)

> **···**
>
> > On Fri, Sep 06, 2002 at 09:49:32AM +0900, Patrick May wrote:
