logoalt Hacker News

cenamus • today at 5:16 PM • 1 reply • view on HN

Is the improvement from using std:variant vs polymorphism just due to the indirection you save on?


Replies

glouwbug • today at 5:57 PM

That, and it frees the compiler from reasoning about virtual inlining, and that the std::variant approach can pack potentially more than one object into a single cacheline. TLBs also work with 4096 byte pages, so 32 polymorphic 128 byte entities may (at the absolute worst case) use 32 distinct pages which requires 32 TLB virtual translations, while the std::variant one uses 1.

The next step of going SOA benefits from all of the above, it just further unlocks you packed quad and oct instructions (AVX256 and 512 depending if you buy AMD or not).