-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8371164: ArrayList.addAll() optimizations #28116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jengebr
wants to merge
6
commits into
openjdk:master
Choose a base branch
from
jengebr:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−3
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dbb5b6c
8371164 Optimizing ArrayList.addAll()
jengebr 626dabb
Adding direct unit tests, minor revisions to optimizations
jengebr 7725441
Simplifying ArrayList.addAll() fastpath
jengebr 6c333bd
Cleaning up tests per PR feedback
jengebr dd713ae
Whitespace fixes
jengebr 49b4347
Unit test revisions
jengebr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
test/micro/org/openjdk/bench/java/util/ArrayListBulkOpsBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
jengebr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Param({"0", "1", "5", "75"}) | ||
| int size; | ||
jengebr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Param({"ArrayList", "LinkedList"}) | ||
| String type; | ||
|
|
||
| List<String> source; | ||
jengebr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @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 { | ||
jengebr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing EOL here. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.