From 754f6e6116b8889c49abf34d01f6fc3e9f1b3cb7 Mon Sep 17 00:00:00 2001 From: Sergey Tsypanov Date: Wed, 18 Jan 2023 13:55:09 +0000 Subject: [PATCH] 8300237: Minor improvements in MethodHandles Reviewed-by: redestad --- .../java/lang/invoke/MethodHandles.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java index 4c901afefb1..7795ccadb6c 100644 --- a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java +++ b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java @@ -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. * * 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.Arrays; import java.util.BitSet; +import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Objects; @@ -6744,25 +6745,19 @@ assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum")); } private static List> longestParameterList(Stream mhs, int skipSize) { - final List> empty = List.of(); - final List> longest = mhs.filter(Objects::nonNull). + return mhs.filter(Objects::nonNull) // take only those that can contribute to a common suffix because they are longer than the prefix - map(MethodHandle::type). - filter(t -> t.parameterCount() > skipSize). - map(MethodType::parameterList). - reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty); - return longest.isEmpty() ? empty : longest.subList(skipSize, longest.size()); - } - - private static List> longestParameterList(List>> lists) { - final List> empty = List.of(); - return lists.stream().reduce((p, q) -> p.size() >= q.size() ? p : q).orElse(empty); + .map(MethodHandle::type) + .filter(t -> t.parameterCount() > skipSize) + .max(Comparator.comparingInt(MethodType::parameterCount)) + .map(methodType -> List.of(Arrays.copyOfRange(methodType.ptypes(), skipSize, methodType.parameterCount()))) + .orElse(List.of()); } private static List> buildCommonSuffix(List init, List step, List pred, List fini, int cpSize) { final List> longest1 = longestParameterList(Stream.of(step, pred, fini).flatMap(List::stream), cpSize); final List> longest2 = longestParameterList(init.stream(), 0); - return longestParameterList(List.of(longest1, longest2)); + return longest1.size() >= longest2.size() ? longest1 : longest2; } private static void loopChecks1b(List init, List> commonSuffix) {