An analysis of C++ compiler devirtualization optimization capabilities and corner cases
By
lionkor
Kettled twice. Extra chewy, extra trustworthy.
Summary
This article explores C++ compiler devirtualization optimizations — when compilers can replace virtual function calls with direct calls. It examines how modern compilers reliably devirtualize calls to final methods, but also investigates various corner cases where devirtualization behavior differs across compilers. The author conducted experiments to understand the conditions under which devirtualization occurs and which compilers handle different edge cases.
Key quotes
· 3 pulledModern compilers devirtualize calls to final methods pretty reliably.
But there are many interesting corner cases — including some I haven't thought of, I'm sure! — and different compilers do catch different subsets of those corner cases.
Someone recently asked me about devirtualization optimizations: when do they happen? when can we rely on devirtualization? do different compilers do devirtualization differently? As usual, this led me down an experimental rabbit-hole.
You might also wanna read
Go Language Performance Improvements: Shifting Memory Allocations from Heap to Stack
The article discusses recent efforts by the Go programming language team to improve performance by shifting more memory allocations from the
Understanding Function Call Overhead and Compiler Inlining Optimization
The article discusses the performance implications of function calls in programming, explaining how compilers optimize code through techniqu
lemire.me·3mo agoHow a Production Bug in C++ Payment System Demonstrated the Real Dangers of Undefined Behavior
A software engineer shares a personal story about a production bug in a critical C++ payment processing system that taught them the real-wor
Compiler Optimization Surprises: Clever Loop Transformations That Impress Engineers
The article discusses compiler optimization surprises, specifically how compilers can perform clever optimizations that even experienced eng
Understanding Partial Inlining: How Compilers Optimize Functions with Fast and Slow Paths
This technical blog post explores partial inlining as a compiler optimization technique, explaining how compilers can selectively inline onl
How Compiler Optimizers Recognize Patterns and Simplify Obfuscated Code
The article discusses how modern compilers and optimizers can recognize patterns in code and simplify complex-looking operations into effici
