We're open-source, so it'll be whoever puts the effort into contributing a production-quality implementation. These days I don't end up Thanosing a lot of features.
Genuine question how many people are working on the STL at Microsoft? Cuz you guys all seen way overworked... (It doesn't help also that the past three major versions of the standard have all been super huge)
a hive is a linked list of independently allocated memory blocks.
So is std::deque - what's the difference?
No random access
Ah, that's a difference, making hive closer to a linked list than deque is, as deque can still invalidate pointers that aren't the first or last element.
Pointer stability on erase...
One I thing I wonder, if you have a pointer to one of your game object's, is whether there's any way to map that pointer back to the corresponding iterator for deletion, short of a full scan, as it seems it would be possible for the class to figure that out (detecting which block it's in first, then computing the iterator from the pointer difference from the block base) a lot faster than manually looping through begin/end. Never mind, get_iterator.
An advantage of std::hive is that it has better "cache locality" (but not as good as std::vector), i.e. it stores elements close together in memory which makes accessing the elements faster, especially when iterating over them in order.
The block size of std::deque is often too small. std::list is even worse because the elements could be stored all over the place.
A deque is reasonably dissimilar to a hive - being a double-ended queue, it requires a different internal framework. In addition, being a random-access container, having a growth factor for element blocks in a deque is problematic (though not impossible). deque and hive have no comparable performance characteristics except for insertion (assuming a good deque implementation). Deque erasure performance can vary substantially depending on implementation, but is generally similar to vector erasure performance. A deque invalidates pointers to subsequent container elements when erasing elements, which a hive does not, and guarantees ordered insertion.
One of the key properties is that hive can have holes between elements. When you erase an element, you are left with a hole and the elements before or after it are not shuffled along to fill the hole. This means that elements have stable addresses once inserted. And on the next insertion any hole can be used as the insertion point, instead of adding a new element at the end.
Yeah - honestly I have to read up on it how far the comparison holds.
Another angle is: if you only insert default-constructed T's, it acts like a fixed-size allocator - and the performance guarantees seem to be modeled after that.
(memory locality alone could be a big enough plus to warrant yet another container - but from spurious reads, I'm not sure if it won't suffer from delocalization - that it can reach a state where inserting 10 new elements will spread them all over memory)
I asusme it does, yes - but then it depends on the insert/delete pattern how long it takes to become de-localized. if the earliest free slots are in chiunk 1, 12 and 34, they'll be inserted there.
I mean, that's a deeply researched issue in memory allocators - but that's also a topic I didn't really follow for the recent 10 years.
(The last thing I remember is the realization that "the more advanced your allocation scheme is, the worse the edge cases get. Simpler is better for general purpose." - so it might be the best we can hope for anyway..)
but then it depends on the insert/delete pattern how long it takes to become de-localized. if the earliest free slots are in chiunk 1, 12 and 34, they'll be inserted there.
It would only become "de-localized" if you removed a lot of elements. Inserting elements would make it more "localized" because it fills in the gaps.
If you had a lot of elements, and you removed most of them, then ...
you might still occupy the same amount of memory as at the peak but not more than that unless you exceed the number of elements you had before (similar to a std::vector), and
there might be more dead space between the elements but iterating through all the elements wouldn't take more time than before when you had more elements.
I'm thinking a game where you might use a hive to store game entitles. So if it was fast enough at the peak then it should be fast enough later when it's more fragmented but fewer elements. I assume one big hive that you iterate through each frame. I could see how having many smaller ones that grow big occasionally could be a problem (just like with vectors).
It wonโt replace std::vector for most use cases โ but for the use cases it is designed for, nothing in the standard library has come close before.
This could be true but it could also be an oversight because nobody has ever benchmarked a std::pmr::list that is constructed with a std::pmr::[un]synchronized_pool_resource.
With std::hive, the iteration order is optimal within each block in terms of cache locality.
With std::pmr::list, the iteration order would be independent of where the nodes are allocated (assuming you frequently add and remove elements at random positions) so the cache locality would probably be worse.
Seeing an actual benchmark would be interesting though.
If you fill up a hive with elements then start randomly removing and replacing them, and you perform the exact same sequence of events but with a pmr::list which uses a pool allocator, the memory layout should end more or less identical in both cases.
The biggest differences are small variations in the node layout for a hive node vs a list node.
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.
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)
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.
Huh? Am object pool is litterally a pool of objects you can re-use, and has nothing to do with the underlying data representation of how those objects are "pooled", just like a "container" is just a thing that holds other things.ย ย
"Each entity is stored in a container, and other subsystems hold pointers to those entities.", that is something you should avoid, good game engines use handles for entities which are typically a version and index and you have to never worry about stale pointers ever again.
I also have to add that I'm not a big fan of regular skiplists.
Sure, it allows advancing to the next item with a MUL (or SHL) + ADD, or perhaps single FMA instruction. But it just feels wasteful when the item's presence can be represented by just a single bit. The skipcount could then be retrieved by a simple SHR and LZCNT ...which might even be faster than the above.
I think you might have to do this multiple times, in a loop, in case there are many erased elements (unless you restrict the block size).
Is LZCNT even supported by all hardware?
Finding next used slot should need just a single LZCNT, no loop, and would maintain the iteration of a block to be O(1). What could need a loop is alternative trick using BSR, but I'd need to think on that a little.
The LZCNT was introduced with Phenoms on AM2+ (along with SSE4a) and Haswell in 2013. BSR was on 386 so I think that's safe.
Yeah, that would be significantly slower. Theoretically. Practically I'd guess that any attempt to measure real difference between 3 instructions and this 10 or so instruction loop (likely unrolled) would be completely obliterated by cache access latency and such.
Yep I was very surprised to see that it used RLE for the skiplist rather than using a bitset to determine which slots are occupied and which are not. I wonder what the cost of maintaining the RLE skiplist will be like if you are heavily inserting and deleting elements to the container...
Sad thing for me is this container being only one which is not constexpr in 26. It's because it went in similar timeline as my `constexpr containers` papers in, and then NB comment to fix this was rejected for being too late change.
Interesting thing about the implementation of it is various size of allocated blocks, which requires various space for skiplist/bitfield and data arrays, and C++ doesn't have dynamically sized structs. So what the prototype is doing is allocating array of structs, and then casting pointers to them to an unrelated structure type to have the skiplist/bitfield there.
It's for situations where you iterate over all elements frequently but don't care about the order, while elements are often being added/removed, and you need pointers/iterators to stay valid.
We use a in-house library that does exactly this. It's written for C, and we need to use placement new to allocate C++ object. Looks like we can finally replace it.
โข
u/STL MSVC STL Dev Sep 03 '26
This blog is substantially AI-generated. I'm wondering whether we should continue allowing links to it.
123
u/pepejovi Sep 03 '26
A tiniest bit of effort when it comes to introducing the post would be nice.
15
u/_DafuuQ Sep 03 '26
Its notable that boost also has an alternative to that which recently got accepted, its called boost::container::hub
11
u/Tringi github.com/tringi Sep 03 '26
I, for one, am quite interested how will the MSVC implementation turn out.
16
u/fdwr fdwr@github ๐ Sep 03 '26 edited Sep 03 '26
So is
std::deque- what's the difference?Ah, that's a difference, making hive closer to a linked list than deque is, as deque can still invalidate pointers that aren't the first or last element.
One I thing I wonder, if you have a pointer to one of your game object's, is whether there's any way to map that pointer back to the corresponding iterator for deletion, short of a full scan, as it seems it would be possible for the class to figure that out (detecting which block it's in first, then computing the iterator from the pointer difference from the block base) a lot faster than manually looping throughbegin/end.Never mind, get_iterator.4
u/ABlockInTheChain Sep 03 '26
This could be true but it could also be an oversight because nobody has ever benchmarked a
std::pmr::listthat is constructed with astd::pmr::[un]synchronized_pool_resource.3
u/all_is_love6667 Sep 03 '26
I think that's usually called an "object pool", but that was not academic enough so I can understand why they called it hive
3
u/Zeh_Matt No, no, no, no Sep 03 '26
"Each entity is stored in a container, and other subsystems hold pointers to those entities.", that is something you should avoid, good game engines use handles for entities which are typically a version and index and you have to never worry about stale pointers ever again.
3
u/Tringi github.com/tringi Sep 04 '26
I also have to add that I'm not a big fan of regular skiplists.
Sure, it allows advancing to the next item with a MUL (or SHL) + ADD, or perhaps single FMA instruction. But it just feels wasteful when the item's presence can be represented by just a single bit. The skipcount could then be retrieved by a simple SHR and LZCNT ...which might even be faster than the above.
2
u/hanickadot WG21 + CTRE + EWG ๐ธ๐ป Sep 04 '26
Sad thing for me is this container being only one which is not constexpr in 26. It's because it went in similar timeline as my `constexpr containers` papers in, and then NB comment to fix this was rejected for being too late change.
Interesting thing about the implementation of it is various size of allocated blocks, which requires various space for skiplist/bitfield and data arrays, and C++ doesn't have dynamically sized structs. So what the prototype is doing is allocating array of structs, and then casting pointers to them to an unrelated structure type to have the skiplist/bitfield there.
1
u/kiner_shah Sep 13 '26
What's the use case for which std::hive was introduced in the first place?
1
u/Baardi Sep 18 '26
We use a in-house library that does exactly this. It's written for C, and we need to use placement new to allocate C++ object. Looks like we can finally replace it.
-1
u/LazySapiens Sep 03 '26
I can't recall an article which said it's too late for this, and there are better alternatives.