puzpuzpuz / xsync

Concurrent data structures for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Map: add way to provide initial size hints

costela opened this issue ยท comments

Since the difference in performance caused by map growth is noticeble, it would be great if we could initialize the map with size hints to try to avoid growths on add.

The fact that we spread keys across buckets makes this non-trivial, but maybe it would be enough to assume normally distributed keys and just grow the buckets proportionally to the initial hint?

I've just glossed over the code, but could try a PR if there's no obvious blocker I missed and nobody else beats me to it!

Yes, it would be a good addition to both Map and MapOf.

just grow the buckets proportionally to the initial hint

Yes, I'd simply use nextPowOf2(size / entriesPerMapBucket) as the initial table size. That's the usual way to initialize the hash table based on the hint.

damn, that was fast! I intended to work on this tonight! ๐Ÿ˜…

Thanks! ๐ŸŽ‰

Thanks for opening this issue. It was an overlooked enhancement.

I intended to work on this tonight!

If you see any issues or potential improvements around #86, please submit a PR. I won't be releasing the next version for a few days.

Thanks, will do! ๐Ÿ‘๐Ÿป