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

Soft Linden soft at lindenlab.com
Mon Jul 2 20:33:24 PDT 2007


With Pentiums, virtual function calls can put the brakes on
speculative execution and branch prediction. I'm not up to date on the
effect on newer processors, but I'd bet costs remain. If you see
unnecessary virtual functions in code that gets called a *lot*, it's
probably worth cleaning up.

On 7/1/07, Nicholaz Beresford <nicholaz at blueflash.cc> wrote:
>
>
> 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/
> _______________________________________________
> Click here to unsubscribe or manage your list subscription:
> /index.html
>


More information about the SLDev mailing list