https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99386
Bug ID: 99386 Summary: std::variant overhead much larger compared to clang Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: mail at milianw dot de Target Milestone: --- I've come across some code in an application I'm working on that makes use of std::variant. The overhead imposed by std::variant compared to a raw type is extremely high (700% and more). I created a little MWE to show this behavior: https://github.com/milianw/cpp-variant-overhead To reproduce, compile two versions in different build folders with both g++ or clang++: ``` CXX=g++ cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo CXX=clang++ cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ``` Then run both versions: ``` perf stat -r 5 -d ./variant 0 perf stat -r 5 -d ./variant 1 perf stat -r 5 -d ./variant 2 ``` I put the measurements on my machine into the README.md. The gist is, the relative runtime overhead is huge when compiling with g++: g++ uint64_t: 100% std::variant<uint64_t>: 720% std::variant<uint64_t, uint32_t>: 840% clang++ uint64_t: 100% std::variant<uint64_t>: 114% std::variant<uint64_t, uint32_t>: 184% The baseline for both g++/clang++ is roughly the same.