‹ BackHN Continuity

Thread

Writing Efficient C++ Code (2013)

176 points · 161 comments · ibobev

  1. hn_submit · · focus · HN ↗
    I write in C++ almost every day but never have the need to optimize for speed. Even when you write straightforward code it's already blazingly fast.
    1. glouwbug · · focus · HN ↗
      True, but moving from a list of unique polymorphic pointers to a std::variant gains you at least a 2-3x speed up in terms of TLB and cacheline locality. From there, swapping to SOA will net you another 4-8x, so you're looking at nearly 25x improvement by going data first. That may not matter in the unique case of say, games, where rendering a million entities will dwarf the cost of SIMD processing a million entities, but in something like numerical simulations (fluids) or quant it will be warmly welcomed
      1. cenamus · · focus · HN ↗
        Is the improvement from using std:variant vs polymorphism just due to the indirection you save on?
        1. glouwbug · · focus · HN ↗
          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).

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.