RFR: 8282662: Use List/Set.of() factory methods to reduce memory consumption

liach duke at openjdk.java.net
Tue Mar 8 14:35:12 UTC 2022


On Mon, 7 Mar 2022 15:11:50 GMT, Сергей Цыпанов <duke at openjdk.java.net> wrote:

> `List.of()` along with `Set.of()` create unmodifiable `List/Set` but with smaller footprint comparing to `Arrays.asList()` / `new HashSet()` when called with vararg of size 0, 1, 2.
> 
> In general replacement of `Arrays.asList()` with `List.of()` is dubious as the latter is null-hostile, however in some cases we are sure that arguments are non-null. Into this PR I've included the following cases (in addition to those where the argument is proved to be non-null at compile-time):
> - `MethodHandles.longestParameterList()` never returns null
> - parameter types are never null
> - interfaces used for proxy construction and returned from `Class.getInterfaces()` are never null
> - exceptions types of method signature are never null

Calling `Arrays.asList` vs `List.of` on a switch on the array length seems like an overkill for such a simple thing. Imo the safest changes are where we know the input array length is going to be 1 or 2.

src/java.base/share/classes/java/nio/file/FileTreeIterator.java line 70:

> 68:         throws IOException
> 69:     {
> 70:         this.walker = new FileTreeWalker(List.of(options), maxDepth);

Relates to https://bugs.openjdk.java.net/browse/JDK-8145048

src/java.base/share/classes/sun/reflect/annotation/AnnotationSupport.java line 79:

> 77:                                     containerBeforeContainee(annotations, annoClass);
> 78: 
> 79:             result.addAll((indirectFirst ? 0 : 1), List.of(indirect));

This `indirect` is most likely to be of size > 2

-------------

PR: https://git.openjdk.java.net/jdk/pull/7729



More information about the security-dev mailing list