RFR: 8300237: Minor improvements in MethodHandles
Rémi Forax
forax at openjdk.org
Tue Jan 17 10:14:14 UTC 2023
On Tue, 17 Jan 2023 09:51:31 GMT, Claes Redestad <redestad at openjdk.org> wrote:
>> - `MethodType.ptypes()` can be used instead of `MethodType.parameterList()` when we don't need a copy
>> - comparison of two lists can be done without `Stream.reduce()`
>
> src/java.base/share/classes/java/lang/invoke/MethodHandles.java line 6754:
>
>> 6752: filter(t -> t.parameterCount() > skipSize).
>> 6753: map(MethodType::ptypes).
>> 6754: reduce((p, q) -> p.length >= q.length ? p : q).orElse(EMPTY);
>
> Could avoid the need to introduce `EMPTY` if you make the stream expression return the longest list directly:
>
> reduce((p, q) -> {
> var longest = (p.size() >= q.size()) ? p : q;
> return List.of(Arrays.copyOfRange(longest, skipSize, longest.size()); // checking isEmpty() is redundant here since we filter on `t.parameterCount() > skipSize`
> }).orElse(List.of());
Using lambdas inside MethodHandles is quite dangerous given that lambdas are initialized using method handles. It may work now because longuestParameterList() is not called when initializing a lambda but it may make any changes in the implementation of lambdas painfull in the future.
-------------
PR: https://git.openjdk.org/jdk/pull/12025
More information about the core-libs-dev
mailing list