r/cpp • u/sporacid • Mar 21 '26
Slot map implementation for C++20
I've just finished submitting the initial version of my slot map implementation, based on this post. A slot map is a data structure that provides stable and versioned keys to stored values. Inserting into the map creates and return a unique key, which stays valid until the slot is explicitly freed.
I hope someone will find this useful :)
https://github.com/sporacid/slot-map
EDIT: As suggested by some of you, I've added benchmarks to compare to plf::colony. As i did those benchmarks, I had to work on a bunch of optomizations to compete, and I've introduce a static storage that has a fixed number of slots, and I kept the original dynamic storage with a fixed number of blocks, but the blcoks are allocated as needed. Let me know what you think!
View all comments
Show parent comment
2
u/sporacid Mar 23 '26 edited Mar 23 '26
I won't be able to tidy up and merge the branch in the coming days , but I got the benchmarks results! I had a few performance issues here and there, so I did a few optimizations. One optimization was to parameterize the storage to have static and dynamic storage available. As expected, pre-allocating a huge block of memory (static storage) is faster! The library is a tad bit slower on reads than
plf::colony. That is to be expected, sinceplf::colony::iteratorsimply dereference a pointer, where-as my library re-fetch everytime. However, since reference/pointer are stable, we can achieve the same performance by caching the pointer. Also, I wasn't able to testplf::colonyin the multithreaded benchmark I've written, since it's crashing somewhere, so I don't have any multi-threaded results for it.Here is the link to the raw results, they still need to be described and formatted, but it gives a rough estimate of what to expect.
Link to the results: https://github.com/sporacid/slot-map/blob/sporacid/benchmarks/README.md#benchmarks
Link to the benchmarks: https://github.com/sporacid/slot-map/blob/sporacid%2Fbenchmarks/benchmarks%2Fbench.cpp