-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Optimize BytesArray::indexOf, which is used heavily in ndjson parsing #135087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+374
−84
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5655ea9
Optimize BytesArray::indexOf, which is used heavily in ndjson parsing
ChrisHegarty e376434
itr
ChrisHegarty e71b43b
Update docs/changelog/135087.yaml
ChrisHegarty 6c70e7d
itr
ChrisHegarty c0632fb
Merge branch 'main' into indexOf_vec
ChrisHegarty 31f2b7d
changelog
ChrisHegarty 233f079
Merge branch 'main' into indexOf_vec
ChrisHegarty 72ec4b6
small benchmark sizes
ChrisHegarty 13095ab
Merge branch 'main' into indexOf_vec
ChrisHegarty f7526c4
use ByteArrayUtils.indexOf for tail
ChrisHegarty d91c0f0
Merge branch 'main' into indexOf_vec
ChrisHegarty 396632d
Merge branch 'main' into indexOf_vec
ChrisHegarty 482eee8
[CI] Update transport version definitions
6399b2b
Revert "[CI] Update transport version definitions"
ChrisHegarty 1b8f7c8
[CI] Update transport version definitions
d8b0ea0
Merge branch 'main' into indexOf_vec
ChrisHegarty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
benchmarks/src/main/java/org/elasticsearch/benchmark/bytes/BytesArrayIndexOfBenchmark.java
This file contains hidden or 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,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
package org.elasticsearch.benchmark.bytes; | ||
|
||
import org.elasticsearch.common.bytes.BytesArray; | ||
import org.elasticsearch.common.logging.LogConfigurator; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Warmup(iterations = 4, time = 1) | ||
@Measurement(iterations = 5, time = 1) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Thread) | ||
@Fork(value = 1) | ||
public class BytesArrayIndexOfBenchmark { | ||
|
||
static { | ||
LogConfigurator.configureESLogging(); // native access requires logging to be initialized | ||
} | ||
|
||
static final byte MARKER = (byte) '\n'; | ||
|
||
@Param(value = { "64", "127", "128", "4096", "16384", "65536", "1048576" }) | ||
public int size; | ||
|
||
BytesArray bytesArray; | ||
|
||
@Setup | ||
public void setup() { | ||
byte[] bytes = new byte[size]; | ||
bytes[bytes.length - 1] = MARKER; | ||
bytesArray = new BytesArray(bytes, 0, bytes.length); | ||
} | ||
|
||
@Benchmark | ||
public int indexOf() { | ||
return bytesArray.indexOf(MARKER, 0); | ||
} | ||
|
||
@Benchmark | ||
@Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) | ||
public int indexOfPanama() { | ||
return bytesArray.indexOf(MARKER, 0); | ||
} | ||
|
||
@Benchmark | ||
public int withOffsetIndexOf() { | ||
return bytesArray.indexOf(MARKER, 1); | ||
} | ||
|
||
@Benchmark | ||
@Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) | ||
public int withOffsetIndexPanama() { | ||
return bytesArray.indexOf(MARKER, 1); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...arks/src/test/java/org/elasticsearch/benchmark/bytes/BytesArrayIndexOfBenchmarkTests.java
This file contains hidden or 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,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.benchmark.bytes; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.openjdk.jmh.annotations.Param; | ||
|
||
import java.util.Arrays; | ||
|
||
public class BytesArrayIndexOfBenchmarkTests extends ESTestCase { | ||
|
||
final int size; | ||
|
||
public BytesArrayIndexOfBenchmarkTests(int size) { | ||
this.size = size; | ||
} | ||
|
||
public void testIndexOf() { | ||
var bench = new BytesArrayIndexOfBenchmark(); | ||
bench.size = size; | ||
bench.setup(); | ||
assertEquals(size - 1, bench.indexOf()); | ||
assertEquals(size - 1, bench.indexOfPanama()); | ||
assertEquals(size - 1, bench.withOffsetIndexOf()); | ||
assertEquals(size - 1, bench.withOffsetIndexPanama()); | ||
} | ||
|
||
@ParametersFactory | ||
public static Iterable<Object[]> parametersFactory() { | ||
try { | ||
var params = BytesArrayIndexOfBenchmark.class.getField("size").getAnnotationsByType(Param.class)[0].value(); | ||
return () -> Arrays.stream(params).map(Integer::parseInt).map(i -> new Object[] { i }).iterator(); | ||
} catch (NoSuchFieldException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
} |
This file contains hidden or 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,5 @@ | ||
pr: 135087 | ||
summary: "Optimize `BytesArray::indexOf,` which is used heavily in ndjson parsing" | ||
area: "Performance" | ||
type: feature | ||
issues: [] |
This file contains hidden or 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
110 changes: 110 additions & 0 deletions
110
...imdvec/src/main/java/org/elasticsearch/simdvec/internal/vectorization/ByteArrayUtils.java
This file contains hidden or 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,110 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.simdvec.internal.vectorization; | ||
|
||
import static org.apache.lucene.util.BitUtil.VH_LE_LONG; | ||
|
||
/** Byte array utilities. */ | ||
final class ByteArrayUtils { | ||
|
||
/** | ||
* Implementation of {@link ESVectorUtilSupport#indexOf(byte[], int, int, byte)} using fast | ||
* SWAR (SIMD Within A Register) loop. | ||
*/ | ||
static int indexOf(final byte[] bytes, final int offset, final int len, final byte marker) { | ||
final int end = offset + len; | ||
int i = offset; | ||
|
||
// First, try to find the marker in the first few bytes, so we can enter the faster 8-byte aligned loop below. | ||
// The idea for this logic is taken from Netty's io.netty.buffer.ByteBufUtil.firstIndexOf and optimized for little endian hardware. | ||
// See e.g. https://richardstartin.github.io/posts/finding-bytes for the idea behind this optimization. | ||
final int byteCount = len & 7; | ||
ChrisHegarty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (byteCount > 0) { | ||
final int index = unrolledFirstIndexOf(bytes, i, byteCount, marker); | ||
if (index != -1) { | ||
return index - offset; | ||
} | ||
i += byteCount; | ||
if (i == end) { | ||
return -1; | ||
} | ||
} | ||
final int longCount = len >>> 3; | ||
// faster SWAR (SIMD Within A Register) loop | ||
final long pattern = compilePattern(marker); | ||
for (int j = 0; j < longCount; j++) { | ||
int index = findInLong(readLongLE(bytes, i), pattern); | ||
if (index < Long.BYTES) { | ||
return i + index - offset; | ||
} | ||
i += Long.BYTES; | ||
} | ||
return -1; | ||
} | ||
|
||
private static long readLongLE(byte[] arr, int offset) { | ||
return (long) VH_LE_LONG.get(arr, offset); | ||
} | ||
|
||
private static long compilePattern(byte byteToFind) { | ||
return (byteToFind & 0xFFL) * 0x101010101010101L; | ||
} | ||
|
||
private static int findInLong(long word, long pattern) { | ||
long input = word ^ pattern; | ||
long tmp = (input & 0x7F7F7F7F7F7F7F7FL) + 0x7F7F7F7F7F7F7F7FL; | ||
tmp = ~(tmp | input | 0x7F7F7F7F7F7F7F7FL); | ||
final int binaryPosition = Long.numberOfTrailingZeros(tmp); | ||
return binaryPosition >>> 3; | ||
} | ||
|
||
private static int unrolledFirstIndexOf(byte[] buffer, int fromIndex, int byteCount, byte value) { | ||
if (buffer[fromIndex] == value) { | ||
return fromIndex; | ||
} | ||
if (byteCount == 1) { | ||
return -1; | ||
} | ||
if (buffer[fromIndex + 1] == value) { | ||
return fromIndex + 1; | ||
} | ||
if (byteCount == 2) { | ||
return -1; | ||
} | ||
if (buffer[fromIndex + 2] == value) { | ||
return fromIndex + 2; | ||
} | ||
if (byteCount == 3) { | ||
return -1; | ||
} | ||
if (buffer[fromIndex + 3] == value) { | ||
return fromIndex + 3; | ||
} | ||
if (byteCount == 4) { | ||
return -1; | ||
} | ||
if (buffer[fromIndex + 4] == value) { | ||
return fromIndex + 4; | ||
} | ||
if (byteCount == 5) { | ||
return -1; | ||
} | ||
if (buffer[fromIndex + 5] == value) { | ||
return fromIndex + 5; | ||
} | ||
if (byteCount == 6) { | ||
return -1; | ||
} | ||
if (buffer[fromIndex + 6] == value) { | ||
return fromIndex + 6; | ||
} | ||
return -1; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -1122,4 +1122,26 @@ private void transposeHalfByte128(int[] q, byte[] quantQueryByte) { | |
quantQueryByte[index + quantQueryByte.length / 2] = (byte) upperMiddleByte; | ||
quantQueryByte[index + 3 * quantQueryByte.length / 4] = (byte) upperByte; | ||
} | ||
|
||
@Override | ||
public int indexOf(final byte[] bytes, final int offset, final int length, final byte marker) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I don't think we need to keep this in main21, as 21 is the minimum version we support now. We need to apply the mrjar plugin for the preview bit handling, but no need for a different sourceset I think |
||
final ByteVector markerVector = ByteVector.broadcast(ByteVector.SPECIES_PREFERRED, marker); | ||
final int loopBound = ByteVector.SPECIES_PREFERRED.loopBound(length); | ||
for (int i = 0; i < loopBound; i += ByteVector.SPECIES_PREFERRED.length()) { | ||
ByteVector chunk = ByteVector.fromArray(ByteVector.SPECIES_PREFERRED, bytes, offset + i); | ||
VectorMask<Byte> mask = chunk.eq(markerVector); | ||
if (mask.anyTrue()) { | ||
return i + mask.firstTrue(); | ||
} | ||
} | ||
// tail | ||
if (loopBound < length) { | ||
int remaining = length - loopBound; | ||
int tail = ByteArrayUtils.indexOf(bytes, offset + loopBound, remaining, marker); | ||
if (tail >= 0) { | ||
return loopBound + tail; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.