dashmap
dashmap copied to clipboard
memoize the default shard amount
the default_shard_amount
calls std::thread::available_parallelism
which makes syscalls to check the resources available in the cgroup. Could this value be stored after the first call? It's unlikely to change often (there could be a mechanism to update it if needed?)
https://github.com/xacrimon/dashmap/blob/459db7ac6f3d0b46f507cb724dd9bb0ce389f4ae/src/lib.rs#L55
Made a simple improvement using once_cell
, which should do the job.
It's hard for me to think of a case where the value would change and caching the previous result was a meaningful problem. If it really does change, all the previous maps would have the wrong shard amount either way, so I'd rather not overcomplicate it.
In the benchmark, it showed that DashMap is heavily hit by the syscall overhead. https://github.com/jerry73204/which-hash-map-is-faster
By running 1000 HashMap initializations per 12 threads, it runs for 57.64 ms. A simple once_cell to cache the shared amount reduces the running time to 1.49ms. That's a great win.
@jerry73204 Could you update your benchmarks with results for v5.4.0?