r/cpp • • 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!

The benchmarks results can be found here.

35 Upvotes

21 comments sorted by

View all comments

Show parent comment

2

u/EstablishmentHour335 Mar 27 '26

They are interesting benchmarks, although there is a couple of things that could be done to make them more informative, Matthew Bentley (plf::colony creator) has a good writeup (https://plflib.org/blog.htm 24-12-2024 - A few tips for correct benchmarking).

I would especially benchmark them separated into their own programs, rather than compiled into the same executable. I noticed with the static slot map especially, it would alter the performance of the other containers when compiled into the same executable in my testing.

Another thing is, I would test sparsity and fragmentation, plf::colony is supposed to be good at that.

I actually got really interested in the static slow map, what exactly is it doing at compile time? It does bloat my compile times with huge element counts, but it has really great performance.

1

u/sporacid Mar 27 '26

I'll see if I can find some time to add other benchmarks to represent other type of workload.

I'm surprised it bloats your compile time, I would be very interested as well to understand why, especially the part where it seems to scale with map capacity. Theoretically, it's not doing much at compile time, only figuring some optimal block size and bitset depth. Most functions are constexpr, but I would assume most slot map workloads to be exclusively resolved at runtime. I've made it constexpr just because I could. Let me know if you find anything.