vigna / fastutil

fastutil extends the Java™ Collections Framework by providing type-specific maps, sets, lists and queues.
Apache License 2.0
1.76k stars 196 forks source link

Expose OpenHashMap#ensureCapacity #273

Closed TheMode closed 1 year ago

TheMode commented 2 years ago

Useful to avoid unnecessary resizing on batch insertion

vigna commented 2 years ago

Presently you can have a subclass that exposes a method using rehash(), exactly like ensureCapacity() does. putAll() will also try to increase capacity, so you should that for batch insertion.

I don't see particular reasons not to make the method public. Does anybody have a different opinion? @incaseoftrouble @techsy730 ?

TheMode commented 2 years ago

Presently you can have a subclass that exposes a method using rehash(), exactly like ensureCapacity() does. putAll() will also try to increase capacity, so you should that for batch insertion.

These could work, but far from being convenient in my case: https://github.com/Minestom/Minestom/blob/708307dbdec3671592fdfd3c06b4cd33a1b41cb8/src/main/java/net/minestom/server/ServerProcessImpl.java#L273-L275 as I would also need to make my own lazy Map, which may be counterproductive and add more overhead.

Having the method exposed would definitely be welcome.

mmadoo commented 1 year ago

I also need such behaviour. I create the map in one class and in another class, I got collection future size before doing all puts. So I cannot init the class with the right size. Ability to set call ensureCapacity would have help me in this case.

vigna commented 1 year ago

Done in 85bb33b1bfde34dac21005037951b615268f0083. Let me know if it works for you...