r/cpp • u/Xaneris47 • Mar 02 '26
Devirtualization and Static Polymorphism
https://david.alvarezrosa.com/posts/devirtualization-and-static-polymorphism/
58
Upvotes
r/cpp • u/Xaneris47 • Mar 02 '26
View all comments
32
u/matthieum Mar 02 '26
Back in 2014, Honza Hubička (GCC developer) wrote an entire serie on devirtualization, and the work carried out in GCC to enable partial devirtualization.
You can find the first part here.
The key idea of partial devirtualization is that even if you don't know for sure that
bis of typeDerived, you can still check, and statically dispatch the call if the check is successful:This means that even if the optimizer has only a partial view of the type hierarchy -- a typical case in libraries -- it can still inline calls to the non-final virtual methods it knows of.
Of course, this isn't always beneficial, so as with all compiler optimizations, there's going to be some heuristics to pick which types are worth branching on, and which are not.