Skip to content

Commit 754f6e6

Browse files
Sergey Tsypanovcl4es
Sergey Tsypanov
authored andcommitted
8300237: Minor improvements in MethodHandles
Reviewed-by: redestad
1 parent 85d8bac commit 754f6e6

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -59,6 +59,7 @@
5959
import java.util.ArrayList;
6060
import java.util.Arrays;
6161
import java.util.BitSet;
62+
import java.util.Comparator;
6263
import java.util.Iterator;
6364
import java.util.List;
6465
import java.util.Objects;
@@ -6744,25 +6745,19 @@ private static void loopChecks1a(int i, MethodHandle in, MethodHandle st) {
67446745
}
67456746

67466747
private static List<Class<?>> longestParameterList(Stream<MethodHandle> mhs, int skipSize) {
6747-
final List<Class<?>> empty = List.of();
6748-
final List<Class<?>> longest = mhs.filter(Objects::nonNull).
6748+
return mhs.filter(Objects::nonNull)
67496749
// take only those that can contribute to a common suffix because they are longer than the prefix
6750-
map(MethodHandle::type).
6751-
filter(t -> t.parameterCount() > skipSize).
6752-
map(MethodType::parameterList).
6753-
reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty);
6754-
return longest.isEmpty() ? empty : longest.subList(skipSize, longest.size());
6755-
}
6756-
6757-
private static List<Class<?>> longestParameterList(List<List<Class<?>>> lists) {
6758-
final List<Class<?>> empty = List.of();
6759-
return lists.stream().reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty);
6750+
.map(MethodHandle::type)
6751+
.filter(t -> t.parameterCount() > skipSize)
6752+
.max(Comparator.comparingInt(MethodType::parameterCount))
6753+
.map(methodType -> List.of(Arrays.copyOfRange(methodType.ptypes(), skipSize, methodType.parameterCount())))
6754+
.orElse(List.of());
67606755
}
67616756

67626757
private static List<Class<?>> buildCommonSuffix(List<MethodHandle> init, List<MethodHandle> step, List<MethodHandle> pred, List<MethodHandle> fini, int cpSize) {
67636758
final List<Class<?>> longest1 = longestParameterList(Stream.of(step, pred, fini).flatMap(List::stream), cpSize);
67646759
final List<Class<?>> longest2 = longestParameterList(init.stream(), 0);
6765-
return longestParameterList(List.of(longest1, longest2));
6760+
return longest1.size() >= longest2.size() ? longest1 : longest2;
67666761
}
67676762

67686763
private static void loopChecks1b(List<MethodHandle> init, List<Class<?>> commonSuffix) {

0 commit comments

Comments
 (0)