Skip to content

Commit

Permalink
Remove vInt-like encoding in ByteBlockPool and BytesRefHash and switc…
Browse files Browse the repository at this point in the history
…h to native byte order for the length
  • Loading branch information
uschindler committed May 13, 2022
1 parent 36de2bb commit c1b626c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 47 deletions.
11 changes: 2 additions & 9 deletions lucene/core/src/java/org/apache/lucene/util/ByteBlockPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,8 @@ void setBytesRef(BytesRefBuilder builder, BytesRef result, long offset, int leng
public void setBytesRef(BytesRef term, int textStart) {
final byte[] bytes = term.bytes = buffers[textStart >> BYTE_BLOCK_SHIFT];
int pos = textStart & BYTE_BLOCK_MASK;
if ((bytes[pos] & 0x80) == 0) {
// length is 1 byte
term.length = bytes[pos];
term.offset = pos + 1;
} else {
// length is 2 bytes
term.length = ((short) BitUtil.VH_BE_SHORT.get(bytes, pos)) & 0x7FFF;
term.offset = pos + 2;
}
term.length = ((short) BitUtil.VH_NATIVE_SHORT.get(bytes, pos)) & 0x7FFF;
term.offset = pos + 2;
assert term.length >= 0;
}

Expand Down
46 changes: 8 additions & 38 deletions lucene/core/src/java/org/apache/lucene/util/BytesRefHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,8 @@ private boolean equals(int id, BytesRef b) {
final int textStart = bytesStart[id];
final byte[] bytes = pool.buffers[textStart >> BYTE_BLOCK_SHIFT];
int pos = textStart & BYTE_BLOCK_MASK;
final int length;
final int offset;
if ((bytes[pos] & 0x80) == 0) {
// length is 1 byte
length = bytes[pos];
offset = pos + 1;
} else {
// length is 2 bytes
length = ((short) BitUtil.VH_BE_SHORT.get(bytes, pos)) & 0x7FFF;
offset = pos + 2;
}
final int length = ((short) BitUtil.VH_NATIVE_SHORT.get(bytes, pos)) & 0x7FFF;
final int offset = pos + 2;
return Arrays.equals(bytes, offset, offset + length, b.bytes, b.offset, b.offset + b.length);
}

Expand Down Expand Up @@ -270,22 +261,10 @@ public int add(BytesRef bytes) {

bytesStart[e] = bufferUpto + pool.byteOffset;

// We first encode the length, followed by the
// bytes. Length is encoded as vInt, but will consume
// 1 or 2 bytes at most (we reject too-long terms,
// above).
if (length < 128) {
// 1 byte to store length
buffer[bufferUpto] = (byte) length;
pool.byteUpto += length + 1;
assert length >= 0 : "Length must be positive: " + length;
System.arraycopy(bytes.bytes, bytes.offset, buffer, bufferUpto + 1, length);
} else {
// 2 byte to store length
BitUtil.VH_BE_SHORT.set(buffer, bufferUpto, (short) (length | 0x8000));
pool.byteUpto += length + 2;
System.arraycopy(bytes.bytes, bytes.offset, buffer, bufferUpto + 2, length);
}
BitUtil.VH_NATIVE_SHORT.set(buffer, bufferUpto, (short) (length | 0x8000));
pool.byteUpto += length + 2;
System.arraycopy(bytes.bytes, bytes.offset, buffer, bufferUpto + 2, length);

assert ids[hashPos] == -1;
ids[hashPos] = e;

Expand Down Expand Up @@ -384,17 +363,8 @@ private void rehash(final int newSize, boolean hashOnData) {
final int off = bytesStart[e0];
final int start = off & BYTE_BLOCK_MASK;
final byte[] bytes = pool.buffers[off >> BYTE_BLOCK_SHIFT];
final int len;
int pos;
if ((bytes[start] & 0x80) == 0) {
// length is 1 byte
len = bytes[start];
pos = start + 1;
} else {
len = ((short) BitUtil.VH_BE_SHORT.get(bytes, start)) & 0x7FFF;
pos = start + 2;
}
code = doHash(bytes, pos, len);
final int len = ((short) BitUtil.VH_NATIVE_SHORT.get(bytes, start)) & 0x7FFF;
code = doHash(bytes, start + 2, len);
} else {
code = bytesStart[e0];
}
Expand Down

0 comments on commit c1b626c

Please sign in to comment.