[sldev] Question on Overuse of virtual in C++

Nicholaz Beresford nicholaz at blueflash.cc
Sun Jul 1 16:44:58 PDT 2007



Dirk Moerenhout wrote:
>> On the design side however, unless you are dealing with
>> extremely performance sensitive code, if in doubt, it's
>> usually a good idea to make functions virtual and not need
>> it than the other way round.
> 
> Well we're dealing with performance sensitive code as it's one of the
> top functions (when using an optimised viewer) even though it has only
> 4 instructions (including RET ...). I get upto 2% of the cycle samples
> in those 4 instructions and that doesn't take into account all the
> wasted cycles on additional instructions that are needed to do the
> call.

There's definitely overhead in the call itself too.  I think the
pointer to the virtual function map is the first or second member
in the objects, so the call is something like

   this->vfptrs[<number of virtual function>](<parameters>);

which I think I've seen translated into two or three machine
instructions, including the call.


If it is speed sensitive and if you can't find an overload there
(which should be easy to spot by the same name), try it non-virtual.


Nick


Second Life from the inside out:
http://nicholaz-beresford.blogspot.com/


More information about the SLDev mailing list