Skip to content

Commit

Permalink
Switch?!
Browse files Browse the repository at this point in the history
  • Loading branch information
nik9000 committed Sep 15, 2020
1 parent c65a0b1 commit 5cd3531
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package org.elasticsearch.benchmark.search.aggregations.bucket.terms;

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.DelayableWriteable;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.search.DocValueFormat;
Expand All @@ -39,6 +42,7 @@
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -89,4 +93,17 @@ private StringTerms newTerms(boolean withNested) {
public DelayableWriteable<InternalAggregations> serialize() {
return results.asSerialized(InternalAggregations::readFrom, REGISTRY);
}

@Benchmark
public BytesReference serializeVint() throws IOException {
try (BytesStreamOutput buffer = new BytesStreamOutput()) {
buffer.setVersion(Version.CURRENT);
for (int i = 0; i < 1000000; i++) {
buffer.writeVInt(i * 31);
buffer.reset();
}
return buffer.bytes();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -226,53 +226,77 @@ public void writeVInt(int i) throws IOException {
* together this saves quite a bit of time compared to a naive
* implementation.
*/
if (i < 0x7f) {
if (i >= 0) {
switch (Integer.numberOfLeadingZeros(i)) {
case 32:
case 31:
case 30:
case 29:
case 28:
case 27:
case 26:
case 25:
writeByte((byte) i);
return;
}
byte[] buffer = scratch.get();
buffer[0] = (byte) (i & 0x7f | 0x80);
buffer[1] = (byte) ((i >>> 7) & 0x7f | 0x80);
buffer[2] = (byte) ((i >>> 14) & 0x7f | 0x80);
buffer[3] = (byte) ((i >>> 21) & 0x7f | 0x80);
buffer[4] = (byte) (i >>> 28);
assert buffer[4] <= 0x7f;
writeBytes(buffer, 0, 5);
return;
}
byte[] buffer = scratch.get();
if (i < 0x3fff) {
buffer[0] = (byte) (i & 0x7f | 0x80);
buffer[1] = (byte) (i >>> 7);
assert buffer[1] <= 0x7f;
writeBytes(buffer, 0, 2);
return;
}
if (i < 0x1f_ffff) {
buffer[0] = (byte) (i & 0x7f | 0x80);
buffer[1] = (byte) ((i >>> 7) & 0x7f | 0x80);
buffer[2] = (byte) (i >>> 14);
assert buffer[2] <= 0x7f;
writeBytes(buffer, 0, 3);
return;
}
if (i < 0x0fff_ffff) {
buffer[0] = (byte) (i & 0x7f | 0x80);
buffer[1] = (byte) ((i >>> 7) & 0x7f | 0x80);
buffer[2] = (byte) ((i >>> 14) & 0x7f | 0x80);
buffer[3] = (byte) (i >>> 21);
assert buffer[3] <= 0x7f;
writeBytes(buffer, 0, 4);
return;
case 24:
case 23:
case 22:
case 21:
case 20:
case 19:
case 18:
byte[] buffer = scratch.get();
buffer[0] = (byte) (i & 0x7f | 0x80);
buffer[1] = (byte) (i >>> 7);
assert buffer[1] <= 0x7f;
writeBytes(buffer, 0, 2);
return;
case 17:
case 16:
case 15:
case 14:
case 13:
case 12:
case 11:
buffer = scratch.get();
buffer[0] = (byte) (i & 0x7f | 0x80);
buffer[1] = (byte) ((i >>> 7) & 0x7f | 0x80);
buffer[2] = (byte) (i >>> 14);
assert buffer[2] <= 0x7f;
writeBytes(buffer, 0, 3);
return;
case 10:
case 9:
case 8:
case 7:
case 6:
case 5:
case 4:
buffer = scratch.get();
buffer[0] = (byte) (i & 0x7f | 0x80);
buffer[1] = (byte) ((i >>> 7) & 0x7f | 0x80);
buffer[2] = (byte) ((i >>> 14) & 0x7f | 0x80);
buffer[3] = (byte) (i >>> 21);
assert buffer[3] <= 0x7f;
writeBytes(buffer, 0, 4);
return;
case 3:
case 2:
case 1:
case 0:
buffer = scratch.get();
buffer[0] = (byte) (i & 0x7f | 0x80);
buffer[1] = (byte) ((i >>> 7) & 0x7f | 0x80);
buffer[2] = (byte) ((i >>> 14) & 0x7f | 0x80);
buffer[3] = (byte) ((i >>> 21) & 0x7f | 0x80);
buffer[4] = (byte) (i >>> 28);
assert buffer[4] <= 0x7f;
writeBytes(buffer, 0, 5);
return;
default:
throw new UnsupportedOperationException(
"Can't encode [" + i + "]. Missing case for [" + Integer.numberOfLeadingZeros(i) + "]?"
);
}
buffer[0] = (byte) ((i & 0x7f) | 0x80);
buffer[1] = (byte) ((i >>> 7) & 0x7f | 0x80);
buffer[2] = (byte) ((i >>> 14) & 0x7f | 0x80);
buffer[3] = (byte) ((i >>> 21) & 0x7f | 0x80);
buffer[4] = (byte) (i >>> 28);
assert buffer[4] <= 0x7f;
writeBytes(buffer, 0, 5);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.common.io.stream;

import com.carrotsearch.randomizedtesting.annotations.Repeat;

import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Constants;
Expand Down Expand Up @@ -823,12 +825,11 @@ public void testReadNegativeArraySize() throws IOException {
}
}

@Repeat(iterations=1000)
public void testVInt() throws IOException {
final int value = randomInt();
BytesStreamOutput output = new BytesStreamOutput();
output.writeVInt(value);
StreamInput input = output.bytes().streamInput();
assertEquals(value, input.readVInt());

BytesStreamOutput simple = new BytesStreamOutput();
int i = value;
Expand All @@ -838,6 +839,9 @@ public void testVInt() throws IOException {
}
simple.writeByte((byte) i);
assertEquals(simple.bytes().toBytesRef().toString(), output.bytes().toBytesRef().toString());

StreamInput input = output.bytes().streamInput();
assertEquals(value, input.readVInt());
}

public void testVLong() throws IOException {
Expand Down

0 comments on commit 5cd3531

Please sign in to comment.