eclipse-collections icon indicating copy to clipboard operation
eclipse-collections copied to clipboard

Expose compact/trimToSize in HashBag implementations

Open victornoel opened this issue 2 years ago • 4 comments

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.

victornoel avatar Sep 22 '22 13:09 victornoel

Shouldn't the conversion to unmodifiable do the compacting?

mohrezaei avatar Sep 22 '22 16:09 mohrezaei

@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?

victornoel avatar Sep 22 '22 16:09 victornoel

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.

mohrezaei avatar Sep 22 '22 16:09 mohrezaei

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!

victornoel avatar Sep 22 '22 16:09 victornoel

Closing as this should be available in 12.0.0.M1

victornoel avatar Feb 08 '23 17:02 victornoel