On Sat, 6 Oct 2001, Simon Cozens wrote:
> I always seem to work on the assumption that "function calls are
> always very costly", but that might not be true these days.
On i386, parameters are added to a list using PUSH. then you use CALL to
perform the call. The receiving end does ENTER to set up a stack
marker. [BP] is the stack marker of the caller, [BP+4] is a pointer to the
next instruction in the caller, [BP+8] etc are the arguments passed by
the caller, [BP-4] etc are the new local variables. To return, you call
LEAVE then RET and the caller does ADD SP,4n where n is the number of
arguments. All that stuff, supposing a cache hit, is not much more than a
clock cycle per instruction, and some might be pairable. If you get many
cache misses, the cost of calling instructions is probably irrelevant.
On SPARC, registers are all in banks of 8, and you have 3 banks directly
on the stack. Passing parameters is like assigning to the last bank, and
calling is a optimized instruction that calls and cycles 2 banks out and 2
banks in. Reading parameters is like reading registers. I may be slightly
bullshitting here, but that's about it.
On all modern processors though, registers and the L0/L1 cache (whichever
is the closest) are equally fast (?) so those two processors do
essentially the same thing, and your biggest time consumption is when you
have a cache miss.
All this to say that I think calls are not too expensive. However I did
not talk about indirect calls. The gap between direct and indirect calls
seem to widen. Also, branches ("if", "unless", "while", etc) seem to be
something difficult to optimize.
________________________________________________________________
Mathieu Bouchard http://hostname.2y.net/~matju