8300237: Minor improvements in MethodHandles

Reviewed-by: redestad
This commit is contained in:
Sergey Tsypanov 2023-01-18 13:55:09 +00:00 committed by Claes Redestad
parent 85d8bacb0f
commit 754f6e6116

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -59,6 +59,7 @@ import java.security.ProtectionDomain;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.BitSet; import java.util.BitSet;
import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -6744,25 +6745,19 @@ assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
} }
private static List<Class<?>> longestParameterList(Stream<MethodHandle> mhs, int skipSize) { private static List<Class<?>> longestParameterList(Stream<MethodHandle> mhs, int skipSize) {
final List<Class<?>> empty = List.of(); return mhs.filter(Objects::nonNull)
final List<Class<?>> longest = mhs.filter(Objects::nonNull).
// take only those that can contribute to a common suffix because they are longer than the prefix // take only those that can contribute to a common suffix because they are longer than the prefix
map(MethodHandle::type). .map(MethodHandle::type)
filter(t -> t.parameterCount() > skipSize). .filter(t -> t.parameterCount() > skipSize)
map(MethodType::parameterList). .max(Comparator.comparingInt(MethodType::parameterCount))
reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty); .map(methodType -> List.of(Arrays.copyOfRange(methodType.ptypes(), skipSize, methodType.parameterCount())))
return longest.isEmpty() ? empty : longest.subList(skipSize, longest.size()); .orElse(List.of());
}
private static List<Class<?>> longestParameterList(List<List<Class<?>>> lists) {
final List<Class<?>> empty = List.of();
return lists.stream().reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty);
} }
private static List<Class<?>> buildCommonSuffix(List<MethodHandle> init, List<MethodHandle> step, List<MethodHandle> pred, List<MethodHandle> fini, int cpSize) { private static List<Class<?>> buildCommonSuffix(List<MethodHandle> init, List<MethodHandle> step, List<MethodHandle> pred, List<MethodHandle> fini, int cpSize) {
final List<Class<?>> longest1 = longestParameterList(Stream.of(step, pred, fini).flatMap(List::stream), cpSize); final List<Class<?>> longest1 = longestParameterList(Stream.of(step, pred, fini).flatMap(List::stream), cpSize);
final List<Class<?>> longest2 = longestParameterList(init.stream(), 0); final List<Class<?>> longest2 = longestParameterList(init.stream(), 0);
return longestParameterList(List.of(longest1, longest2)); return longest1.size() >= longest2.size() ? longest1 : longest2;
} }
private static void loopChecks1b(List<MethodHandle> init, List<Class<?>> commonSuffix) { private static void loopChecks1b(List<MethodHandle> init, List<Class<?>> commonSuffix) {