Feature Request: nontrivial functions and vtable optimizations

Craig Black cblack at ara.com
Tue Aug 12 07:46:21 PDT 2008


"downs" <default_357-line at yahoo.de> wrote in message 
news:g7ribh$1vii$1 at digitalmars.com...
> The overhead of vtable calls is well known.
>
> For this reason, it may be worthwhile to include an optimization that 
> essentially checks if a certain class is only inherited from a few times, 
> like, <= three, in the visible program, and if so, replace vtable calls 
> with a tree of classinfo pointer comparisons and direct function calls.
>
> When I did this manually in some hobbyist path tracing code, I gained 
> significant speed-ups, even though I didn't collect any hard numbers.
>
> So, for instance, the equivalent D code would look like so:
>
> class A { ... } class B : A { ... } class C : A { ... }
>
> A a = genSomeObj();
> // a.test();
> if (a.classinfo == typeid(B)) (cast(B)cast(void*) a).test(); // call as if 
> final
> else if (a.classinfo == typeid(C)) (cast(C)cast(void*) a).test(); // dito
> else a.test(); // fallback
>
> Ideas?

You are right.  Function pointer invokation tends to slow down processing 
because it doesn't cooperate well with pipelining and branch prediction.  I 
don't understand why this is so hard for some programmers to accept.

See Microsoft Visual C++ profile guided optimizations (PGO) "Virtual Call 
Speculation". Using PGO, the most commonly called virtual functions are 
optimized so that they do not require a function pointer invokation.

Rather than relying on PGO, I would prefer the ability to assign a 
priorities to virtual functions in the code.  This would indicate to the 
compiler what virtual functions will be called the most.

-Craig 





More information about the Digitalmars-d mailing list