[concurrency-interest] Optimizing unmodifiableXXX

Martin Buchholz Martin.Buchholz at Sun.COM
Mon Mar 5 17:29:56 EST 2007


Osvaldo wrote:

*Idea: the Collections.unmodifiableXxx() methods could embed the
intelligence of my utility: if the received collection is empty or a
singleton, unmodifiableXxx() could return an instance of the optimized
empty/singleton class, rather than always creating an instance of the
CollectionsUnmodifiableXxx class. This optimization only requires that
**unmodifiableXxx() implement an additional switch structure like the
code above. The result also avoids memory allocation for empty
collections. As far as I can see, there is no compatibility issue...
except for a strawman case like code that expects that for any c1 and
c2, unmodifiableXxx(c1) != **unmodifiableXxx(c2)** even if
c1.equals(c2), a requirement that's especially absurd for immutable
collections.*

I think this won't work, since the underlying map may be mutable.

Unmodifiability is not immutability.

Martin


More information about the Concurrency-interest mailing list