MOO-cows Mailing List Archive


[ Re: Server Improvement]

------- Start of forwarded message -------
Date: Tue, 18 Mar 1997 13:25:33 PST
From: "Robert J. Brown" <>
In-reply-to: <Pine.LNX.3.91.970319062515.21401B-100000@rowanark> (message from
	Gevan Dutton on Tue, 18 Mar 1997 10:51:59 PST)
Subject: Re: Server Improvement
Precedence: bulk

>>>>> "Gevan" == Gevan Dutton <> writes:

    Gevan> 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?

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

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

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

A function call with a signature of void (*func)(void) passes no
parameters, and returns no results.  Consequently, it is quite fast,
and generates a single subroutine call instruction, with no stack
pushing or popping other than the return address itself.  The overhead
here is a single memory cycle for call, and another single memory
cycle for returning.

- -- 
- --------  "And there came a writing to him from Elijah"  [2Ch 21:12]  --------
Robert Jay Brown III  1 847 705-0424
Elijah Laboratories Inc.;  37 South Greenwood Avenue;  Palatine, IL 60067-6328
- -----  M o d e l i n g   t h e   M e t h o d s   o f   t h e   M i n d  ------
------- End of forwarded message -------

Home | Subject Index | Thread Index