test/jdk/java/util/Collection/IteratorMicroBenchmark.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45937 jdk/test/java/util/Collection/IteratorMicroBenchmark.java@646816090183
child 47307 6864969a78ad
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    30
import static java.util.stream.Collectors.summingInt;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    31
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    32
import java.lang.ref.WeakReference;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    33
import java.util.ArrayDeque;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    34
import java.util.Arrays;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    35
import java.util.ArrayList;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    36
import java.util.Collection;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    37
import java.util.Collections;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    38
import java.util.Deque;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    39
import java.util.Enumeration;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    40
import java.util.Iterator;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    41
import java.util.LinkedList;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    42
import java.util.List;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    43
import java.util.ListIterator;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    44
import java.util.Map;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    45
import java.util.PriorityQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    46
import java.util.Spliterator;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    47
import java.util.Vector;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    48
import java.util.concurrent.ArrayBlockingQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    49
import java.util.concurrent.ConcurrentLinkedDeque;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    50
import java.util.concurrent.ConcurrentLinkedQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    51
import java.util.concurrent.LinkedBlockingDeque;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    52
import java.util.concurrent.LinkedBlockingQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    53
import java.util.concurrent.LinkedTransferQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    54
import java.util.concurrent.PriorityBlockingQueue;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    55
import java.util.concurrent.ConcurrentSkipListMap;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    56
import java.util.concurrent.CountDownLatch;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    57
import java.util.concurrent.ThreadLocalRandom;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    58
import java.util.concurrent.TimeUnit;
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
    59
import java.util.concurrent.atomic.LongAdder;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    60
import java.util.regex.Pattern;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    61
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    62
/**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    63
 * Usage: [iterations=N] [size=N] [filter=REGEXP] [warmup=SECONDS]
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    64
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    65
 * To run this in micro-benchmark mode, simply run as a normal java program.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    66
 * Be patient; this program runs for a very long time.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    67
 * For faster runs, restrict execution using command line args.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    68
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    69
 * This is an interface based version of ArrayList/IteratorMicroBenchmark
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    70
 *
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    71
 * @author Martin Buchholz
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    72
 */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    73
public class IteratorMicroBenchmark {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    74
    abstract static class Job {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    75
        private final String name;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    76
        public Job(String name) { this.name = name; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    77
        public String name() { return name; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    78
        public abstract void work() throws Throwable;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    79
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    80
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    81
    final int iterations;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    82
    final int size;             // number of elements in collections
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    83
    final double warmupSeconds;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    84
    final long warmupNanos;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    85
    final Pattern filter;       // select subset of Jobs to run
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    86
    final boolean reverse;      // reverse order of Jobs
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    87
    final boolean shuffle;      // randomize order of Jobs
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    88
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    89
    IteratorMicroBenchmark(String[] args) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    90
        iterations    = intArg(args, "iterations", 10_000);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    91
        size          = intArg(args, "size", 1000);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    92
        warmupSeconds = doubleArg(args, "warmup", 7.0);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    93
        filter        = patternArg(args, "filter");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    94
        reverse       = booleanArg(args, "reverse");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    95
        shuffle       = booleanArg(args, "shuffle");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    96
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    97
        warmupNanos = (long) (warmupSeconds * (1000L * 1000L * 1000L));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    98
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
    99
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   100
    // --------------- GC finalization infrastructure ---------------
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   101
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   102
    /** No guarantees, but effective in practice. */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   103
    static void forceFullGc() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   104
        CountDownLatch finalizeDone = new CountDownLatch(1);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   105
        WeakReference<?> ref = new WeakReference<Object>(new Object() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   106
            protected void finalize() { finalizeDone.countDown(); }});
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   107
        try {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   108
            for (int i = 0; i < 10; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   109
                System.gc();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   110
                if (finalizeDone.await(1L, TimeUnit.SECONDS) && ref.get() == null) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   111
                    System.runFinalization(); // try to pick up stragglers
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   112
                    return;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   113
                }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   114
            }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   115
        } catch (InterruptedException unexpected) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   116
            throw new AssertionError("unexpected InterruptedException");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   117
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   118
        throw new AssertionError("failed to do a \"full\" gc");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   119
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   120
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   121
    /**
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   122
     * Runs each job for long enough that all the runtime compilers
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   123
     * have had plenty of time to warm up, i.e. get around to
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   124
     * compiling everything worth compiling.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   125
     * Returns array of average times per job per run.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   126
     */
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   127
    long[] time0(List<Job> jobs) throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   128
        final int size = jobs.size();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   129
        long[] nanoss = new long[size];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   130
        for (int i = 0; i < size; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   131
            if (warmupNanos > 0) forceFullGc();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   132
            Job job = jobs.get(i);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   133
            long totalTime;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   134
            int runs = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   135
            long startTime = System.nanoTime();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   136
            do { job.work(); runs++; }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   137
            while ((totalTime = System.nanoTime() - startTime) < warmupNanos);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   138
            nanoss[i] = totalTime/runs;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   139
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   140
        return nanoss;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   141
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   142
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   143
    void time(List<Job> jobs) throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   144
        if (warmupNanos > 0) time0(jobs); // Warm up run
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   145
        final int size = jobs.size();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   146
        final long[] nanoss = time0(jobs); // Real timing run
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   147
        final long[] milliss = new long[size];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   148
        final double[] ratios = new double[size];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   149
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   150
        final String nameHeader   = "Method";
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   151
        final String millisHeader = "Millis";
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   152
        final String ratioHeader  = "Ratio";
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   153
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   154
        int nameWidth   = nameHeader.length();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   155
        int millisWidth = millisHeader.length();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   156
        int ratioWidth  = ratioHeader.length();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   157
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   158
        for (int i = 0; i < size; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   159
            nameWidth = Math.max(nameWidth, jobs.get(i).name().length());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   160
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   161
            milliss[i] = nanoss[i]/(1000L * 1000L);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   162
            millisWidth = Math.max(millisWidth,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   163
                                   String.format("%d", milliss[i]).length());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   164
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   165
            ratios[i] = (double) nanoss[i] / (double) nanoss[0];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   166
            ratioWidth = Math.max(ratioWidth,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   167
                                  String.format("%.3f", ratios[i]).length());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   168
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   169
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   170
        String format = String.format("%%-%ds %%%dd %%%d.3f%%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   171
                                      nameWidth, millisWidth, ratioWidth);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   172
        String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   173
                                            nameWidth, millisWidth, ratioWidth);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   174
        System.out.printf(headerFormat, "Method", "Millis", "Ratio");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   175
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   176
        // Print out absolute and relative times, calibrated against first job
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   177
        for (int i = 0; i < size; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   178
            System.out.printf(format, jobs.get(i).name(), milliss[i], ratios[i]);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   179
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   180
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   181
    private static String keywordValue(String[] args, String keyword) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   182
        for (String arg : args)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   183
            if (arg.startsWith(keyword))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   184
                return arg.substring(keyword.length() + 1);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   185
        return null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   186
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   187
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   188
    private static int intArg(String[] args, String keyword, int defaultValue) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   189
        String val = keywordValue(args, keyword);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   190
        return (val == null) ? defaultValue : Integer.parseInt(val);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   191
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   192
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   193
    private static double doubleArg(String[] args, String keyword, double defaultValue) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   194
        String val = keywordValue(args, keyword);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   195
        return (val == null) ? defaultValue : Double.parseDouble(val);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   196
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   197
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   198
    private static Pattern patternArg(String[] args, String keyword) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   199
        String val = keywordValue(args, keyword);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   200
        return (val == null) ? null : Pattern.compile(val);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   201
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   202
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   203
    private static boolean booleanArg(String[] args, String keyword) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   204
        String val = keywordValue(args, keyword);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   205
        if (val == null || val.equals("false")) return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   206
        if (val.equals("true")) return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   207
        throw new IllegalArgumentException(val);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   208
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   209
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   210
    private static List<Job> filter(Pattern filter, List<Job> jobs) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   211
        if (filter == null) return jobs;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   212
        ArrayList<Job> newJobs = new ArrayList<>();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   213
        for (Job job : jobs)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   214
            if (filter.matcher(job.name()).find())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   215
                newJobs.add(job);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   216
        return newJobs;
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 void deoptimize(int sum) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   220
        if (sum == 42)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   221
            System.out.println("the answer");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   222
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   223
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   224
    private static <T> List<T> asSubList(List<T> list) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   225
        return list.subList(0, list.size());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   226
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   227
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   228
    private static <T> Iterable<T> backwards(final List<T> list) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   229
        return new Iterable<T>() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   230
            public Iterator<T> iterator() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   231
                return new Iterator<T>() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   232
                    final ListIterator<T> it = list.listIterator(list.size());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   233
                    public boolean hasNext() { return it.hasPrevious(); }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   234
                    public T next()          { return it.previous(); }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   235
                    public void remove()     {        it.remove(); }};}};
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   236
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   237
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   238
    // Checks for correctness *and* prevents loop optimizations
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   239
    class Check {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   240
        private int sum;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   241
        public void sum(int sum) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   242
            if (this.sum == 0)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   243
                this.sum = sum;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   244
            if (this.sum != sum)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   245
                throw new AssertionError("Sum mismatch");
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   246
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   247
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   248
    volatile Check check = new Check();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   249
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   250
    public static void main(String[] args) throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   251
        new IteratorMicroBenchmark(args).run();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   252
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   253
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   254
    void run() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   255
//         System.out.printf(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   256
//             "iterations=%d size=%d, warmup=%1g, filter=\"%s\"%n",
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   257
//             iterations, size, warmupSeconds, filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   258
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   259
        final ArrayList<Integer> al = new ArrayList<>(size);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   260
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   261
        // Populate collections with random data
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   262
        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   263
        for (int i = 0; i < size; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   264
            al.add(rnd.nextInt(size));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   265
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   266
        final ArrayDeque<Integer> ad = new ArrayDeque<>(al);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   267
        final ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(al.size());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   268
        abq.addAll(al);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   269
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   270
        // shuffle circular array elements so they wrap
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   271
        for (int i = 0, n = rnd.nextInt(size); i < n; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   272
            ad.addLast(ad.removeFirst());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   273
            abq.add(abq.remove());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   274
        }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   275
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   276
        ArrayList<Job> jobs = new ArrayList<>(Arrays.asList());
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   277
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   278
        List.of(al, ad, abq,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   279
                new LinkedList<>(al),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   280
                new PriorityQueue<>(al),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   281
                new Vector<>(al),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   282
                new ConcurrentLinkedQueue<>(al),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   283
                new ConcurrentLinkedDeque<>(al),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   284
                new LinkedBlockingQueue<>(al),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   285
                new LinkedBlockingDeque<>(al),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   286
                new LinkedTransferQueue<>(al),
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   287
                new PriorityBlockingQueue<>(al))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   288
            .stream()
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   289
            .forEach(x -> {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   290
                         jobs.addAll(collectionJobs(x));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   291
                         if (x instanceof Deque)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   292
                             jobs.addAll(dequeJobs((Deque<Integer>)x));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   293
                     });
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   294
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   295
        if (reverse) Collections.reverse(jobs);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   296
        if (shuffle) Collections.shuffle(jobs);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   297
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   298
        time(filter(filter, jobs));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   299
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   300
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   301
    List<Job> collectionJobs(Collection<Integer> x) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   302
        String klazz = x.getClass().getSimpleName();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   303
        return List.of(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   304
            new Job(klazz + " iterate for loop") {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   305
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   306
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   307
                        int sum = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   308
                        for (Integer n : x)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   309
                            sum += n;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   310
                        check.sum(sum);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   311
            new Job(klazz + " iterator().forEachRemaining()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   312
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   313
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   314
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   315
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   316
                        x.iterator().forEachRemaining(n -> sum[0] += n);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   317
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   318
            new Job(klazz + " spliterator().tryAdvance()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   319
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   320
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   321
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   322
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   323
                        Spliterator<Integer> spliterator = x.spliterator();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   324
                        do {} while (spliterator.tryAdvance(n -> sum[0] += n));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   325
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   326
            new Job(klazz + " spliterator().forEachRemaining()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   327
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   328
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   329
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   330
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   331
                        x.spliterator().forEachRemaining(n -> sum[0] += n);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   332
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   333
            new Job(klazz + " removeIf") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   334
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   335
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   336
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   337
                        sum[0] = 0;
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   338
                        if (x.removeIf(n -> { sum[0] += n; return false; }))
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   339
                            throw new AssertionError();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   340
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   341
            new Job(klazz + " contains") {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   342
                public void work() throws Throwable {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   343
                    int[] sum = new int[1];
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   344
                    Object y = new Object() {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   345
                        public boolean equals(Object z) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   346
                            sum[0] += (int) z; return false; }};
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   347
                    for (int i = 0; i < iterations; i++) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   348
                        sum[0] = 0;
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   349
                        if (x.contains(y)) throw new AssertionError();
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   350
                        check.sum(sum[0]);}}},
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   351
            new Job(klazz + " remove(Object)") {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   352
                public void work() throws Throwable {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   353
                    int[] sum = new int[1];
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   354
                    Object y = new Object() {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   355
                        public boolean equals(Object z) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   356
                            sum[0] += (int) z; return false; }};
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   357
                    for (int i = 0; i < iterations; i++) {
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   358
                        sum[0] = 0;
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   359
                        if (x.remove(y)) throw new AssertionError();
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   360
                        check.sum(sum[0]);}}},
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   361
            new Job(klazz + " forEach") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   362
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   363
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   364
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   365
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   366
                        x.forEach(n -> sum[0] += n);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   367
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   368
            new Job(klazz + " toArray()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   369
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   370
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   371
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   372
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   373
                        for (Object o : x.toArray())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   374
                            sum[0] += (Integer) o;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   375
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   376
            new Job(klazz + " toArray(a)") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   377
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   378
                    Integer[] a = new Integer[x.size()];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   379
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   380
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   381
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   382
                        x.toArray(a);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   383
                        for (Object o : a)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   384
                            sum[0] += (Integer) o;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   385
                        check.sum(sum[0]);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   386
            new Job(klazz + " toArray(empty)") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   387
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   388
                    Integer[] empty = new Integer[0];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   389
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   390
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   391
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   392
                        for (Integer o : x.toArray(empty))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   393
                            sum[0] += o;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   394
                        check.sum(sum[0]);}}},
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   395
            new Job(klazz + " stream().forEach") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   396
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   397
                    int[] sum = new int[1];
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   398
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   399
                        sum[0] = 0;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   400
                        x.stream().forEach(n -> sum[0] += n);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   401
                        check.sum(sum[0]);}}},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   402
            new Job(klazz + " stream().mapToInt") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   403
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   404
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   405
                        check.sum(x.stream().mapToInt(e -> e).sum());}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   406
            new Job(klazz + " stream().collect") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   407
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   408
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   409
                        check.sum(x.stream()
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   410
                                  .collect(summingInt(e -> e)));}}},
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   411
            new Job(klazz + " stream()::iterator") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   412
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   413
                    int[] sum = new int[1];
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   414
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   415
                        sum[0] = 0;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   416
                        for (Integer o : (Iterable<Integer>) x.stream()::iterator)
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   417
                            sum[0] += o;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   418
                        check.sum(sum[0]);}}},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   419
            new Job(klazz + " parallelStream().forEach") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   420
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   421
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   422
                        LongAdder sum = new LongAdder();
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   423
                        x.parallelStream().forEach(n -> sum.add(n));
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   424
                        check.sum((int) sum.sum());}}},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   425
            new Job(klazz + " parallelStream().mapToInt") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   426
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   427
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   428
                        check.sum(x.parallelStream().mapToInt(e -> e).sum());}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   429
            new Job(klazz + " parallelStream().collect") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   430
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   431
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   432
                        check.sum(x.parallelStream()
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   433
                                  .collect(summingInt(e -> e)));}}},
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   434
            new Job(klazz + " parallelStream()::iterator") {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   435
                public void work() throws Throwable {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   436
                    int[] sum = new int[1];
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   437
                    for (int i = 0; i < iterations; i++) {
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   438
                        sum[0] = 0;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   439
                        for (Integer o : (Iterable<Integer>) x.parallelStream()::iterator)
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   440
                            sum[0] += o;
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 43522
diff changeset
   441
                        check.sum(sum[0]);}}});
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   442
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   443
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   444
    List<Job> dequeJobs(Deque<Integer> x) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   445
        String klazz = x.getClass().getSimpleName();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   446
        return List.of(
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   447
            new Job(klazz + " descendingIterator() loop") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   448
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   449
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   450
                        int sum = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   451
                        Iterator<Integer> it = x.descendingIterator();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   452
                        while (it.hasNext())
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   453
                            sum += it.next();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   454
                        check.sum(sum);}}},
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 42319
diff changeset
   455
            new Job(klazz + " descendingIterator().forEachRemaining()") {
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   456
                public void work() throws Throwable {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   457
                    int[] sum = new int[1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   458
                    for (int i = 0; i < iterations; i++) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   459
                        sum[0] = 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   460
                        x.descendingIterator().forEachRemaining(n -> sum[0] += n);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   461
                        check.sum(sum[0]);}}});
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   462
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents:
diff changeset
   463
}