test/jdk/java/util/Collection/IteratorMicroBenchmark.java
author pli
Tue, 16 Jul 2019 00:57:00 +0000
changeset 55689 8c5c9d86e1d6
parent 53781 940046624bf8
permissions -rw-r--r--
8227512: [TESTBUG] Fix JTReg javac test failures with Graal Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     1
/*
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     4
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     8
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    13
 * accompanied this code).
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    14
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    18
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    21
 * questions.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    22
 */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    23
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    24
/*
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    25
 * @test
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    26
 * @summary micro-benchmark correctness mode
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    27
 * @run main IteratorMicroBenchmark iterations=1 size=8 warmup=0
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    28
 */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    29
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
    30
import static java.util.concurrent.TimeUnit.MILLISECONDS;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    31
import static java.util.stream.Collectors.summingInt;
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
    32
import static java.util.stream.Collectors.toCollection;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    33
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
    34
import java.lang.ref.ReferenceQueue;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    35
import java.lang.ref.WeakReference;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    36
import java.util.ArrayDeque;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
    37
import java.util.Arrays;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    38
import java.util.ArrayList;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    39
import java.util.Collection;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    40
import java.util.Collections;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    41
import java.util.Deque;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    42
import java.util.HashMap;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    43
import java.util.Iterator;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    44
import java.util.LinkedList;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    45
import java.util.List;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    46
import java.util.ListIterator;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    47
import java.util.PriorityQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    48
import java.util.Spliterator;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    49
import java.util.Vector;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    50
import java.util.concurrent.ArrayBlockingQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    51
import java.util.concurrent.ConcurrentLinkedDeque;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    52
import java.util.concurrent.ConcurrentLinkedQueue;
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
    53
import java.util.concurrent.CopyOnWriteArrayList;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    54
import java.util.concurrent.LinkedBlockingDeque;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    55
import java.util.concurrent.LinkedBlockingQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    56
import java.util.concurrent.LinkedTransferQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    57
import java.util.concurrent.PriorityBlockingQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    58
import java.util.concurrent.CountDownLatch;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    59
import java.util.concurrent.ThreadLocalRandom;
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
    60
import java.util.concurrent.atomic.LongAdder;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    61
import java.util.function.UnaryOperator;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    62
import java.util.regex.Pattern;
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
    63
import java.util.stream.Stream;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    64
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    65
/**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    66
 * Usage: [iterations=N] [size=N] [filter=REGEXP] [warmup=SECONDS]
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    67
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    68
 * To run this in micro-benchmark mode, simply run as a normal java program.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    69
 * Be patient; this program runs for a very long time.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    70
 * For faster runs, restrict execution using command line args.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    71
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    72
 * @author Martin Buchholz
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    73
 */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    74
public class IteratorMicroBenchmark {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    75
    abstract static class Job {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    76
        private final String name;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    77
        public Job(String name) { this.name = name; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    78
        public String name() { return name; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    79
        public abstract void work() throws Throwable;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    80
        public void run() {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    81
            try { work(); }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    82
            catch (Throwable ex) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    83
                // current job cannot always be deduced from stacktrace.
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    84
                throw new RuntimeException("Job failed: " + name(), ex);
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    85
            }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
    86
        }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    87
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    88
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    89
    final int iterations;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    90
    final int size;             // number of elements in collections
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    91
    final double warmupSeconds;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    92
    final long warmupNanos;
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
    93
    final Pattern nameFilter;   // select subset of Jobs to run
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    94
    final boolean reverse;      // reverse order of Jobs
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    95
    final boolean shuffle;      // randomize order of Jobs
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    96
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    97
    IteratorMicroBenchmark(String[] args) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    98
        iterations    = intArg(args, "iterations", 10_000);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    99
        size          = intArg(args, "size", 1000);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   100
        warmupSeconds = doubleArg(args, "warmup", 7.0);
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   101
        nameFilter    = patternArg(args, "filter");
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   102
        reverse       = booleanArg(args, "reverse");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   103
        shuffle       = booleanArg(args, "shuffle");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   104
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   105
        warmupNanos = (long) (warmupSeconds * (1000L * 1000L * 1000L));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   106
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   107
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   108
    // --------------- GC finalization infrastructure ---------------
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   109
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   110
    /** No guarantees, but effective in practice. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   111
    static void forceFullGc() {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   112
        long timeoutMillis = 1000L;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   113
        CountDownLatch finalized = new CountDownLatch(1);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   114
        ReferenceQueue<Object> queue = new ReferenceQueue<>();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   115
        WeakReference<Object> ref = new WeakReference<>(
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   116
            new Object() {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   117
                @SuppressWarnings("deprecation")
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   118
                protected void finalize() { finalized.countDown(); }},
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   119
            queue);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   120
        try {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   121
            for (int tries = 3; tries--> 0; ) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   122
                System.gc();
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   123
                if (finalized.await(timeoutMillis, MILLISECONDS)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   124
                    && queue.remove(timeoutMillis) != null
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   125
                    && ref.get() == null) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   126
                    System.runFinalization(); // try to pick up stragglers
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   127
                    return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   128
                }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   129
                timeoutMillis *= 4;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   130
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   131
        } catch (InterruptedException unexpected) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   132
            throw new AssertionError("unexpected InterruptedException");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   133
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   134
        throw new AssertionError("failed to do a \"full\" gc");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   135
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   136
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   137
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   138
     * Runs each job for long enough that all the runtime compilers
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   139
     * have had plenty of time to warm up, i.e. get around to
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   140
     * compiling everything worth compiling.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   141
     * Returns array of average times per job per run.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   142
     */
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   143
    long[] time0(List<Job> jobs) {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   144
        final int size = jobs.size();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   145
        long[] nanoss = new long[size];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   146
        for (int i = 0; i < size; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   147
            if (warmupNanos > 0) forceFullGc();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   148
            Job job = jobs.get(i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   149
            long totalTime;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   150
            int runs = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   151
            long startTime = System.nanoTime();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   152
            do { job.run(); runs++; }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   153
            while ((totalTime = System.nanoTime() - startTime) < warmupNanos);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   154
            nanoss[i] = totalTime/runs;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   155
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   156
        return nanoss;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   157
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   158
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   159
    void time(List<Job> jobs) throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   160
        if (warmupNanos > 0) time0(jobs); // Warm up run
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   161
        final int size = jobs.size();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   162
        final long[] nanoss = time0(jobs); // Real timing run
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   163
        final long[] milliss = new long[size];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   164
        final double[] ratios = new double[size];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   165
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   166
        final String nameHeader   = "Method";
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   167
        final String millisHeader = "Millis";
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   168
        final String ratioHeader  = "Ratio";
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   169
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   170
        int nameWidth   = nameHeader.length();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   171
        int millisWidth = millisHeader.length();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   172
        int ratioWidth  = ratioHeader.length();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   173
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   174
        for (int i = 0; i < size; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   175
            nameWidth = Math.max(nameWidth, jobs.get(i).name().length());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   176
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   177
            milliss[i] = nanoss[i]/(1000L * 1000L);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   178
            millisWidth = Math.max(millisWidth,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   179
                                   String.format("%d", milliss[i]).length());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   180
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   181
            ratios[i] = (double) nanoss[i] / (double) nanoss[0];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   182
            ratioWidth = Math.max(ratioWidth,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   183
                                  String.format("%.3f", ratios[i]).length());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   184
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   185
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   186
        String format = String.format("%%-%ds %%%dd %%%d.3f%%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   187
                                      nameWidth, millisWidth, ratioWidth);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   188
        String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   189
                                            nameWidth, millisWidth, ratioWidth);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   190
        System.out.printf(headerFormat, "Method", "Millis", "Ratio");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   191
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   192
        // Print out absolute and relative times, calibrated against first job
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   193
        for (int i = 0; i < size; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   194
            System.out.printf(format, jobs.get(i).name(), milliss[i], ratios[i]);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   195
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   196
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   197
    private static String keywordValue(String[] args, String keyword) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   198
        for (String arg : args)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   199
            if (arg.startsWith(keyword))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   200
                return arg.substring(keyword.length() + 1);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   201
        return null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   202
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   203
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   204
    private static int intArg(String[] args, String keyword, int defaultValue) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   205
        String val = keywordValue(args, keyword);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   206
        return (val == null) ? defaultValue : Integer.parseInt(val);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   207
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   208
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   209
    private static double doubleArg(String[] args, String keyword, double defaultValue) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   210
        String val = keywordValue(args, keyword);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   211
        return (val == null) ? defaultValue : Double.parseDouble(val);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   212
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   213
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   214
    private static Pattern patternArg(String[] args, String keyword) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   215
        String val = keywordValue(args, keyword);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   216
        return (val == null) ? null : Pattern.compile(val);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   217
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   218
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   219
    private static boolean booleanArg(String[] args, String keyword) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   220
        String val = keywordValue(args, keyword);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   221
        if (val == null || val.equals("false")) return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   222
        if (val.equals("true")) return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   223
        throw new IllegalArgumentException(val);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   224
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   225
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   226
    private static void deoptimize(int sum) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   227
        if (sum == 42)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   228
            System.out.println("the answer");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   229
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   230
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   231
    private static <T> Iterable<T> backwards(final List<T> list) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   232
        return new Iterable<T>() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   233
            public Iterator<T> iterator() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   234
                return new Iterator<T>() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   235
                    final ListIterator<T> it = list.listIterator(list.size());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   236
                    public boolean hasNext() { return it.hasPrevious(); }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   237
                    public T next()          { return it.previous(); }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   238
                    public void remove()     {        it.remove(); }};}};
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   239
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   240
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   241
    // Checks for correctness *and* prevents loop optimizations
47730
c7b5b1ce8145 8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents: 47307
diff changeset
   242
    static class Check {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   243
        private int sum;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   244
        public void sum(int sum) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   245
            if (this.sum == 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   246
                this.sum = sum;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   247
            if (this.sum != sum)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   248
                throw new AssertionError("Sum mismatch");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   249
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   250
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   251
    volatile Check check = new Check();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   252
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   253
    public static void main(String[] args) throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   254
        new IteratorMicroBenchmark(args).run();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   255
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   256
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   257
    HashMap<Class<?>, String> goodClassName = new HashMap<>();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   258
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   259
    String goodClassName(Class<?> klazz) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   260
        return goodClassName.computeIfAbsent(
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   261
            klazz,
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   262
            k -> {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   263
                String simple = k.getSimpleName();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   264
                return (simple.equals("SubList")) // too simple!
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   265
                    ? k.getName().replaceFirst(".*\\.", "")
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   266
                    : simple;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   267
            });
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   268
    }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   269
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   270
    String goodClassName(Object x) {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   271
        return goodClassName(x.getClass());
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   272
    }
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   273
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   274
    static List<Integer> makeSubList(
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   275
        List<Integer> elements,
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   276
        UnaryOperator<List<Integer>> copyConstructor) {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   277
        final ArrayList<Integer> padded = new ArrayList<>();
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   278
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   279
        final int frontPorch = rnd.nextInt(3);
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   280
        final int backPorch = rnd.nextInt(3);
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   281
        for (int n = frontPorch; n--> 0; ) padded.add(rnd.nextInt());
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   282
        padded.addAll(elements);
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   283
        for (int n = backPorch; n--> 0; ) padded.add(rnd.nextInt());
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   284
        return copyConstructor.apply(padded)
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   285
            .subList(frontPorch, frontPorch + elements.size());
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   286
    }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   287
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   288
    void run() throws Throwable {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   289
        final ArrayList<Integer> al = new ArrayList<>(size);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   290
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   291
        // Populate collections with random data
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   292
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   293
        for (int i = 0; i < size; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   294
            al.add(rnd.nextInt(size));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   295
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   296
        final ArrayDeque<Integer> ad = new ArrayDeque<>(al);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   297
        final ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(al.size());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   298
        abq.addAll(al);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   299
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   300
        // shuffle circular array elements so they wrap
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   301
        for (int i = 0, n = rnd.nextInt(size); i < n; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   302
            ad.addLast(ad.removeFirst());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   303
            abq.add(abq.remove());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   304
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   305
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   306
        final Integer[] array = al.toArray(new Integer[0]);
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   307
        final List<Integer> immutableSubList
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   308
            = makeSubList(al, x -> List.of(x.toArray(new Integer[0])));
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   309
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   310
        Stream<Collection<Integer>> collections = concatStreams(
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   311
            Stream.of(
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   312
                // Lists and their subLists
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   313
                al,
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   314
                makeSubList(al, ArrayList::new),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   315
                new Vector<>(al),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   316
                makeSubList(al, Vector::new),
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   317
                new LinkedList<>(al),
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   318
                makeSubList(al, LinkedList::new),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   319
                new CopyOnWriteArrayList<>(al),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   320
                makeSubList(al, CopyOnWriteArrayList::new),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   321
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   322
                ad,
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   323
                new PriorityQueue<>(al),
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   324
                new ConcurrentLinkedQueue<>(al),
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   325
                new ConcurrentLinkedDeque<>(al),
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   326
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   327
                // Blocking Queues
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   328
                abq,
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   329
                new LinkedBlockingQueue<>(al),
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   330
                new LinkedBlockingDeque<>(al),
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   331
                new LinkedTransferQueue<>(al),
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   332
                new PriorityBlockingQueue<>(al),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   333
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   334
                List.of(al.toArray(new Integer[0]))),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   335
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   336
            // avoid UnsupportedOperationException in jdk9 and jdk10
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   337
            (goodClassName(immutableSubList).equals("RandomAccessSubList"))
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   338
            ? Stream.empty()
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   339
            : Stream.of(immutableSubList));
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   340
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   341
        ArrayList<Job> jobs = collections
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   342
            .flatMap(x -> jobs(x))
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   343
            .filter(job ->
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   344
                nameFilter == null || nameFilter.matcher(job.name()).find())
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   345
            .collect(toCollection(ArrayList::new));
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   346
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   347
        if (reverse) Collections.reverse(jobs);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   348
        if (shuffle) Collections.shuffle(jobs);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   349
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   350
        time(jobs);
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   351
    }
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   352
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   353
    @SafeVarargs @SuppressWarnings("varargs")
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 50229
diff changeset
   354
    private static <T> Stream<T> concatStreams(Stream<T> ... streams) {
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   355
        return Stream.of(streams).flatMap(s -> s);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   356
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   357
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   358
    boolean isMutable(Collection<Integer> x) {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   359
        return !(x.getClass().getName().contains("ImmutableCollections$"));
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   360
    }
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   361
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   362
    Stream<Job> jobs(Collection<Integer> x) {
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   363
        final String klazz = goodClassName(x);
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   364
        return concatStreams(
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   365
            collectionJobs(x),
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   366
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   367
            (isMutable(x))
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   368
            ? mutableCollectionJobs(x)
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   369
            : Stream.empty(),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   370
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   371
            (x instanceof Deque)
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   372
            ? dequeJobs((Deque<Integer>)x)
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   373
            : Stream.empty(),
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   374
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   375
            (x instanceof List)
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   376
            ? listJobs((List<Integer>)x)
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   377
            : Stream.empty(),
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   378
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   379
            (x instanceof List && isMutable(x))
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   380
            ? mutableListJobs((List<Integer>)x)
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   381
            : Stream.empty());
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   382
    }
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   383
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   384
    Object sneakyAdder(int[] sneakySum) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   385
        return new Object() {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   386
            public int hashCode() { throw new AssertionError(); }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   387
            public boolean equals(Object z) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   388
                sneakySum[0] += (int) z; return false; }};
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   389
    }
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   390
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   391
    Stream<Job> collectionJobs(Collection<Integer> x) {
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   392
        final String klazz = goodClassName(x);
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   393
        return Stream.of(
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   394
            new Job(klazz + " iterate for loop") {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   395
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   396
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   397
                        int sum = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   398
                        for (Integer n : x)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   399
                            sum += n;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   400
                        check.sum(sum);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   401
            new Job(klazz + " iterator().forEachRemaining()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   402
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   403
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   404
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   405
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   406
                        x.iterator().forEachRemaining(n -> sum[0] += n);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   407
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   408
            new Job(klazz + " spliterator().tryAdvance()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   409
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   410
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   411
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   412
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   413
                        Spliterator<Integer> spliterator = x.spliterator();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   414
                        do {} while (spliterator.tryAdvance(n -> sum[0] += n));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   415
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   416
            new Job(klazz + " spliterator().forEachRemaining()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   417
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   418
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   419
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   420
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   421
                        x.spliterator().forEachRemaining(n -> sum[0] += n);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   422
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   423
            new Job(klazz + " contains") {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   424
                public void work() throws Throwable {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   425
                    int[] sum = new int[1];
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   426
                    Object sneakyAdder = sneakyAdder(sum);
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   427
                    for (int i = 0; i < iterations; i++) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   428
                        sum[0] = 0;
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   429
                        if (x.contains(sneakyAdder)) throw new AssertionError();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   430
                        check.sum(sum[0]);}}},
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   431
            new Job(klazz + " containsAll") {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   432
                public void work() throws Throwable {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   433
                    int[] sum = new int[1];
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   434
                    Collection<Object> sneakyAdderCollection =
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   435
                        Collections.singleton(sneakyAdder(sum));
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   436
                    for (int i = 0; i < iterations; i++) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   437
                        sum[0] = 0;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   438
                        if (x.containsAll(sneakyAdderCollection))
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   439
                            throw new AssertionError();
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   440
                        check.sum(sum[0]);}}},
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   441
            new Job(klazz + " forEach") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   442
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   443
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   444
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   445
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   446
                        x.forEach(n -> sum[0] += n);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   447
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   448
            new Job(klazz + " toArray()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   449
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   450
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   451
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   452
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   453
                        for (Object o : x.toArray())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   454
                            sum[0] += (Integer) o;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   455
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   456
            new Job(klazz + " toArray(a)") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   457
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   458
                    Integer[] a = new Integer[x.size()];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   459
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   460
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   461
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   462
                        x.toArray(a);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   463
                        for (Object o : a)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   464
                            sum[0] += (Integer) o;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   465
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   466
            new Job(klazz + " toArray(empty)") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   467
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   468
                    Integer[] empty = new Integer[0];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   469
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   470
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   471
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   472
                        for (Integer o : x.toArray(empty))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   473
                            sum[0] += o;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   474
                        check.sum(sum[0]);}}},
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   475
            new Job(klazz + " stream().forEach") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   476
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   477
                    int[] sum = new int[1];
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   478
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   479
                        sum[0] = 0;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   480
                        x.stream().forEach(n -> sum[0] += n);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   481
                        check.sum(sum[0]);}}},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   482
            new Job(klazz + " stream().mapToInt") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   483
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   484
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   485
                        check.sum(x.stream().mapToInt(e -> e).sum());}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   486
            new Job(klazz + " stream().collect") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   487
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   488
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   489
                        check.sum(x.stream()
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   490
                                  .collect(summingInt(e -> e)));}}},
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   491
            new Job(klazz + " stream()::iterator") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   492
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   493
                    int[] sum = new int[1];
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   494
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   495
                        sum[0] = 0;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   496
                        for (Integer o : (Iterable<Integer>) x.stream()::iterator)
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   497
                            sum[0] += o;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   498
                        check.sum(sum[0]);}}},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   499
            new Job(klazz + " parallelStream().forEach") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   500
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   501
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   502
                        LongAdder sum = new LongAdder();
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   503
                        x.parallelStream().forEach(n -> sum.add(n));
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   504
                        check.sum((int) sum.sum());}}},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   505
            new Job(klazz + " parallelStream().mapToInt") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   506
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   507
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   508
                        check.sum(x.parallelStream().mapToInt(e -> e).sum());}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   509
            new Job(klazz + " parallelStream().collect") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   510
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   511
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   512
                        check.sum(x.parallelStream()
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   513
                                  .collect(summingInt(e -> e)));}}},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   514
            new Job(klazz + " parallelStream()::iterator") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   515
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   516
                    int[] sum = new int[1];
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   517
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   518
                        sum[0] = 0;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   519
                        for (Integer o : (Iterable<Integer>) x.parallelStream()::iterator)
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   520
                            sum[0] += o;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   521
                        check.sum(sum[0]);}}});
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   522
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   523
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   524
    Stream<Job> mutableCollectionJobs(Collection<Integer> x) {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   525
        final String klazz = goodClassName(x);
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   526
        return Stream.of(
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   527
            new Job(klazz + " removeIf") {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   528
                public void work() throws Throwable {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   529
                    int[] sum = new int[1];
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   530
                    for (int i = 0; i < iterations; i++) {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   531
                        sum[0] = 0;
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   532
                        if (x.removeIf(n -> { sum[0] += n; return false; }))
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   533
                            throw new AssertionError();
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   534
                        check.sum(sum[0]);}}},
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   535
            new Job(klazz + " remove(Object)") {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   536
                public void work() throws Throwable {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   537
                    int[] sum = new int[1];
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   538
                    Object sneakyAdder = sneakyAdder(sum);
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   539
                    for (int i = 0; i < iterations; i++) {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   540
                        sum[0] = 0;
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   541
                        if (x.remove(sneakyAdder)) throw new AssertionError();
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   542
                        check.sum(sum[0]);}}});
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   543
    }
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   544
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   545
    Stream<Job> dequeJobs(Deque<Integer> x) {
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   546
        final String klazz = goodClassName(x);
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   547
        return Stream.of(
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   548
            new Job(klazz + " descendingIterator() loop") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   549
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   550
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   551
                        int sum = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   552
                        Iterator<Integer> it = x.descendingIterator();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   553
                        while (it.hasNext())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   554
                            sum += it.next();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   555
                        check.sum(sum);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   556
            new Job(klazz + " descendingIterator().forEachRemaining()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   557
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   558
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   559
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   560
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   561
                        x.descendingIterator().forEachRemaining(n -> sum[0] += n);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   562
                        check.sum(sum[0]);}}});
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   563
    }
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   564
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   565
    Stream<Job> listJobs(List<Integer> x) {
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   566
        final String klazz = goodClassName(x);
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   567
        return Stream.of(
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   568
            new Job(klazz + " listIterator forward loop") {
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   569
                public void work() throws Throwable {
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   570
                    for (int i = 0; i < iterations; i++) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   571
                        int sum = 0;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   572
                        ListIterator<Integer> it = x.listIterator();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   573
                        while (it.hasNext())
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   574
                            sum += it.next();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   575
                        check.sum(sum);}}},
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   576
            new Job(klazz + " listIterator backward loop") {
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   577
                public void work() throws Throwable {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   578
                    for (int i = 0; i < iterations; i++) {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   579
                        int sum = 0;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   580
                        ListIterator<Integer> it = x.listIterator(x.size());
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   581
                        while (it.hasPrevious())
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   582
                            sum += it.previous();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   583
                        check.sum(sum);}}},
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   584
            new Job(klazz + " indexOf") {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   585
                public void work() throws Throwable {
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   586
                    int[] sum = new int[1];
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   587
                    Object sneakyAdder = sneakyAdder(sum);
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   588
                    for (int i = 0; i < iterations; i++) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   589
                        sum[0] = 0;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   590
                        if (x.indexOf(sneakyAdder) != -1)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   591
                            throw new AssertionError();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   592
                        check.sum(sum[0]);}}},
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   593
            new Job(klazz + " lastIndexOf") {
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   594
                public void work() throws Throwable {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   595
                    int[] sum = new int[1];
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   596
                    Object sneakyAdder = sneakyAdder(sum);
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   597
                    for (int i = 0; i < iterations; i++) {
49564
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   598
                        sum[0] = 0;
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   599
                        if (x.lastIndexOf(sneakyAdder) != -1)
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   600
                            throw new AssertionError();
260bf39376a4 8200258: Improve CopyOnWriteArrayList subList code
dl
parents: 48686
diff changeset
   601
                        check.sum(sum[0]);}}},
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   602
            new Job(klazz + " equals") {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   603
                public void work() throws Throwable {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   604
                    ArrayList<Integer> copy = new ArrayList<>(x);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   605
                    for (int i = 0; i < iterations; i++) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   606
                        if (!x.equals(copy))
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   607
                            throw new AssertionError();}}},
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   608
            new Job(klazz + " hashCode") {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   609
                public void work() throws Throwable {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   610
                    int hashCode = Arrays.hashCode(x.toArray());
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   611
                    for (int i = 0; i < iterations; i++) {
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   612
                        if (x.hashCode() != hashCode)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 49564
diff changeset
   613
                            throw new AssertionError();}}});
48686
e3dcdd73a549 8196207: Inefficient ArrayList.subList().toArray()
dl
parents: 48541
diff changeset
   614
    }
53781
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   615
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   616
    Stream<Job> mutableListJobs(List<Integer> x) {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   617
        final String klazz = goodClassName(x);
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   618
        return Stream.of(
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   619
            new Job(klazz + " replaceAll") {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   620
                public void work() throws Throwable {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   621
                    int[] sum = new int[1];
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   622
                    UnaryOperator<Integer> sneakyAdder =
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   623
                        x -> { sum[0] += x; return x; };
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   624
                    for (int i = 0; i < iterations; i++) {
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   625
                        sum[0] = 0;
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   626
                        x.replaceAll(sneakyAdder);
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   627
                        check.sum(sum[0]);}}});
940046624bf8 8215249: Miscellaneous changes imported from jsr166 CVS 2019-02
dl
parents: 50764
diff changeset
   628
    }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   629
}