Permalink
Browse files

squeeze a little bit of extra performance out of deflate decompressor

  • Loading branch information...
UnknownShadow200 committed Oct 12, 2017
1 parent 41c2320 commit 0dca6357f8e2af689d268870e5035f8b9006f050
Showing with 8 additions and 6 deletions.
  1. +7 −5 src/Client/Deflate.c
  2. +1 −1 src/Client/Deflate.h
View
@@ -218,10 +218,12 @@ void Huffman_Build(HuffmanTable* table, UInt8* bitLens, Int32 count) {
table->FirstOffsets[i] = (UInt16)offset;
offset += bl_count[i];
/* Last codeword is actually: code + (bl_count[i] - 1) */
/* When decoding we peform < against this value though, so we need to add 1 here */
if (bl_count[i]) {
table->EndCodewords[i] = code + (bl_count[i] - 1);
table->EndCodewords[i] = (UInt16)(code + bl_count[i]);
} else {
table->EndCodewords[i] = -1;
table->EndCodewords[i] = 0;
}
}
@@ -271,13 +273,13 @@ Int32 Huffman_Decode(DeflateState* state, HuffmanTable* table) {
}
/* Slow, bit by bit lookup */
Int32 codeword = 0;
UInt32 codeword = 0;
UInt32 i, j;
for (i = 1, j = 0; i < DEFLATE_MAX_BITS; i++, j++) {
if (state->NumBits < i) return -1;
codeword = (codeword << 1) | ((state->Bits >> j) & 1);
if (codeword >= table->FirstCodewords[i] && codeword <= table->EndCodewords[i]) {
if (codeword < table->EndCodewords[i]) {
Int32 offset = table->FirstOffsets[i] + (codeword - table->FirstCodewords[i]);
DEFLATE_CONSUME_BITS(state, i);
return table->Values[offset];
@@ -304,7 +306,7 @@ Int32 Huffman_Unsafe_Decode(DeflateState* state, HuffmanTable* table) {
for (i = DEFLATE_ZFAST_BITS + 1, j = DEFLATE_ZFAST_BITS; i < DEFLATE_MAX_BITS; i++, j++) {
codeword = (codeword << 1) | ((state->Bits >> j) & 1);
if (codeword >= table->FirstCodewords[i] && (Int32)codeword <= table->EndCodewords[i]) {
if (codeword < table->EndCodewords[i]) {
Int32 offset = table->FirstOffsets[i] + (codeword - table->FirstCodewords[i]);
DEFLATE_CONSUME_BITS(state, i);
return table->Values[offset];
View
@@ -48,7 +48,7 @@ void ZLibHeader_Read(Stream* s, ZLibHeader* header);
typedef struct HuffmanTable_ {
UInt16 FirstCodewords[DEFLATE_MAX_BITS]; /* Starting codeword for each bit length */
Int32 EndCodewords[DEFLATE_MAX_BITS]; /* End codeword for each bit length. -1 is ignored. */
UInt16 EndCodewords[DEFLATE_MAX_BITS]; /* (Last codeword + 1) for each bit length. 0 is ignored. */
UInt16 FirstOffsets[DEFLATE_MAX_BITS]; /* Base offset into Values for codewords of each bit length. */
UInt16 Values[DEFLATE_MAX_LITS]; /* Values/Symbols list */
Int16 Fast[1 << DEFLATE_ZFAST_BITS]; /* Fast lookup table for huffman codes */

0 comments on commit 0dca635

Please sign in to comment.