diff --git a/src/java.base/share/classes/java/util/ArrayList.java b/src/java.base/share/classes/java/util/ArrayList.java index c00b130a553a2..53e818b99c51f 100644 --- a/src/java.base/share/classes/java/util/ArrayList.java +++ b/src/java.base/share/classes/java/util/ArrayList.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2025, 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 @@ -750,9 +750,17 @@ public void clear() { * @throws NullPointerException if the specified collection is null */ public boolean addAll(Collection c) { - Object[] a = c.toArray(); + Object[] a; + int numNew; + if (c.getClass() == ArrayList.class) { + ArrayList src = (ArrayList) c; + a = src.elementData; + numNew = src.size; + } else { + a = c.toArray(); + numNew = a.length; + } modCount++; - int numNew = a.length; if (numNew == 0) return false; Object[] elementData; diff --git a/src/java.base/share/classes/java/util/Collections.java b/src/java.base/share/classes/java/util/Collections.java index c48dbd8cf6c13..f3342e9877e98 100644 --- a/src/java.base/share/classes/java/util/Collections.java +++ b/src/java.base/share/classes/java/util/Collections.java @@ -5253,6 +5253,20 @@ public boolean removeIf(Predicate filter) { public int hashCode() { return Objects.hashCode(element); } + @Override + public Object[] toArray() { + return new Object[] {element}; + } + @Override + @SuppressWarnings("unchecked") + public T[] toArray(T[] a) { + if (a.length < 1) + a = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), 1); + a[0] = (T)element; + if (a.length > 1) + a[1] = null; + return a; + } } /** diff --git a/test/jdk/java/util/Collection/MOAT.java b/test/jdk/java/util/Collection/MOAT.java index ab9e4b4309d0e..4e1cb6e94814f 100644 --- a/test/jdk/java/util/Collection/MOAT.java +++ b/test/jdk/java/util/Collection/MOAT.java @@ -26,7 +26,7 @@ * @bug 6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464 * 4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753 * 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215 - * 4802647 7123424 8024709 8193128 8327858 8368178 + * 4802647 7123424 8024709 8193128 8327858 8368178 8371164 * @summary Run many tests on many Collection and Map implementations * @author Martin Buchholz * @modules java.base/java.util:open @@ -887,6 +887,28 @@ private static void testStringElement(Collection c) { catch (Throwable t) { unexpected(t); } } + private static void testAddAll(Collection c) { + clear(c); + + // Test ArrayList source + ArrayList arrayListSource = new ArrayList<>(); + arrayListSource.add(42); + arrayListSource.add(99); + check(c.addAll(arrayListSource)); + equal(c.size(), arrayListSource.size()); + check(c.containsAll(arrayListSource)); + + clear(c); + + // Test non-ArrayList source + LinkedList linkedListSource = new LinkedList<>(); + linkedListSource.add(77); + linkedListSource.add(88); + check(c.addAll(linkedListSource)); + equal(c.size(), linkedListSource.size()); + check(c.containsAll(linkedListSource)); + } + private static void testConcurrentCollection(Collection c) { try { c.add(1); @@ -1294,6 +1316,8 @@ private static void testCollection1(Collection c) { clear(c); testStringElement(c); oneElement(c); testStringElement(c); + testAddAll(c); + if (c.getClass().getName().matches(".*concurrent.*")) testConcurrentCollection(c); diff --git a/test/micro/org/openjdk/bench/java/util/ArrayListBulkOpsBenchmark.java b/test/micro/org/openjdk/bench/java/util/ArrayListBulkOpsBenchmark.java new file mode 100644 index 0000000000000..78b902f0c3add --- /dev/null +++ b/test/micro/org/openjdk/bench/java/util/ArrayListBulkOpsBenchmark.java @@ -0,0 +1,128 @@ +/* + * Copyright Amazon.com Inc. 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.openjdk.bench.java.util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import java.util.WeakHashMap; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + + +/** + * Benchmark measuring ArrayList addAll() performance. + * + * Tests the performance of ArrayList.addAll() when copying from another ArrayList. + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Fork(value = 1, jvmArgs = { "-XX:+UseParallelGC", "-Xmx3g" }) +public class ArrayListBulkOpsBenchmark { + @Param({"0", "1", "5", "75"}) + int size; + + @Param({"ArrayList", "LinkedList"}) + String type; + + List source; + + @Setup(Level.Trial) + public void setup() { + switch (type) { + case "ArrayList" -> source = new ArrayList<>(size); + case "LinkedList" -> source = new LinkedList<>(); + } + for (int i = 0; i < size; i++) source.add("key" + i); + } + + @Benchmark + public ArrayList addAll() { + ArrayList result = new ArrayList<>(size); + result.addAll(source); + return result; + } + + static void poisonCallSites() { + HashMap hashMapSource = new HashMap<>(); + TreeSet treeSetSource = new TreeSet<>(); + WeakHashMap weakHashMapSource = new WeakHashMap<>(); + for (int i = 0; i < 75; i++) { + hashMapSource.put("key" + i, "value" + i); + treeSetSource.add("key" + i); + weakHashMapSource.put("key" + i, "value" + i); + } + // Poison ArrayList.addAll() with different Collection types + for (int i = 0; i < 40_000; i++) { + ArrayList temp = new ArrayList<>(); + temp.addAll(hashMapSource.entrySet()); + temp.addAll(treeSetSource); + temp.addAll(weakHashMapSource.keySet()); + } + } + + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @State(Scope.Benchmark) + @Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) + @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) + @Fork(value = 1, jvmArgs = { "-XX:+UseParallelGC", "-Xmx3g" }) + public static class SingletonSet { + Set singletonSetSource = Collections.singleton("key"); + + @Param({ "false", "true" }) + private boolean poison; + + @Setup(Level.Trial) + public void setup() { + if (poison) poisonCallSites(); + } + + @Benchmark + public ArrayList addAllSingletonSet() { + ArrayList result = new ArrayList<>(1); + result.addAll(singletonSetSource); + return result; + } + } +}