Skip to content

Commit

Permalink
Update hash table for first byte at start of LZS compression loop
Browse files Browse the repository at this point in the history
We're always going to need to do it, so do it while we have everything
to hand. Roughly 19% speed improvement on the random-biased corpus;
perhaps a little less if there are more matches and we end up having
to loop anyway.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Jan 8, 2015
1 parent 93de65d commit 387c7ab
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lzs.c
Expand Up @@ -218,6 +218,9 @@ int lzs_compress(unsigned char *dst, int dstlen, const unsigned char *src, int s
hash = HASH(src + inpos);
hofs = hash_table[hash];

hash_chain[inpos & (MAX_HISTORY - 1)] = hofs;
hash_table[hash] = inpos;

longest_match_len = 0;

while (hofs != INVALID_OFS && hofs + MAX_HISTORY > inpos) {
Expand All @@ -230,8 +233,7 @@ int lzs_compress(unsigned char *dst, int dstlen, const unsigned char *src, int s
}
if (!longest_match_len) {
PUT_BITS(9, src[inpos]);
hash_chain[inpos & (MAX_HISTORY - 1)] = hash_table[hash];
hash_table[hash] = inpos++;
inpos++;
} else {
/* Output offset, as 7-bit or 11-bit as appropriate */
int offset = inpos - longest_match_ofs;
Expand Down Expand Up @@ -259,13 +261,15 @@ int lzs_compress(unsigned char *dst, int dstlen, const unsigned char *src, int s
PUT_BITS(4, length);
}

/* Add byte(s) to the hash tables unless we're done */
/* If we're already done, don't bother updating the hash tables. */
if (inpos + longest_match_len >= srclen - 1) {
inpos += longest_match_len;
break;
}

while (longest_match_len--) {
/* We already added the first byte to the hash tables. Add the rest. */
inpos++;
while (--longest_match_len) {
hash = HASH(src + inpos);
hash_chain[inpos & (MAX_HISTORY - 1)] = hash_table[hash];
hash_table[hash] = inpos++;
Expand Down

0 comments on commit 387c7ab

Please sign in to comment.