-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved merge conflict in changelog
- Loading branch information
Showing
44 changed files
with
673 additions
and
536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/AdvanceBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.benchmark.jmh; | ||
|
||
import java.util.Arrays; | ||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.lucene.search.DocIdSetIterator; | ||
import org.apache.lucene.util.VectorUtil; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.CompilerControl; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 5, time = 1) | ||
@Measurement(iterations = 5, time = 1) | ||
@Fork( | ||
value = 3, | ||
jvmArgsAppend = { | ||
"-Xmx1g", | ||
"-Xms1g", | ||
"-XX:+AlwaysPreTouch", | ||
"--add-modules", | ||
"jdk.incubator.vector" | ||
}) | ||
public class AdvanceBenchmark { | ||
|
||
private final long[] values = new long[129]; | ||
private final int[] startIndexes = new int[1_000]; | ||
private final long[] targets = new long[startIndexes.length]; | ||
|
||
@Setup(Level.Trial) | ||
public void setup() throws Exception { | ||
for (int i = 0; i < 128; ++i) { | ||
values[i] = i; | ||
} | ||
values[128] = DocIdSetIterator.NO_MORE_DOCS; | ||
Random r = new Random(0); | ||
for (int i = 0; i < startIndexes.length; ++i) { | ||
startIndexes[i] = r.nextInt(64); | ||
targets[i] = startIndexes[i] + 1 + r.nextInt(1 << r.nextInt(7)); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void binarySearch() { | ||
for (int i = 0; i < startIndexes.length; ++i) { | ||
binarySearch(values, targets[i], startIndexes[i]); | ||
} | ||
} | ||
|
||
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | ||
private static int binarySearch(long[] values, long target, int startIndex) { | ||
// Standard binary search | ||
int i = Arrays.binarySearch(values, startIndex, values.length, target); | ||
if (i < 0) { | ||
i = -1 - i; | ||
} | ||
return i; | ||
} | ||
|
||
@Benchmark | ||
public void inlinedBranchlessBinarySearch() { | ||
for (int i = 0; i < targets.length; ++i) { | ||
inlinedBranchlessBinarySearch(values, targets[i]); | ||
} | ||
} | ||
|
||
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | ||
private static int inlinedBranchlessBinarySearch(long[] values, long target) { | ||
// This compiles to cmov instructions. | ||
int start = 0; | ||
|
||
if (values[63] < target) { | ||
start += 64; | ||
} | ||
if (values[start + 31] < target) { | ||
start += 32; | ||
} | ||
if (values[start + 15] < target) { | ||
start += 16; | ||
} | ||
if (values[start + 7] < target) { | ||
start += 8; | ||
} | ||
if (values[start + 3] < target) { | ||
start += 4; | ||
} | ||
if (values[start + 1] < target) { | ||
start += 2; | ||
} | ||
if (values[start] < target) { | ||
start += 1; | ||
} | ||
|
||
return start; | ||
} | ||
|
||
@Benchmark | ||
public void linearSearch() { | ||
for (int i = 0; i < startIndexes.length; ++i) { | ||
linearSearch(values, targets[i], startIndexes[i]); | ||
} | ||
} | ||
|
||
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | ||
private static int linearSearch(long[] values, long target, int startIndex) { | ||
// Naive linear search. | ||
for (int i = startIndex; i < values.length; ++i) { | ||
if (values[i] >= target) { | ||
return i; | ||
} | ||
} | ||
return values.length; | ||
} | ||
|
||
@Benchmark | ||
public void vectorUtilSearch() { | ||
for (int i = 0; i < startIndexes.length; ++i) { | ||
VectorUtil.findNextGEQ(values, 128, targets[i], startIndexes[i]); | ||
} | ||
} | ||
|
||
@CompilerControl(CompilerControl.Mode.DONT_INLINE) | ||
private static int vectorUtilSearch(long[] values, long target, int startIndex) { | ||
return VectorUtil.findNextGEQ(values, 128, target, startIndex); | ||
} | ||
|
||
private static void assertEquals(int expected, int actual) { | ||
if (expected != actual) { | ||
throw new AssertionError("Expected: " + expected + ", got " + actual); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
// For testing purposes | ||
long[] values = new long[129]; | ||
for (int i = 0; i < 128; ++i) { | ||
values[i] = i; | ||
} | ||
values[128] = DocIdSetIterator.NO_MORE_DOCS; | ||
for (int start = 0; start < 128; ++start) { | ||
for (int targetIndex = start; targetIndex < 128; ++targetIndex) { | ||
int actualIndex = binarySearch(values, values[targetIndex], start); | ||
assertEquals(targetIndex, actualIndex); | ||
actualIndex = inlinedBranchlessBinarySearch(values, values[targetIndex]); | ||
assertEquals(targetIndex, actualIndex); | ||
actualIndex = linearSearch(values, values[targetIndex], start); | ||
assertEquals(targetIndex, actualIndex); | ||
actualIndex = vectorUtilSearch(values, values[targetIndex], start); | ||
assertEquals(targetIndex, actualIndex); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.