‹ BackHN Continuity

Thread

Writing Efficient C++ Code (2013)

176 points · 161 comments · ibobev

  1. asveikau · · focus · HN ↗
    This article reminds me of performance advice I was starting to see in the 2000s decade. Basically it was to not introduce a bunch of pointer heavy data structures to get lower algorithmic complexity. Stuff it all into a vector. You will use some algorithms that the computer science textbook will say it's slower, but if it fits all in cache it doesn't matter. The cache misses following pointers all over town hurts you more.
    1. bluGill · · focus · HN ↗
      While this advice isn't wrong, it is misleading. In my benchmarks std::map beats vector after 9 elements. Less than that and linear search is better but branch prediction and cache loading is very good.

      Run your own benchmarks on your own data of course. Also map is not considered the best key value store.

      1. mandarax8 · · focus · HN ↗
        Because in your benchmark all std::map nodes were allocated in succession, most likely being placed in adjacent memory locations...

        This likely won't be true in a real application with a non-trivial allocation pattern.

        1. einpoklum · · focus · HN ↗
          Actually it really depends, because allocators can also be kind of smart (and you don't have to use the default allocator).

          And then, on the other hand - I really doubt GP's map beats a vector, with all of those pointers bins and stuff, in a non-contrived benchmark with 10 elements.

          Finally - it's not either-or: There are better hash maps whose memory is sequentially allocated and/or are otherwise cache-aware. And there are data structures geared towards parallel execution on multiple threads; and towards SIMD; etc. etc.

        2. bluGill · · focus · HN ↗
          That might or might not be true in the real world. Often in my applications I'm creating at startup and then referencing later.

          Still a custom map that allocated a bunch of nodes would be a useful optimization.

      2. danbolt · · focus · HN ↗
        I’ve definitely been on teams where they ran the numbers, and found that they were mostly working with smaller containers, and std::vector was the way to go.

        If you’re down to that sort of decision-making, you have to measure.

      3. someonebaggy · · focus · HN ↗
        That is not a plausible result, sorry. Perhaps you're using the painfully slow MSVC debug mode vector?
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.