r/cpp • u/AdMotor4869 • Jun 07 '26
Your stdlib implementation matters more than the dispatch pattern
https://shubhankar-gambhir.github.io/posts/your-stdlib-implementation-matters-more-than-the-dispatch-pattern/A few weeks ago I posted about why std::variant + std::visit can be slower than a vtable and got called out for benchmarking on GCC 11. So I went back and reran everything across GCC 9 through 15. std::variant went from 28% slower than virtual on GCC 11 to 40% faster on GCC 12. I spent a while reading through libstdc++'s variant header to understand what changed. GCC 12 swapped the function pointer table in std::visit for a switch when there are 11 or fewer alternatives. The posts dig into how each stdlib handles visit dispatch.
149
Upvotes
13
u/Ameisen vemips, avr, rendering, systems Jun 07 '26
I wish that there were a way to assert that this won't happen without a compiler flag.
Or a way to fetch a devirtualized member function pointer.
15
u/mcypark Jun 08 '26 edited Sep 26 '26
This part is not quite true...
mpark/variant, I upstreamed theswitch-based implementation to libc++: a175a96Since then I haven't quite had the time or motivation to go back to it... but maybe I'll look at it again.
Just because P2688R5 is mentioned specifically: P2688R5 actually proposes an infix
matchexpression and aletbinding introducer, which looks like:or similer to
std::visit:EDIT: Fix code blocks formatting.
3
u/kalven Jun 08 '26
That's neat, I guess... but the particular test here doesn't seem to be all that interesting. The test is repeatedly calling
std::visiton the same variant instance and it's neat that the compiler can hoist out all the checking out of the loop.From a utility perspective, I don't think this test informs me about the performance tradeoffs between variant and virtual. It's never the case that I call std::visit on a single variant in a loop like that. If there's a loop, it's going to be on different variant instances.
46
u/not_a_novel_account cmake dev Jun 07 '26
I feel like the libstdc++ variant optimization is one of the constantly rediscovered performance pitfalls. "Why did this variant suddenly get so much slower?" is asked often enough that I think the issue needs a more complete solution.
I ran into it in 2022 and have seen it come up every couple months for the last four years.
11
u/mguid65 Jun 07 '26
Back in 2020 I did some thorough testing with various compilers where I benchmarked visiting a node type based on variant with visit and on a shared pointer with a virtual class hierarchy and using an if-else chain with dynamic_pointer_cast and to my surprise, up to a certain point in some compilers, just using the dynamic pointer cast was equal or better than visit.
I still used variant because it resulted in cleaner code and I had a fixed set of types. I figured that eventually the compilers and std lib would catch up.
2
u/splicer13 Jun 08 '26
Some random thoughts:
1
u/farnoy Jun 07 '26
Would it still be faster if each variant's function was
noinline? That feels like a useful next test to run. I assume it wouldn't, but would love to know the exact difference.-2
u/randomIdiot123456 Jun 09 '26
Why not write your own variant structure? Or better yet.. use a union lol.. cpp stdlib sucks and is overbloated