Subject: Re: [boost] Request For a feature - Templated virtual functions
From: Gokulakannan Somasundaram (gokul007_at_[hidden])
Date: 2009-02-13 02:26:33
On Fri, Feb 13, 2009 at 12:07 AM, Simonson, Lucanus J <
> Mathias Gaunard wrote:
> > Niels Dekker - mail address until 2010-10-10 wrote:
> >> Mathias Gaunard wrote:
> >>> A switch-case is actually faster than a virtual table lookup,
> >>> however.
> >> Is that independent of the number of cases?
> > Both are constant-time.
> > Nothing prevents a compiler to implement a switch/case with virtual
> > functions, (well actually scope might be an issue, but
> > circumventable),
> > but typically it will do smarter things that do not prevent inlining
> > for example.
> Older versions of gcc actually create a jump table to compiler generated
> out-of-line functions, which show up as weak symbols. This could cause
> performance problems and the fix was generally to rewrite the code with if
> statements. To my imperfect knowledge, newer versions of gcc often compile
> switch/case to a conditional branch tree if the number of cases is small.
> branch_cost * log(num_cases) is constant if you bound num_cases to a
> constant. I'm not sure exactly what the compiler does when the number of
> cases is large, it is very rare. In any case, if you are using virual
> functions at all you've got much bigger performance penalties than vtable
> lookup itself or switch/case overhead. Virtual functions usually can't be
> inlined and out-of-line function calls make it impossible for the compiler
> to make assumptions that allow it to optimize code following the function
> call based upon information it gathered before the function call. This
> leads directly to the misconception
> that C++ is slower than C, and that Java is as fast as C++, since C++
> written in a Java style effectively ties the compiler's hands.
> Thanks for the nice explanation. After your argument, i tried the code in
Visual Studio and the Boost Variant was commandingly fast. I reran the code
in g++ and it was equal. But what we are working with is the latest version
of g++. I am planning to try this out in Sun Studio compiler and i will get
back with the results. Please have a look at the code and i think it can't
be more simpler.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk