r/cpp • u/david-alvarez-rosa • Apr 23 '26
Devirtualization and Static Polymorphism
https://david.alvarezrosa.com/posts/devirtualization-and-static-polymorphism/
81
Upvotes
r/cpp • u/david-alvarez-rosa • Apr 23 '26
24
u/matthieum Apr 23 '26
You may be interested in the decade old serie of articles from Honza Hubička's explaining the partial devirtualization optimization he implemented in GCC at the time:
There is still some overhead compared to full devirtualization, BUT you do regain inlining for some calls, which is pretty cool!
16
u/User_Deprecated Apr 24 '26
`final` is usually the easy win, one keyword and the compiler handles the rest. In my experience though the bigger gain from CRTP is that once the call inlines, the optimizer suddenly sees the whole path. Constant propagation, dead branches, all that stuff starts firing. With a vtable boundary the compiler usually loses most of that visibility.
8
u/johannes1971 Apr 23 '26
The article is based on the premise that a single additional assembly instruction causes 'underperformance in benchmarks', yet it completely fails to present any kind of performance data. I'm reminded, somehow, of the story of Don Quixote, who was also known for fighting imaginary problems.
And sure, a memory load can be the cause of some slowdown. That assumes that the data is not already in cache, or needed soon anyway. Is that a realistic assumption?
3
u/SyntheticDuckFlavour Apr 24 '26
My only criticism of static polymorphism is that base class always needs to know about the concrete definition of the derived class in some capacity. So with the example given above, this is not possible:
11
u/Usual_Office_1740 Apr 23 '26
Deducing this feels like magic sometimes. Want to reduce code duplication for operator overloads or any member function that you want to write as both const or non const? How about free performance increases with less code? Deducing this to the rescue!
2
u/david-alvarez-rosa Apr 23 '26
Ever wondered why your “clean” polymorphic design underperforms in benchmarks? Virtual dispatch enables polymorphism, but it comes with hidden overhead: pointer indirection, larger object layouts, and fewer inlining opportunities.
Compilers do their best to devirtualize these calls, but it isn’t always possible. On latency-sensitive paths, it’s beneficial to manually replace dynamic dispatch with static polymorphism, so calls are resolved at compile time and the abstraction has effectively zero runtime cost.
17
u/_Noreturn Apr 23 '26
I really hate when I see static polymorphism stated as an altnerative to virtual polymorphism when 99.9% of the time I used virtual polymorphism is because I don't want the call to be known.
7
u/eeiaao Apr 23 '26
Here we go again
2
u/tokemura Apr 23 '26
Typical CRTP. It has been discussed on cppcon with all the problems https://m.youtube.com/watch?v=pmdwAf6hCWg&pp=0gcJCU8Co7VqN5tD
1
u/Unhappy_Play4699 Apr 24 '26
I always wonder what code that is, that is bound by virtual dispatch overhead. I think I have never written production code that performed notably worse because of virtual dispatch.
Even on embedded stuff you probably run into memory usage issues before virtual dispatch is producing notable overhead, I'd assume.
Edit: But maybe someone has a valid example.
1
u/angelicosphosphoros Apr 25 '26
All this template based replacements of virtual functions work only if you write all your code in headers. As soon as you want to put definitions into cpps, you are unable to use those tricks.