An example of polymorphism preventing compiler optimization?

I can’t remember where I saw this, but somewhere I read that dynamic polymorphism does not allow the compiler to make various optimizations.

Also, can anyone deepen me with any examples of such “missed” optimization opportunities that polymorphism prevents the compiler from creating?

+4
source share
2 answers

FROM

Derived d;
d.vMethod(); // that will call Derived::vMethod statically (allowing inlining).

C (if only one of Derivedor is Derived::vMethoddeclared finalin C ++ 11):

void foo(Derived& d)
{
    d.vMethod(); // this will call virtually vMethod (disallowing inlining).
}

A virtual call has an additional cost (as an indirectness via vtable).

++ 11 final, .

+2

, ++ . , List. ..

Poly.vmethod() , vmethod() , Poly- > vmethod() - . (, , . .)

(RVO) - , . RVO : , "" . .

0

Source: https://habr.com/ru/post/1526258/


All Articles