How Google Sparsehash achieves two bits of overhead per entry using sparsetable
smerity.com
smerity.com
Of course, this doesn't do away with the need to resize, so it might not be appropriate for low latency or memory constrained systems. I wonder whether a tree-based data structure like a crit-bit tree or a HAMT might be able to do similar things to sparsehash with better performance.
If you are looking for low memory overhead ordered sets/maps, then B-Trees can provide that.
Or are these magic bits where zeroes are free?
Also, I would not reallocate, but allocate at max size or, possibly, half size and grow if needed.
With that change, once you access the array, you likely have all pointers to shift in your level 1 cache.
Because of that, I expect it to be plenty fast enough.
(Hm, are there CPUs that have instructions for shifting parts of cache lines around?)
Seems to me it could easily be a significant amount of overhead.
This makes this sort of scheme decidedly less attractive.
This is how many of the simpler malloc implementations, which do not directly mmap, work.
So, remember, if you want to malloc 20 ints, don’t do 20 times malloc(sizeof(int)), do malloc(20*sizeof(int)) and treat them as int[].
I am saddened to hear that he passed away in 2012 - I had no clue. He gave so much in the time he was active. It is bittersweet that his obituary[1] contains open problems he hoped others would solve.
[1]: https://docs.google.com/file/d/0B8ttd1KbGd3EWktsR29qNVdNVEE/...