Re #5: Snappy allocation optimisation - create and reuse largest possible hash table (array of shorts) with 16k entries.

This commit is contained in:
tomasz.kowalczewski committed 2021-02-07 16:53:00 +01:00
1 parent 93d189237b
commit f11128631f
1 file changed
+8 -7
@@ -17,6 +17,8 @@ package pl.tkowalcz.tjahzi.http;
import io.netty.buffer.ByteBuf;
import java.util.Arrays;
/**
* Uncompresses an input {@link ByteBuf} encoded with Snappy compression into an
* output {@link ByteBuf}.
@@ -25,7 +27,6 @@ import io.netty.buffer.ByteBuf;
*/
public final class Snappy {
private static final int MAX_HT_SIZE = 1 << 14;
private static final int MIN_COMPRESSIBLE_BYTES = 15;
// constants for the tag types
@@ -33,6 +34,9 @@ public final class Snappy {
private static final int COPY_1_BYTE_OFFSET = 1;
private static final int COPY_2_BYTE_OFFSET = 2;
private static final int MAX_HT_SIZE = 1 << 14;
private final short[] hashTable = new short[MAX_HT_SIZE];
public void encode(final ByteBuf in, final ByteBuf out, final int length) {
// Write the preamble length to the output buffer
for (int i = 0; ; i++) {
@@ -144,12 +148,9 @@ public final class Snappy {
* @param inputSize The size of our input, ie. the number of bytes we need to encode
* @return An appropriately sized empty hashtable
*/
private static short[] getHashTable(int inputSize) {
int htSize = 256;
while (htSize < MAX_HT_SIZE && htSize < inputSize) {
htSize <<= 1;
}
return new short[htSize];
private short[] getHashTable(int inputSize) {
Arrays.fill(hashTable, (short) 0);
return hashTable;
}
/**