RFR: 8273656: Improve java.lang.invoke.MethodType.parameterList() and its usage

Jorn Vernee jvernee at openjdk.java.net
Wed Sep 22 11:52:54 UTC 2021

On Mon, 13 Sep 2021 11:06:15 GMT, Сергей Цыпанов <github.com+10835776+stsypanov at openjdk.org> wrote:

> Currently the method is implemented like
> public List<Class<?>> parameterList() {
>   return Collections.unmodifiableList(Arrays.asList(ptypes.clone()));
> }
> This seems to be excessive, as three objects are allocated here. Instead we can use `List.of(ptypes)` which doesn't allocate anything for empty array and for one of length 1 and 2 it allocates lightweight objects with 2 fields, still copying longer arrays. This is likely to be fruitful as most of methods have 0-2 parameters.
> Also there is a couple of cases when `MethodType.parameterLis()` is called to get its size, which is excessive either as we can use `MethodType.parameterCount()` instead.

> Mailing list message from John Rose on core-libs-dev:
> On Sep 13, 2021, at 10:24 AM, Vladimir Ivanov <vlivanov at openjdk.java.net<mailto:vlivanov at openjdk.java.net>> wrote:
> BTW it can be improved even further by caching the immutable List view of parameters.
> I would go further: If I were writing MethodType.java today
> I would probably use List.of as the backing store for the
> parameters, instead of the private array. So ptypes should
> be List<Class<?>> not Class<?>[]. I don?t think the footprint
> or polymorphism effects would be dealbreakers, and the
> code would (I think) be simpler overall. But that?s a messy
> change, since the array representation is dug in.

Another thing that makes this change hard is that `MethodType` is mapped directly in HotSpot, so that HotSpot can retrieve for instance the parameter types [1]. The VM already knows how to dig into arrays, but adding support for `List` as well seems more cumbersome, since the list internals can change, and AFAIK most VM code can not just call `List::get`.

[1] : https://github.com/openjdk/jdk/blob/master/src/hotspot/share/classfile/javaClasses.cpp#L4135-L4137


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

More information about the core-libs-dev mailing list