|
Boost : |
Subject: Re: [boost] Request For a feature - Templated virtual functions
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2009-02-12 19:34:57
Simonson, Lucanus J wrote:
> 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.
>
This reminds me of a discussion that came up when Steven Watanabe's
Switch library was reviewed.
http://lists.boost.org/Archives/boost/2004/08/69787.php
-- Michael Marcin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk