author | pli |
Tue, 16 Jul 2019 00:57:00 +0000 | |
changeset 55689 | 8c5c9d86e1d6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
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 SortingIntBenchmarkTestJMH { |
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", "pairFlipOneHundredPairFlip" |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
54 |
, "zeroHi", "hiZeroLow", "hiFlatLow", "identical", |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
55 |
"randomDups", "randomNoDups", "sortedReversedSorted", "pairFlip", "endLessThan"}) |
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 |
public String listType; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
58 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
59 |
private int[] array; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
63 |
@Setup |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
64 |
public void setUp() { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
65 |
Random random = new Random(123456789012345L); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
66 |
this.array = new int[LIST_SIZE]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
67 |
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
|
68 |
if ("zeroHi".equals(this.listType)) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
69 |
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
|
70 |
this.array[i] = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
71 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
72 |
int k = 1; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
73 |
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
|
74 |
this.array[i] = k; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
75 |
k++; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
76 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
77 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
78 |
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
|
79 |
int oneThird = LIST_SIZE / 3; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
80 |
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
|
81 |
this.array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
82 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
83 |
int twoThirds = oneThird * 2; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
84 |
int constant = oneThird - 1; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
85 |
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
|
86 |
this.array[i] = constant; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
87 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
91 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
92 |
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
|
93 |
int oneThird = LIST_SIZE / 3; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
94 |
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
|
95 |
this.array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
96 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
97 |
int twoThirds = oneThird * 2; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
98 |
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
|
99 |
this.array[i] = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
100 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
104 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
this.array[i] = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
108 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
109 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
114 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
115 |
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
|
116 |
Set<Integer> set = new HashSet(); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
117 |
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
|
118 |
set.add(random.nextInt()); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
119 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
120 |
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
|
121 |
list.addAll(set); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
122 |
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
|
123 |
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
|
124 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
125 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
126 |
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
|
127 |
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
|
128 |
this.array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
129 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
130 |
int num = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
131 |
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
|
132 |
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
|
133 |
num++; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
134 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
135 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
136 |
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
|
137 |
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
|
138 |
this.array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
139 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
140 |
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
|
141 |
int temp = this.array[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
142 |
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
|
143 |
this.array[i + 1] = temp; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
144 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
145 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
146 |
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
|
147 |
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
|
148 |
this.array[i] = 3; |
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 |
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
|
151 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
152 |
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
|
153 |
//pairflip |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
154 |
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
|
155 |
this.array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
156 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
157 |
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
|
158 |
int temp = this.array[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
159 |
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
|
160 |
this.array[i + 1] = temp; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
161 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
162 |
//zero |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
163 |
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
|
164 |
this.array[i] = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
165 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
166 |
//pairflip |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
167 |
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
|
168 |
this.array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
169 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
170 |
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
|
171 |
int temp = this.array[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
172 |
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
|
173 |
this.array[i + 1] = temp; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
174 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
175 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
176 |
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
|
177 |
//10, 5 |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
178 |
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
|
179 |
if (i % 2 == 0) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
180 |
this.array[i] = 10; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
181 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
182 |
else { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
183 |
this.array[i] = 5; |
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 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
186 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
187 |
//100 |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
188 |
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
|
189 |
this.array[i] = 100; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
190 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
191 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
192 |
//10, 5 |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
193 |
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
|
194 |
if (i % 2 == 0) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
195 |
this.array[i] = 10; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
196 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
197 |
else { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
198 |
this.array[i] = 5; |
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 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
203 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
204 |
@Warmup(iterations = 20) |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
205 |
@Measurement(iterations = 10) |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
206 |
@Benchmark |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
207 |
public void sortNewWay() { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
208 |
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
|
209 |
SortingIntTestJMH.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
|
210 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
211 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
212 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
213 |
@Warmup(iterations = 20) |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
214 |
@Measurement(iterations = 10) |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
215 |
@Benchmark |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
216 |
public void sortCurrentWay() { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
217 |
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
|
218 |
Arrays.sort(this.array); |
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 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
222 |
static void sort(int[] a, int left, int right, |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
223 |
int[] work, int workBase, int workLen) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
224 |
// Use Quicksort on small arrays |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
225 |
if (right - left < QUICKSORT_THRESHOLD) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
226 |
SortingIntTestJMH.sort(a, left, right, true); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
227 |
return; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
228 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
229 |
|
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 |
* 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
|
232 |
* (ascending or descending sequence). |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
233 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
234 |
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
|
235 |
int count = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
236 |
run[0] = left; |
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 |
// 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
|
239 |
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
|
240 |
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
|
241 |
k++; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
242 |
if (k == right) break; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
243 |
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
|
244 |
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
|
245 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
int t = a[lo]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
250 |
a[lo] = a[hi]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
251 |
a[hi] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
252 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
253 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
254 |
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
|
255 |
count--; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
256 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
257 |
/* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
258 |
* 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
|
259 |
* 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
|
260 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
261 |
if (++count == MAX_RUN_COUNT) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
262 |
sort(a, left, right, true); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
263 |
return; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
264 |
} |
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 |
// Check special cases |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
268 |
// 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
|
269 |
if (run[count] == right++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
270 |
run[++count] = right; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
271 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
272 |
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
|
273 |
return; |
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 |
// Determine alternation base for merge |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
277 |
byte odd = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
278 |
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
|
279 |
} |
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 |
// 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
|
282 |
int[] 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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
work = new int[blen]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
287 |
workBase = 0; |
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 |
if (odd == 0) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
290 |
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
|
291 |
b = a; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
292 |
bo = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
293 |
a = work; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
294 |
ao = workBase - left; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
295 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
296 |
else { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
297 |
b = work; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
298 |
ao = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
299 |
bo = workBase - left; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
300 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
301 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
302 |
// Merging |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
303 |
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
|
304 |
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
|
305 |
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
|
306 |
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
|
307 |
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
|
308 |
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
|
309 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
310 |
else { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
311 |
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
|
312 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
313 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
314 |
run[++last] = hi; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
315 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
316 |
if ((count & 1) != 0) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
317 |
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
|
318 |
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
|
319 |
) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
320 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
321 |
run[++last] = right; |
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 |
int[] t = a; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
324 |
a = b; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
325 |
b = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
326 |
int o = ao; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
327 |
ao = bo; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
328 |
bo = o; |
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 |
} |
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 |
private static void sort(int[] 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
|
333 |
int length = right - left + 1; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
334 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
335 |
// 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
|
336 |
if (length < INSERTION_SORT_THRESHOLD) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
337 |
if (leftmost) { |
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 |
* Traditional (without sentinel) insertion sort, |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
340 |
* 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
|
341 |
* the leftmost part. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
342 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
343 |
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
|
344 |
int ai = a[i + 1]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
345 |
while (ai < a[j]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
346 |
a[j + 1] = a[j]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
347 |
if (j-- == left) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
348 |
break; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
349 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
350 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
351 |
a[j + 1] = ai; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
352 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
353 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
354 |
else { |
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 |
* Skip the longest ascending sequence. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
357 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
358 |
do { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
359 |
if (left >= right) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
360 |
return; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
361 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
362 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
363 |
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
|
364 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
365 |
/* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
366 |
* 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
|
367 |
* 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
|
368 |
* 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
|
369 |
* 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
|
370 |
* 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
|
371 |
* 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
|
372 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
373 |
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
|
374 |
int 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
|
375 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
376 |
if (a1 < a2) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
377 |
a2 = a1; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
378 |
a1 = a[left]; |
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 (a1 < a[--k]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
381 |
a[k + 2] = a[k]; |
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 |
a[++k + 1] = a1; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
384 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
385 |
while (a2 < a[--k]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
386 |
a[k + 1] = a[k]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
387 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
388 |
a[k + 1] = a2; |
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 |
int last = a[right]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
391 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
392 |
while (last < a[--right]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
393 |
a[right + 1] = a[right]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
394 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
395 |
a[right + 1] = last; |
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 |
return; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
398 |
} |
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 |
// Inexpensive approximation of length / 7 |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
401 |
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
|
402 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
403 |
/* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
404 |
* 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
|
405 |
* 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
|
406 |
* 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
|
407 |
* 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
|
408 |
* a wide variety of inputs. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
409 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
410 |
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
|
411 |
int e2 = e3 - seventh; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
412 |
int e1 = e2 - seventh; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
413 |
int e4 = e3 + seventh; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
414 |
int e5 = e4 + seventh; |
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 |
// 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
|
417 |
if (a[e2] < a[e1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
418 |
int t = a[e2]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
419 |
a[e2] = a[e1]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
420 |
a[e1] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
421 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
422 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
423 |
if (a[e3] < a[e2]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
424 |
int t = a[e3]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
425 |
a[e3] = a[e2]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
426 |
a[e2] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
427 |
if (t < a[e1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
428 |
a[e2] = a[e1]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
429 |
a[e1] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
430 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
431 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
432 |
if (a[e4] < a[e3]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
433 |
int t = a[e4]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
434 |
a[e4] = a[e3]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
435 |
a[e3] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
436 |
if (t < a[e2]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
437 |
a[e3] = a[e2]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
438 |
a[e2] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
439 |
if (t < a[e1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
440 |
a[e2] = a[e1]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
441 |
a[e1] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
442 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
443 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
444 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
445 |
if (a[e5] < a[e4]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
446 |
int t = a[e5]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
447 |
a[e5] = a[e4]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
448 |
a[e4] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
449 |
if (t < a[e3]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
450 |
a[e4] = a[e3]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
451 |
a[e3] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
452 |
if (t < a[e2]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
453 |
a[e3] = a[e2]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
454 |
a[e2] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
455 |
if (t < a[e1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
456 |
a[e2] = a[e1]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
457 |
a[e1] = t; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
458 |
} |
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 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
463 |
// Pointers |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
464 |
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
|
465 |
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
|
466 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
467 |
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
|
468 |
/* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
469 |
* 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
|
470 |
* 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
|
471 |
* 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
|
472 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
473 |
int pivot1 = a[e2]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
474 |
int pivot2 = a[e4]; |
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 |
* 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
|
478 |
* 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
|
479 |
* 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
|
480 |
* 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
|
481 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
482 |
a[e2] = a[left]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
483 |
a[e4] = a[right]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
484 |
|
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 |
* 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
|
487 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
488 |
while (a[++less] < pivot1) { |
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 |
while (a[--great] > pivot2) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
491 |
} |
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 |
* Partitioning: |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
495 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
496 |
* 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
|
497 |
* +--------------------------------------------------------------+ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
498 |
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
499 |
* +--------------------------------------------------------------+ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
500 |
* ^ ^ ^ |
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 |
* less k great |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
503 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
504 |
* Invariants: |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
505 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
506 |
* all in (left, less) < pivot1 |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
507 |
* 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
|
508 |
* all in (great, right) > pivot2 |
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 |
* 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
|
511 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
512 |
outer: |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
513 |
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
|
514 |
int ak = a[k]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
515 |
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
|
516 |
a[k] = a[less]; |
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 |
* 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
|
519 |
* 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
|
520 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
521 |
a[less] = ak; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
522 |
++less; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
523 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
524 |
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
|
525 |
while (a[great] > pivot2) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
526 |
if (great-- == k) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
527 |
break outer; |
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 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
530 |
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
|
531 |
a[k] = a[less]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
532 |
a[less] = a[great]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
533 |
++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 |
else { // pivot1 <= a[great] <= pivot2 |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
536 |
a[k] = a[great]; |
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 |
/* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
539 |
* 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
|
540 |
* 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
|
541 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
542 |
a[great] = ak; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
543 |
--great; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
544 |
} |
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 |
// 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
|
548 |
a[left] = a[less - 1]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
549 |
a[less - 1] = pivot1; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
550 |
a[right] = a[great + 1]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
551 |
a[great + 1] = pivot2; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
552 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
553 |
// 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
|
554 |
SortingIntTestJMH.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
|
555 |
SortingIntTestJMH.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
|
556 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
557 |
/* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
558 |
* 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
|
559 |
* 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
|
560 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
561 |
if (less < e1 && e5 < great) { |
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 |
* 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
|
564 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
565 |
while (a[less] == pivot1) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
566 |
++less; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
567 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
568 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
569 |
while (a[great] == pivot2) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
570 |
--great; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
571 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
572 |
|
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 |
* Partitioning: |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
575 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
576 |
* 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
|
577 |
* +----------------------------------------------------------+ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
578 |
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
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 |
* ^ ^ ^ |
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 |
* less k great |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
583 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
584 |
* Invariants: |
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 |
* all in (*, less) == pivot1 |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
587 |
* 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
|
588 |
* all in (great, *) == pivot2 |
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 |
* 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
|
591 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
592 |
outer: |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
593 |
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
|
594 |
int ak = a[k]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
595 |
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
|
596 |
a[k] = a[less]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
597 |
a[less] = ak; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
598 |
++less; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
599 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
600 |
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
|
601 |
while (a[great] == pivot2) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
602 |
if (great-- == k) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
603 |
break outer; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
604 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
605 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
606 |
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
|
607 |
a[k] = a[less]; |
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 |
* 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
|
610 |
* 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
|
611 |
* 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
|
612 |
* 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
|
613 |
* 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
|
614 |
* 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
|
615 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
616 |
a[less] = pivot1; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
617 |
++less; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
618 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
619 |
else { // pivot1 < a[great] < pivot2 |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
620 |
a[k] = a[great]; |
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 |
a[great] = ak; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
623 |
--great; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
624 |
} |
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 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
627 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
628 |
// Sort center part recursively |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
629 |
SortingIntTestJMH.sort(a, less, great, false); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
630 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
631 |
else { // Partitioning with one pivot |
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 |
* 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
|
634 |
* 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
|
635 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
636 |
int pivot = a[e3]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
637 |
|
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 |
* 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
|
640 |
* (or "Dutch National Flag") schema: |
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 |
* 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
|
643 |
* +-------------------------------------------------+ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
644 |
* | < pivot | == pivot | ? | > pivot | |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
645 |
* +-------------------------------------------------+ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
646 |
* ^ ^ ^ |
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 |
* less k great |
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 |
* Invariants: |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
651 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
652 |
* all in (left, less) < pivot |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
653 |
* all in [less, k) == pivot |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
654 |
* all in (great, right) > pivot |
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 |
* 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
|
657 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
658 |
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
|
659 |
if (a[k] == pivot) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
660 |
continue; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
661 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
662 |
int ak = a[k]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
663 |
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
|
664 |
a[k] = a[less]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
665 |
a[less] = ak; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
666 |
++less; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
667 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
668 |
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
|
669 |
while (a[great] > pivot) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
670 |
--great; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
671 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
672 |
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
|
673 |
a[k] = a[less]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
674 |
a[less] = a[great]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
675 |
++less; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
676 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
677 |
else { // a[great] == pivot |
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 |
* 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
|
680 |
* 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
|
681 |
* 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
|
682 |
* 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
|
683 |
* 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
|
684 |
* 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
|
685 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
686 |
a[k] = pivot; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
687 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
688 |
a[great] = ak; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
689 |
--great; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
690 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
691 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
692 |
|
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 |
* 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
|
695 |
* 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
|
696 |
* and, therefore, already sorted. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
697 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
698 |
SortingIntTestJMH.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
|
699 |
SortingIntTestJMH.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
|
700 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
701 |
} |
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 |
private static void swap(int[] arr, int i, int j) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
704 |
int tmp = arr[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
705 |
arr[i] = arr[j]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
706 |
arr[j] = tmp; |
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 |
} |