[comp.lang.c] function pointer overhead?

bglenden@mandrill.cv.nrao.edu (Brian Glendenning) (12/11/90)

Someone once told me that calling through a function pointer can cause
significant overhead. Is this true? If so, for what types of machines
is it true?

Brian
--
       Brian Glendenning - National Radio Astronomy Observatory
bglenden@nrao.edu          bglenden@nrao.bitnet          (804) 296-0286

jfc@athena.mit.edu (John F Carr) (12/11/90)

In article <BGLENDEN.90Dec10224133@mandrill.cv.nrao.edu>
	bglenden@mandrill.cv.nrao.edu (Brian Glendenning) writes:

>Someone once told me that calling through a function pointer can cause
>significant overhead. Is this true? If so, for what types of machines
>is it true?

Most processors' call instructions are optimized to work well with constant
addresses.  On the VAX 3 series, I found the call instruction to be slightly
faster with a constant address than with the address in a register.  The
call instructions on the 2 IBM RISC machines I use (RT and S/6000) take
equal times for calls to constant or variable addresses, but the calling
convention forces an extra memory reference for calling through a pointer
(on the RT, a function pointer is really a pointer to a word which contains
the address of the first instruction in the function; if the function
address is constant the lookup of the instruction address can be optimized
out).  Using a function pointer will probably decrease code size.

I don't know of any architecture where the difference between calling a
constant function address and calling via a function pointer is noticable in
normal use (on the IBM RT, the difference is at most 5 CPU cycles = 500 ns).
If you really care, you should profile your program on the machine you will
be using.

--
    John Carr (jfc@athena.mit.edu)

bls@u02.svl.cdc.com (Brian Scearce) (12/14/90)

bglenden@mandrill.cv.nrao.edu (Brian Glendenning) writes:
>Someone once told me that calling through a function pointer can cause
>significant overhead. Is this true? If so, for what types of machines
>is it true?

With our compiler on our machine, you can slow code down by using
pointers to functions -- but not in the obvious way.

For static functions that don't have their addresses taken, parameters
are passed in registers.  If you take the address of the function,
it might be called from without, and we can't do this neat
optimization.

--
     Brian Scearce (bls@robin.svl.cdc.com  -or-  robin!bls@shamash.cdc.com)
        "How do you explain the vast discrepancy between your testimony
         and my client's?" "He has perjured himself." "Objection!" "I am
         under oath.  I was there.  He *is* lying."
 Any opinions expressed herein do not necessarily reflect CDC corporate policy.