Do proper bounds-checking in HpackHuffmanDecoder to reduce overhead of IndexOutOfBoundsException creation

Motivation:

HpackHuffmanDecoder.Decoder did not do any bound-checking but just catched IndexOutOfBoundsException to detect if the array needs to grow. This can be very expensive because of fillInStackTrace()

Modifications:

Add proper bounds checking and grow the array if needed without catching IndexOutOfBoundsException.

Result:

Less overhead if the array needs to grow.
This commit is contained in:
Norman Maurer 2017-06-11 12:47:56 +02:00
parent f00638af52
commit 3f0085c267

View File

@ -230,16 +230,13 @@ final class HpackHuffmanDecoder {
} }
private void append(int i) { private void append(int i) {
try { if (bytes.length == index) {
bytes[index] = (byte) i;
} catch (IndexOutOfBoundsException ignore) {
// Always just expand by INITIAL_SIZE // Always just expand by INITIAL_SIZE
byte[] newBytes = new byte[bytes.length + initialCapacity]; byte[] newBytes = new byte[bytes.length + initialCapacity];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length); System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
bytes = newBytes; bytes = newBytes;
bytes[index] = (byte) i;
} }
index++; bytes[index++] = (byte) i;
} }
} }
} }