r/cpp • u/AdMotor4869 • May 19 '26
Virtual dispatch isn't always the slowest, and std::variant isn't always the fastest
https://shubhankar-gambhir.github.io/posts/four-ways-to-dispatch-a-runtime-selected-strategy-in-cpp/I've been looking at how OpenJDK's GC barrier system picks its implementation at runtime using templates instead of virtual dispatch. The trick is lazy resolution: you pay once at first use instead of a vtable lookup on every call.
That got me curious enough to benchmark it against three other approaches: virtual functions, function pointers, and std::variant + std::visit. I was surprised to see std::variant being the slowest on libstdc++ while virtual dispatch beat it comfortably.
Please refer to the blog for my full analysis. Would love to hear what you think!
Edit: Benchmarks are on GCC 11 (Ubuntu 22.04 default). GCC 12+ significantly improves std::visit. Full compiler version comparison in the next post.
90
u/induality May 19 '26
I think when people complain that virtual methods are slow, they’re saying it’s slower than static dispatch. I don’t think people generally think virtual methods are slower than other types of dynamic dispatch. In fact, intuitively speaking, it makes sense that virtual methods are the fastest form of dynamic dispatch, since it would probably be the one most heavily optimized by the compiler.
37
u/f382 May 19 '26
Note that inheritance with virtual functions and std::variant solve different problems from a design perspective (open set of types vs. open set of operations). In large software projects it is therefore usually wrong to choose one or the other purely on a performance basis.
15
u/sfider_sky May 19 '26
Why not gcc 12+? The output for
std::variantlooks a lot cleaner, so it's probably more perfomant.6
u/cdb_11 May 20 '26
This benchmark doesn't measure branch misses. It creates a single object, and calls the same virtual function on that same object over and over again. So it's not the full story. Create a lot of objects of random types, and then do dynamic dispatch on every one of them.
5
u/User_Deprecated May 20 '26
Honestly the bigger question is whether the dispatch key is actually a narrow integer space.
For FIX parsing the MsgType is just an ASCII byte, so I ended up ditching variant/virtual/switch and just used a 256-entry lookup table indexed directly by the byte. Benchmarked it against the switch version on the same hot path (17 message types). Sequential was ~70% faster, random was somewhere around 75-80% depending on the run. Random is where you really see branch prediction stop helping the switch.
For SIMD level selection (scalar/AVX2/AVX-512) it's basically the same idea as the OpenJDK thing. No CPUID in the hot path, so resolve once at startup via call_once, then it's just one indirect call forever.
Variant vs virtual didn't really show up in profiles after that.
13
u/Nolia_X May 19 '26 edited May 19 '26
I feel something is misleading in the benchmarks. In the code showed in the article, every virtual dispatch is straight forward and can be optimized. There are no examples of virtual dispatch on undeduceable types at compile time in the article.
Edit: I was wrong, this has been taken into account
17
u/pigeon768 May 19 '26
This article is trash.
std::variantwas released as part of C++17, and gcc 11, released in 2021, was the first version of gcc to default to C++17.std::variantsupport was very early days in gcc 11. It is true that gcc 11's support for std::variant was bad. But in 2022, when gcc 12 was released, you know what they did? They fixed it.Right now, four fucking years later, the latest version of gcc is up to version 16. Neither gcc 11 nor gcc 12 are supported anymore. This guy writes a blog post benchmarking a feature that was, for all intents and purposes experimental in gcc 11, but had its bugs ironed out in gcc 12 (which, again, is also no longer supported) and comes to the conclusion that the feature is bad.
Nobody ships gcc 11 anymore. It's dead unsupported software. So is gcc 12. OP went out of his way to dig up an old ass compiler that produced bad code in this specific circumstance, and made a broad, sweeping generalization based off of that.
Shame on you OP. Shame.
3
u/azswcowboy May 19 '26
Why no -O3? Maybe it doesn’t matter here but curious if you tried.
1
u/sweetno May 19 '26
Of course you do dynamic linking, that is, manually rewrite every call site with the desired function address!
3
u/arihoenig May 19 '26
Virtual dispatch isn't the slowest if the compiler devirtualized it, but then it isn't virtual dispatch
1
u/HarrierDuBoisX May 22 '26
Are there any comprehensive benchmarks comparing static and dynamic dispatch techniques (vtables, CRTP, std::variant, function pointers)?
1
u/Zeh_Matt No, no, no, no May 20 '26
I think comparing std::variant to virtual functions is like comparing apples and oranges, std::variant is to replace the unsafe union usage and not replace virtual functions.
-1
u/MarcoGreek May 20 '26
The value of this kind of post is diminishing. I used claude to compile different implementation(std::visit, std::get_if, dynamic dispatch) on compiler explorer with different compiler in different versions.
After reading the report I checked myself because the AI is simply too often misleading. It showed very well that std::visit improved quite well over time and is now the best compromise for a closed set, especially on MSVC.
The value of articles like this would be in checking the output and give a good summary. Not in posting one data point and making a story out of it.
-4
u/zerhud May 19 '26
std variant is bad (in gcc and clang): it seems it uses if chain. There is variant implementations with switch case blocks, it faster. Also with cpp26 we can dispatch with goto labels array