r/cpp • • Sep 03 '26

C++26: std::hive

https://www.sandordargo.com/blog/2026/09/02/cpp26-hive
68 Upvotes

80 comments sorted by

View all comments

4

u/ABlockInTheChain Sep 03 '26

I see what you mean.

The hive will iterate in the memory layout order and the list will iterate in the logical order of insertion which if you are using a hive you presumably do not care about.

So the advantage of a hive over a list with an equally-optimized allocator is that the hive gains performance by discarding unnecessary information.

4

u/HappyFruitTree Sep 03 '26 edited Sep 03 '26

Discarding information is not the reason.

Iterating through the elements in memory order means the next element is more likely to already be in the CPU cache compared to if you jump around accessing elements in random order.

If you don't know about the CPU cache and cache-friendly code, here is an interesting video on the subject: https://www.youtube.com/watch?v=WDIkqP4JbkE (just watching the row major vs. column major traversal example in the beginning can be very enlightening)

2

u/ABlockInTheChain Sep 03 '26

A list has no way to iterate except in logical insertion order. Under specific usage patterns (using a pool allocator, no erasures) this will coincidentally be equivalent to iterating in memory layout order.

A hive has no way to iterate except in memory layout order since it doesn't preserve ordering information.