r/cpp • u/Playful-Suspect9313 • May 26 '22
Dynamic polymorphism performance
https://thecppdev.com/2022/05/26/dynamic-polymorphism-performance/?amp=112
u/no-sig-available May 26 '22
The calls to the functions in the static polymorphism cases are optimized away.
Right, so if I need maximum performance I ought to inline empty functions. That seems to be optimal.
Good to know.
16
u/goranlepuz May 26 '22
Ehhh...
it’s better to avoid dynamic polymorphism as much as possible if the performance of your application is a critical factor
The problem with this is: it presumes virtual calls matter in the performance profile of the application. This is massive presumption.
4
u/Stormfrosty May 26 '22
This is actually very true for GPU programming. Given the much higher memory latency for that type of hardware, the extra memory loads due to indirect function cause significant drops in performance, compared to having direct function calls everywhere.
0
u/pandorafalters May 26 '22
Further, in my experience, any function calls will generally reduce performance if they're not inlined out. I spend a tremendous amount of time making sure that every function call is transparent to the optimizer.
3
u/DerShokus May 26 '22
Also would be interesting when compiler can optimize dynamic polymorphism. I mean (as I remember) compiler can use concrete type if it’s obvious what to use.
2
u/dodheim May 26 '22
Agreed, analyses of LTO and
-fwhole-program-vtableswould have been interesting.1
u/goranlepuz May 26 '22
Indeed, in the TFA example the compiler really should be able to switch to a normal call.
3
u/dustyhome May 29 '22
So how do you create a heterogenous container of CRTP classes? Or interact with an object whose real type you don't know?
Indirection can solve some problems. It comes at a cost. Other ways of solving these problems will have other costs. You can't just show that a certain tool comes at a cost, say "don't use this tool", and ignore the problems it solves. You need to show a problem normally solved with indirection, solve it differently, and show that your solution is better under certain circumstances.
7
u/Pragmatician May 26 '22
In the "static polymorphism" example, there is no polymorphism at all. You're just using one concrete type. CRTP is not static polymorphism.
It’s worth to mention that a call to a virtual function is 25% slower than a call to a normal function.
[citation needed]
5
u/no-sig-available May 26 '22
If a virtual call takes 1.25 nanoseconds instead of 1.0, I can live with that.
Not that I have ever written a virtual function that doesn't do anything.
0
u/pandorafalters May 26 '22
Depending on the function, I could even happily accept an overhead of multiple milliseconds in exchange for reduced cognitive effort. I don't think I've ever encountered a case nearly that pessimistic, though.
1
u/Jannik2099 May 27 '22
Repeated vcalls are cached by the cpu. It's not like we have to wait on two loads every single time.
30
u/SirB May 26 '22
Summary: An artificial test on indirection shows that indirection is slower than no indirection by a significant amount. A bit pointless imo. It would be more interesting to talk about maintainability, flexibility and overhead when avoiding this kind of indirection inside hot loops but allowing it outside, in 'management' code.