eclipse-collections
eclipse-collections copied to clipboard
Expose compact/trimToSize in HashBag implementations
Hi,
HashBag
implementations seems to rely on a MutableObjectIntMap
internally, to optimize for memory it would be useful to be able to call compact on the bag so that the internal map is compacted.
My use case is that I have bugs that get filled with data over some time then at one point I convert them to unmodifiable and never modifiy them again but they get moved around and read by different parts of the application. Just before converting them to unmodifiable, it would be good to ensure they use as less space as possible because I have potentially thousands of such bags at every time.
Shouldn't the conversion to unmodifiable do the compacting?
@mohrezaei since unmodifiable returns a wrapper, the original object of still modifiable, so I'm not sure it's really relevant. Also I can see other cases where you just want to trim without creating an unmodifiable view on the collection.
Ideally toImmutableXXX could do something like this in the created immutable though! Well all the immutable connections constructor could do this actually, should I open another ticket for this?
Sorry, I mixed up the two for a second there. Yes, immutable should compact automatically. The unmodifiable wrapper (not conversion), shouldn't. I don't see a problem with adding the trim call to bag; just wanted to make sure it happened automatically when it should.
Yep, ok, totally agree.
For immutable it looks it should be implemented at the constructor level, so that however we create the immutable (factories, other collections method, etc) we get an object that uses just enough memory! I will create a ticket later then :)
Thanks!
Closing as this should be available in 12.0.0.M1