MOO-cows Mailing List Archive


[ Re: Server Improvement]

------- Start of forwarded message -------
Date: Tue, 18 Mar 1997 10:51:59 PST
From: Gevan Dutton <>
X-Sender: shanos@rowanark
To: chris <>
cc: MOO-COWs <>
Subject: Re: Server Improvement
In-Reply-To: <Pine.SUN.3.91.970318112846.9663C-100000@t9>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Precedence: bulk

On Tue, 18 Mar 1997, chris wrote:

> Switch statements in C are meant to be very efficient (although that 
> might change from compiler to compiler), and the MOO server uses a switch 
> statement to select and execute the MOO language operands.
> As the server spends so much time executing this code (located in the 
> function run() in execute.c) any improvement to its performance would 
> have a significant impact on the performance of the site.
> What I would like to put up for discussion is the merits of using a array 
> of pointers to functions instead of a switch statement, as this would 
> eleminate the need to perform a integer search.
> The server already uses enumerations in a switch statement, the 
> enumerations are integer codes which signal performing a addition 
> operation or to pop a value from the stack and so forth. If the code for 
> each operation was inserted into a function, and the address of the 
> function stored in a array then the value of the enumeration could be 
> used as a offset to retrieve the address of the function which should be 
> called. Surely a quick sum is quicker to compute than a search of a 
> integer list?
> Do people think that this would result in a significant performance boost?

In gcc, and I assume in a lot of other compilers, switches are 
generally handled more efficiently than this, anyway.
A switch on a fairly complete sequence of integers generates an array lookup
instead of a search.  Entries in the array are points to jump to, not 
functions to call, so this is a little more efficient.  A quick look at
the output of gcc -S -O execute.c suggests it's doing this:

        jmp *.L1724(,%ebx,4)
        .align 16
        .align 4
        .long .L241
        .long .L241
        .long .L241
        .long .L1228
        .long .L1228
... and so on.

I would consider this a fairly standard feature of a C compiler, and I 
think switch is limited so that it can be implemented easily this way.
Well, there's my contribution.  You might get a performance boost, but I'd
say probably a loss in performance with gcc, because of the overhead of
all those function calls.
------- End of forwarded message -------

Home | Subject Index | Thread Index