Why doesn't Guava use specialized classes for small ImmutableLists?

Guava ImmutableList has a number of overloaded methods of() . As discussed in the context of this resolved issue , they exist to avoid the warnings that occur when mixing varargs with generics.

But in addition to this, the methods of parameters 0 and 1 rely on the implementation of a specialized list. It would seem that the same could be done for parameter methods 2.11, thereby reducing memory consumption by these lists - along the lines

 final class ImmutableListWith2Elements<E> extends ImmutableList<E> { final E e1; final E e2; ... 

Instead, they use an array-based implementation, which means storing an array object and an array reference in addition to content references. Can you help me understand the tradeoffs involved?

+4
source share
1 answer

Can you help me understand the trade-offs here?

This is a compromise between:

  • Performance - there is a preservation from the non-allocation of a temporary array. However, to quantify cost savings, extensive code analysis and benchmarking will be required. (I suspect that in most applications this would be negligible. And read this link provided by @Voo!)
  • Readability - Having a bunch of extra overload clutter javadocs.
  • Maintaining health - the presence of many overloads, which are implemented in such a way that a temporary object is not required, will entail many copy / paste programs, which will simplify work with future code.
  • Utility - how often will these overloads be used? I expect the answer to be "rare."
  • Bytecode footprint - these additional overloads will contribute to application swelling for each application using the Guava JAR file.

My advice:

  • Don't be fooled by Guava developers about this. They have already decided on compromises. You will just waste your breath.
  • If the absence of these classes or methods is harmful to your application, roll your own add-ons. (But try to make it so that you don’t include the private guava plug ... because you are likely to regret it in the long run.)

For the record, I believe that the Guava developers got this right.

+5
source

Source: https://habr.com/ru/post/1412205/


All Articles