mirror of
https://github.com/openjdk/jdk.git
synced 2026-01-28 03:58:21 +00:00
8371164: ArrayList.addAll() optimizations
Reviewed-by: smarks, ogillespie
This commit is contained in:
parent
256a9beffc
commit
aeea849756
@ -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 class ArrayList<E> extends AbstractList<E>
|
||||
* @throws NullPointerException if the specified collection is null
|
||||
*/
|
||||
public boolean addAll(Collection<? extends E> 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;
|
||||
|
||||
@ -5253,6 +5253,20 @@ public final class Collections {
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(element);
|
||||
}
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return new Object[] {element};
|
||||
}
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T[] toArray(T[] a) {
|
||||
if (a.length < 1)
|
||||
a = (T[])Array.newInstance(a.getClass().getComponentType(), 1);
|
||||
a[0] = (T)element;
|
||||
if (a.length > 1)
|
||||
a[1] = null;
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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 @@ public class MOAT {
|
||||
catch (Throwable t) { unexpected(t); }
|
||||
}
|
||||
|
||||
private static void testAddAll(Collection<Integer> c) {
|
||||
clear(c);
|
||||
|
||||
// Test ArrayList source
|
||||
ArrayList<Integer> 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<Integer> 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<Integer> c) {
|
||||
try {
|
||||
c.add(1);
|
||||
@ -1294,6 +1316,8 @@ public class MOAT {
|
||||
clear(c); testStringElement(c);
|
||||
oneElement(c); testStringElement(c);
|
||||
|
||||
testAddAll(c);
|
||||
|
||||
if (c.getClass().getName().matches(".*concurrent.*"))
|
||||
testConcurrentCollection(c);
|
||||
|
||||
|
||||
@ -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<String> 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<String> addAll() {
|
||||
ArrayList<String> result = new ArrayList<>(size);
|
||||
result.addAll(source);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void poisonCallSites() {
|
||||
HashMap<String, String> hashMapSource = new HashMap<>();
|
||||
TreeSet<String> treeSetSource = new TreeSet<>();
|
||||
WeakHashMap<String, String> 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<Object> 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<String> singletonSetSource = Collections.singleton("key");
|
||||
|
||||
@Param({ "false", "true" })
|
||||
private boolean poison;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setup() {
|
||||
if (poison) poisonCallSites();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public ArrayList<String> addAllSingletonSet() {
|
||||
ArrayList<String> result = new ArrayList<>(1);
|
||||
result.addAll(singletonSetSource);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user