test/jdk/java/util/Arrays/SortingLongBenchmarkTestJMH.java
author pli
Tue, 16 Jul 2019 00:57:00 +0000
changeset 55689 8c5c9d86e1d6
parent 47216 71c04702a3d5
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:
31079
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     1
/*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     2
 * Copyright 2015 Goldman Sachs.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     3
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     5
 *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     8
 * published by the Free Software Foundation.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
     9
 *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    14
 * accompanied this code).
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    15
 *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    19
 *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    22
 * questions.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    23
 */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    24
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    25
import org.openjdk.jmh.annotations.Benchmark;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    26
import org.openjdk.jmh.annotations.BenchmarkMode;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    27
import org.openjdk.jmh.annotations.Measurement;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    28
import org.openjdk.jmh.annotations.Mode;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    29
import org.openjdk.jmh.annotations.OutputTimeUnit;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    30
import org.openjdk.jmh.annotations.Param;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    31
import org.openjdk.jmh.annotations.Scope;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    32
import org.openjdk.jmh.annotations.Setup;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    33
import org.openjdk.jmh.annotations.State;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    34
import org.openjdk.jmh.annotations.Warmup;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    35
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    36
import java.util.ArrayList;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    37
import java.util.Arrays;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    38
import java.util.HashSet;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    39
import java.util.List;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    40
import java.util.Random;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    41
import java.util.Set;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    42
import java.util.concurrent.TimeUnit;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    43
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    44
@State(Scope.Thread)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    45
@BenchmarkMode(Mode.Throughput)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    46
@OutputTimeUnit(TimeUnit.SECONDS)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    47
public class SortingLongBenchmarkTestJMH {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    48
    private static final int QUICKSORT_THRESHOLD = 286;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    49
    private static final int MAX_RUN_COUNT = 67;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    50
    private static final int INSERTION_SORT_THRESHOLD = 47;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    51
    public static final int MAX_VALUE = 1_000_000;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    52
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    53
    @Param({"pairFlipZeroPairFlip", "descendingAscending", "zeroHi", "hiZeroLow", "hiFlatLow", "identical",
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    54
            "randomDups", "randomNoDups", "sortedReversedSorted", "pairFlip", "endLessThan"})
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    55
    public String listType;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    56
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    57
    private long[] array;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    58
    private static final int LIST_SIZE = 10_000_000;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    59
    public static final int NUMBER_OF_ITERATIONS = 10;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    60
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    61
    @Setup
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    62
    public void setUp() {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    63
        Random random = new Random(123456789012345L);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    64
        this.array = new long[LIST_SIZE];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    65
        int threeQuarters = (int) (LIST_SIZE * 0.75);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    66
        if ("zeroHi".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    67
            for (int i = 0; i < threeQuarters; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    68
                this.array[i] = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    69
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    70
            int k = 1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    71
            for (int i = threeQuarters; i < LIST_SIZE; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    72
                this.array[i] = k;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    73
                k++;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    74
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    75
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    76
        else if ("hiFlatLow".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    77
            int oneThird = LIST_SIZE / 3;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    78
            for (int i = 0; i < oneThird; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    79
                this.array[i] = i;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    80
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    81
            int twoThirds = oneThird * 2;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    82
            int constant = oneThird - 1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    83
            for (int i = oneThird; i < twoThirds; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    84
                this.array[i] = constant;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    85
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    86
            for (int i = twoThirds; i < LIST_SIZE; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    87
                this.array[i] = constant - i + twoThirds;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    88
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    89
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    90
        else if ("hiZeroLow".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    91
            int oneThird = LIST_SIZE / 3;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    92
            for (int i = 0; i < oneThird; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    93
                this.array[i] = i;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    94
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    95
            int twoThirds = oneThird * 2;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    96
            for (int i = oneThird; i < twoThirds; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    97
                this.array[i] = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    98
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
    99
            for (int i = twoThirds; i < LIST_SIZE; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   100
                this.array[i] = oneThird - i + twoThirds;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   101
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   102
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   103
        else if ("identical".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   104
            for (int i = 0; i < LIST_SIZE; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   105
                this.array[i] = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   106
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   107
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   108
        else if ("randomDups".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   109
            for (int i = 0; i < LIST_SIZE; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   110
                this.array[i] = random.nextInt(1000);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   111
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   112
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   113
        else if ("randomNoDups".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   114
            Set<Integer> set = new HashSet<>();
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   115
            while (set.size() < LIST_SIZE + 1) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   116
                set.add(random.nextInt());
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   117
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   118
            List<Integer> list = new ArrayList<>(LIST_SIZE);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   119
            list.addAll(set);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   120
            for (int i = 0; i < LIST_SIZE; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   121
                this.array[i] = list.get(i);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   122
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   123
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   124
        else if ("sortedReversedSorted".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   125
            for (int i = 0; i < LIST_SIZE / 2; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   126
                this.array[i] = i;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   127
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   128
            int num = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   129
            for (int i = LIST_SIZE / 2; i < LIST_SIZE; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   130
                this.array[i] = LIST_SIZE - num;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   131
                num++;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   132
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   133
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   134
        else if ("pairFlip".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   135
            for (int i = 0; i < LIST_SIZE; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   136
                this.array[i] = i;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   137
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   138
            for (int i = 0; i < LIST_SIZE; i += 2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   139
                long temp = this.array[i];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   140
                this.array[i] = this.array[i + 1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   141
                this.array[i + 1] = temp;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   142
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   143
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   144
        else if ("endLessThan".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   145
            for (int i = 0; i < LIST_SIZE - 1; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   146
                this.array[i] = 3;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   147
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   148
            this.array[LIST_SIZE - 1] = 1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   149
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   150
        else if ("pairFlipZeroPairFlip".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   151
            //pairflip
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   152
            for (int i = 0; i < 64; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   153
                this.array[i] = i;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   154
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   155
            for (int i = 0; i < 64; i += 2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   156
                long temp = this.array[i];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   157
                this.array[i] = this.array[i + 1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   158
                this.array[i + 1] = temp;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   159
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   160
            //zero
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   161
            for (int i = 64; i < this.array.length - 64; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   162
                this.array[i] = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   163
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   164
            //pairflip
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   165
            for (int i = this.array.length - 64; i < this.array.length; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   166
                this.array[i] = i;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   167
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   168
            for (int i = this.array.length - 64; i < this.array.length; i += 2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   169
                long temp = this.array[i];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   170
                this.array[i] = this.array[i + 1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   171
                this.array[i + 1] = temp;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   172
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   173
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   174
        else if ("pairFlipOneHundredPairFlip".equals(this.listType)) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   175
            //10, 5
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   176
            for (int i = 0; i < 64; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   177
                if (i % 2 == 0) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   178
                    this.array[i] = 10;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   179
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   180
                else {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   181
                    this.array[i] = 5;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   182
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   183
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   184
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   185
            //100
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   186
            for (int i = 64; i < this.array.length - 64; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   187
                this.array[i] = 100;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   188
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   189
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   190
            //10, 5
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   191
            for (int i = this.array.length - 64; i < this.array.length; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   192
                if (i % 2 == 0) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   193
                    this.array[i] = 10;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   194
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   195
                else {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   196
                    this.array[i] = 5;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   197
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   198
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   199
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   200
    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   201
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   202
    @Warmup(iterations = 20)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   203
    @Measurement(iterations = 10)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   204
    @Benchmark
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   205
    public void sortNewWay() {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   206
        for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   207
            SortingLongTestJMH.sort(this.array, 0, this.array.length - 1, null, 0, 0);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   208
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   209
    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   210
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   211
    @Warmup(iterations = 20)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   212
    @Measurement(iterations = 10)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   213
    @Benchmark
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   214
    public void sortOldWay() {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   215
        for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   216
            Arrays.sort(this.array);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   217
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   218
    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   219
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   220
    /**
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   221
     * Sorts the specified range of the array using the given
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   222
     * workspace array slice if possible for merging
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   223
     *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   224
     * @param a the array to be sorted
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   225
     * @param left the index of the first element, inclusive, to be sorted
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   226
     * @param right the index of the last element, inclusive, to be sorted
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   227
     * @param work a workspace array (slice)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   228
     * @param workBase origin of usable space in work array
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   229
     * @param workLen usable size of work array
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   230
     */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   231
    static void sort(long[] a, int left, int right,
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   232
                     long[] work, int workBase, int workLen) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   233
// Use Quicksort on small arrays
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   234
        if (right - left < QUICKSORT_THRESHOLD) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   235
            SortingLongTestJMH.sort(a, left, right, true);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   236
            return;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   237
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   238
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   239
          /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   240
         * Index run[i] is the start of i-th run
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   241
         * (ascending or descending sequence).
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   242
         */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   243
        int[] run = new int[MAX_RUN_COUNT + 1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   244
        int count = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   245
        run[0] = left;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   246
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   247
        // Check if the array is nearly sorted
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   248
        for (int k = left; k < right; run[count] = k) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   249
            while (k < right && a[k] == a[k + 1])
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   250
                k++;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   251
            if (k == right) break;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   252
            if (a[k] < a[k + 1]) { // ascending
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   253
                while (++k <= right && a[k - 1] <= a[k]) ;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   254
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   255
            else if (a[k] > a[k + 1]) { // descending
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   256
                while (++k <= right && a[k - 1] >= a[k]) ;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   257
                for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   258
                    long t = a[lo];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   259
                    a[lo] = a[hi];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   260
                    a[hi] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   261
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   262
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   263
            if (run[count] > left && a[run[count]] >= a[run[count] - 1]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   264
                count--;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   265
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   266
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   267
             * The array is not highly structured,
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   268
             * use Quicksort instead of merge sort.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   269
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   270
            if (++count == MAX_RUN_COUNT) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   271
                sort(a, left, right, true);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   272
                return;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   273
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   274
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   275
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   276
        // Check special cases
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   277
        // Implementation note: variable "right" is increased by 1.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   278
        if (run[count] == right++) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   279
            run[++count] = right;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   280
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   281
        if (count <= 1) { // The array is already sorted
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   282
            return;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   283
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   284
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   285
        // Determine alternation base for merge
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   286
        byte odd = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   287
        for (int n = 1; (n <<= 1) < count; odd ^= 1) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   288
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   289
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   290
        // Use or create temporary array b for merging
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   291
        long[] b;                  // temp array; alternates with a
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   292
        int ao, bo;                 // array offsets from 'left'
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   293
        int blen = right - left; // space needed for b
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   294
        if (work == null || workLen < blen || workBase + blen > work.length) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   295
            work = new long[blen];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   296
            workBase = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   297
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   298
        if (odd == 0) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   299
            System.arraycopy(a, left, work, workBase, blen);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   300
            b = a;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   301
            bo = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   302
            a = work;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   303
            ao = workBase - left;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   304
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   305
        else {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   306
            b = work;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   307
            ao = 0;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   308
            bo = workBase - left;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   309
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   310
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   311
        // Merging
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   312
        for (int last; count > 1; count = last) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   313
            for (int k = (last = 0) + 2; k <= count; k += 2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   314
                int hi = run[k], mi = run[k - 1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   315
                for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   316
                    if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   317
                        b[i + bo] = a[p++ + ao];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   318
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   319
                    else {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   320
                        b[i + bo] = a[q++ + ao];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   321
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   322
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   323
                run[++last] = hi;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   324
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   325
            if ((count & 1) != 0) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   326
                for (int i = right, lo = run[count - 1]; --i >= lo;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   327
                     b[i + bo] = a[i + ao]
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   328
                        ) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   329
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   330
                run[++last] = right;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   331
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   332
            long[] t = a;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   333
            a = b;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   334
            b = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   335
            int o = ao;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   336
            ao = bo;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   337
            bo = o;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   338
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   339
    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   340
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   341
    /**
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   342
     * Sorts the specified range of the array by Dual-Pivot Quicksort.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   343
     *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   344
     * @param a the array to be sorted
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   345
     * @param left the index of the first element, inclusive, to be sorted
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   346
     * @param right the index of the last element, inclusive, to be sorted
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   347
     * @param leftmost indicates if this part is the leftmost in the range
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   348
     */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   349
    private static void sort(long[] a, int left, int right, boolean leftmost) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   350
        int length = right - left + 1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   351
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   352
        // Use insertion sort on tiny arrays
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   353
        if (length < INSERTION_SORT_THRESHOLD) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   354
            if (leftmost) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   355
                /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   356
                 * Traditional (without sentinel) insertion sort,
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   357
                 * optimized for server VM, is used in case of
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   358
                 * the leftmost part.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   359
                 */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   360
                for (int i = left, j = i; i < right; j = ++i) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   361
                    long ai = a[i + 1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   362
                    while (ai < a[j]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   363
                        a[j + 1] = a[j];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   364
                        if (j-- == left) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   365
                            break;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   366
                        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   367
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   368
                    a[j + 1] = ai;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   369
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   370
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   371
            else {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   372
                /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   373
                 * Skip the longest ascending sequence.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   374
                 */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   375
                do {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   376
                    if (left >= right) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   377
                        return;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   378
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   379
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   380
                while (a[++left] >= a[left - 1]);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   381
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   382
                /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   383
                 * Every element from adjoining part plays the role
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   384
                 * of sentinel, therefore this allows us to avoid the
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   385
                 * left range check on each iteration. Moreover, we use
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   386
                 * the more optimized algorithm, so called pair insertion
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   387
                 * sort, which is faster (in the context of Quicksort)
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   388
                 * than traditional implementation of insertion sort.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   389
                 */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   390
                for (int k = left; ++left <= right; k = ++left) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   391
                    long a1 = a[k], a2 = a[left];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   392
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   393
                    if (a1 < a2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   394
                        a2 = a1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   395
                        a1 = a[left];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   396
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   397
                    while (a1 < a[--k]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   398
                        a[k + 2] = a[k];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   399
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   400
                    a[++k + 1] = a1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   401
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   402
                    while (a2 < a[--k]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   403
                        a[k + 1] = a[k];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   404
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   405
                    a[k + 1] = a2;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   406
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   407
                long last = a[right];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   408
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   409
                while (last < a[--right]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   410
                    a[right + 1] = a[right];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   411
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   412
                a[right + 1] = last;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   413
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   414
            return;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   415
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   416
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   417
        // Inexpensive approximation of length / 7
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   418
        int seventh = (length >> 3) + (length >> 6) + 1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   419
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   420
        /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   421
         * Sort five evenly spaced elements around (and including) the
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   422
         * center element in the range. These elements will be used for
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   423
         * pivot selection as described below. The choice for spacing
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   424
         * these elements was empirically determined to work well on
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   425
         * a wide variety of inputs.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   426
         */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   427
        int e3 = (left + right) >>> 1; // The midpoint
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   428
        int e2 = e3 - seventh;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   429
        int e1 = e2 - seventh;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   430
        int e4 = e3 + seventh;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   431
        int e5 = e4 + seventh;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   432
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   433
        // Sort these elements using insertion sort
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   434
        if (a[e2] < a[e1]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   435
            long t = a[e2];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   436
            a[e2] = a[e1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   437
            a[e1] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   438
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   439
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   440
        if (a[e3] < a[e2]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   441
            long t = a[e3];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   442
            a[e3] = a[e2];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   443
            a[e2] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   444
            if (t < a[e1]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   445
                a[e2] = a[e1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   446
                a[e1] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   447
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   448
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   449
        if (a[e4] < a[e3]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   450
            long t = a[e4];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   451
            a[e4] = a[e3];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   452
            a[e3] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   453
            if (t < a[e2]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   454
                a[e3] = a[e2];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   455
                a[e2] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   456
                if (t < a[e1]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   457
                    a[e2] = a[e1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   458
                    a[e1] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   459
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   460
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   461
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   462
        if (a[e5] < a[e4]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   463
            long t = a[e5];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   464
            a[e5] = a[e4];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   465
            a[e4] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   466
            if (t < a[e3]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   467
                a[e4] = a[e3];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   468
                a[e3] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   469
                if (t < a[e2]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   470
                    a[e3] = a[e2];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   471
                    a[e2] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   472
                    if (t < a[e1]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   473
                        a[e2] = a[e1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   474
                        a[e1] = t;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   475
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   476
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   477
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   478
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   479
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   480
        // Pointers
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   481
        int less = left;  // The index of the first element of center part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   482
        int great = right; // The index before the first element of right part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   483
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   484
        if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   485
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   486
             * Use the second and fourth of the five sorted elements as pivots.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   487
             * These values are inexpensive approximations of the first and
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   488
             * second terciles of the array. Note that pivot1 <= pivot2.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   489
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   490
            long pivot1 = a[e2];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   491
            long pivot2 = a[e4];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   492
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   493
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   494
             * The first and the last elements to be sorted are moved to the
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   495
             * locations formerly occupied by the pivots. When partitioning
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   496
             * is complete, the pivots are swapped back into their final
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   497
             * positions, and excluded from subsequent sorting.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   498
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   499
            a[e2] = a[left];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   500
            a[e4] = a[right];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   501
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   502
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   503
             * Skip elements, which are less or greater than pivot values.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   504
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   505
            while (a[++less] < pivot1) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   506
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   507
            while (a[--great] > pivot2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   508
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   509
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   510
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   511
             * Partitioning:
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   512
             *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   513
             *   left part           center part                   right part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   514
             * +--------------------------------------------------------------+
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   515
             * |  < pivot1  |  pivot1 <= && <= pivot2  |    ?    |  > pivot2  |
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   516
             * +--------------------------------------------------------------+
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   517
             *               ^                          ^       ^
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   518
             *               |                          |       |
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   519
             *              less                        k     great
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   520
             *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   521
             * Invariants:
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   522
             *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   523
             *              all in (left, less)   < pivot1
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   524
             *    pivot1 <= all in [less, k)      <= pivot2
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   525
             *              all in (great, right) > pivot2
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   526
             *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   527
             * Pointer k is the first index of ?-part.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   528
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   529
            outer:
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   530
            for (int k = less - 1; ++k <= great; ) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   531
                long ak = a[k];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   532
                if (ak < pivot1) { // Move a[k] to left part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   533
                    a[k] = a[less];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   534
                    /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   535
                     * Here and below we use "a[i] = b; i++;" instead
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   536
                     * of "a[i++] = b;" due to performance issue.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   537
                     */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   538
                    a[less] = ak;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   539
                    ++less;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   540
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   541
                else if (ak > pivot2) { // Move a[k] to right part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   542
                    while (a[great] > pivot2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   543
                        if (great-- == k) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   544
                            break outer;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   545
                        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   546
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   547
                    if (a[great] < pivot1) { // a[great] <= pivot2
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   548
                        a[k] = a[less];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   549
                        a[less] = a[great];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   550
                        ++less;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   551
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   552
                    else { // pivot1 <= a[great] <= pivot2
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   553
                        a[k] = a[great];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   554
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   555
                    /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   556
                     * Here and below we use "a[i] = b; i--;" instead
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   557
                     * of "a[i--] = b;" due to performance issue.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   558
                     */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   559
                    a[great] = ak;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   560
                    --great;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   561
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   562
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   563
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   564
            // Swap pivots into their final positions
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   565
            a[left] = a[less - 1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   566
            a[less - 1] = pivot1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   567
            a[right] = a[great + 1];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   568
            a[great + 1] = pivot2;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   569
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   570
            // Sort left and right parts recursively, excluding known pivots
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   571
            SortingLongTestJMH.sort(a, left, less - 2, leftmost);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   572
            SortingLongTestJMH.sort(a, great + 2, right, false);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   573
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   574
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   575
             * If center part is too large (comprises > 4/7 of the array),
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   576
             * swap internal pivot values to ends.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   577
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   578
            if (less < e1 && e5 < great) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   579
                /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   580
                 * Skip elements, which are equal to pivot values.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   581
                 */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   582
                while (a[less] == pivot1) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   583
                    ++less;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   584
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   585
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   586
                while (a[great] == pivot2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   587
                    --great;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   588
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   589
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   590
                /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   591
                 * Partitioning:
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   592
                 *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   593
                 *   left part         center part                  right part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   594
                 * +----------------------------------------------------------+
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   595
                 * | == pivot1 |  pivot1 < && < pivot2  |    ?    | == pivot2 |
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   596
                 * +----------------------------------------------------------+
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   597
                 *              ^                        ^       ^
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   598
                 *              |                        |       |
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   599
                 *             less                      k     great
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   600
                 *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   601
                 * Invariants:
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   602
                 *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   603
                 *              all in (*,  less) == pivot1
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   604
                 *     pivot1 < all in [less,  k)  < pivot2
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   605
                 *              all in (great, *) == pivot2
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   606
                 *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   607
                 * Pointer k is the first index of ?-part.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   608
                 */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   609
                outer:
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   610
                for (int k = less - 1; ++k <= great; ) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   611
                    long ak = a[k];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   612
                    if (ak == pivot1) { // Move a[k] to left part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   613
                        a[k] = a[less];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   614
                        a[less] = ak;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   615
                        ++less;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   616
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   617
                    else if (ak == pivot2) { // Move a[k] to right part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   618
                        while (a[great] == pivot2) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   619
                            if (great-- == k) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   620
                                break outer;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   621
                            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   622
                        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   623
                        if (a[great] == pivot1) { // a[great] < pivot2
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   624
                            a[k] = a[less];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   625
                            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   626
                             * Even though a[great] equals to pivot1, the
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   627
                             * assignment a[less] = pivot1 may be incorrect,
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   628
                             * if a[great] and pivot1 are floating-point zeros
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   629
                             * of different signs. Therefore in float and
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   630
                             * double sorting methods we have to use more
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   631
                             * accurate assignment a[less] = a[great].
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   632
                             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   633
                            a[less] = pivot1;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   634
                            ++less;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   635
                        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   636
                        else { // pivot1 < a[great] < pivot2
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   637
                            a[k] = a[great];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   638
                        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   639
                        a[great] = ak;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   640
                        --great;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   641
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   642
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   643
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   644
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   645
            // Sort center part recursively
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   646
            SortingLongTestJMH.sort(a, less, great, false);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   647
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   648
        else { // Partitioning with one pivot
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   649
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   650
             * Use the third of the five sorted elements as pivot.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   651
             * This value is inexpensive approximation of the median.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   652
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   653
            long pivot = a[e3];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   654
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   655
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   656
             * Partitioning degenerates to the traditional 3-way
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   657
             * (or "Dutch National Flag") schema:
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   658
             *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   659
             *   left part    center part              right part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   660
             * +-------------------------------------------------+
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   661
             * |  < pivot  |   == pivot   |     ?    |  > pivot  |
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   662
             * +-------------------------------------------------+
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   663
             *              ^              ^        ^
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   664
             *              |              |        |
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   665
             *             less            k      great
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   666
             *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   667
             * Invariants:
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   668
             *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   669
             *   all in (left, less)   < pivot
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   670
             *   all in [less, k)     == pivot
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   671
             *   all in (great, right) > pivot
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   672
             *
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   673
             * Pointer k is the first index of ?-part.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   674
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   675
            for (int k = less; k <= great; ++k) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   676
                if (a[k] == pivot) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   677
                    continue;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   678
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   679
                long ak = a[k];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   680
                if (ak < pivot) { // Move a[k] to left part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   681
                    a[k] = a[less];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   682
                    a[less] = ak;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   683
                    ++less;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   684
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   685
                else { // a[k] > pivot - Move a[k] to right part
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   686
                    while (a[great] > pivot) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   687
                        --great;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   688
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   689
                    if (a[great] < pivot) { // a[great] <= pivot
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   690
                        a[k] = a[less];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   691
                        a[less] = a[great];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   692
                        ++less;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   693
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   694
                    else { // a[great] == pivot
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   695
                        /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   696
                         * Even though a[great] equals to pivot, the
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   697
                         * assignment a[k] = pivot may be incorrect,
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   698
                         * if a[great] and pivot are floating-point
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   699
                         * zeros of different signs. Therefore in float
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   700
                         * and double sorting methods we have to use
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   701
                         * more accurate assignment a[k] = a[great].
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   702
                         */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   703
                        a[k] = pivot;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   704
                    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   705
                    a[great] = ak;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   706
                    --great;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   707
                }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   708
            }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   709
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   710
            /*
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   711
             * Sort left and right parts recursively.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   712
             * All elements from center part are equal
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   713
             * and, therefore, already sorted.
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   714
             */
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   715
            SortingLongTestJMH.sort(a, left, less - 1, leftmost);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   716
            SortingLongTestJMH.sort(a, great + 1, right, false);
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   717
        }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   718
    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   719
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   720
    private static void swap(long[] arr, int i, int j) {
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   721
        long tmp = arr[i];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   722
        arr[i] = arr[j];
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   723
        arr[j] = tmp;
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   724
    }
bf5fabb914b6 8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff changeset
   725
}