dashmap icon indicating copy to clipboard operation
dashmap copied to clipboard

memoize the default shard amount

Open Geal opened this issue 2 years ago • 2 comments

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

Geal avatar Jul 06 '22 14:07 Geal

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.

tomkarw avatar Jul 20 '22 20:07 tomkarw

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 avatar Jul 23 '22 01:07 jerry73204

@jerry73204 Could you update your benchmarks with results for v5.4.0?

tomkarw avatar Aug 30 '22 18:08 tomkarw