Skip to content

Commit

Permalink
Simplify the common (literal) case in LZS compression
Browse files Browse the repository at this point in the history
If we're only adding one byte then don't use the loop, don't recalculate
the hash we already have.

This gives roughly a 2% performance improvement

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Jan 7, 2015
1 parent 44476f1 commit c7042d5
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lzs.c
Expand Up @@ -222,7 +222,11 @@ int lzs_compress(unsigned char *dst, int dstlen, const unsigned char *src, int s
}
hofs = hash_chain[hofs & (MAX_HISTORY - 1)];
}
if (longest_match_len) {
if (!longest_match_len) {
PUT_BITS(9, src[inpos]);
hash_chain[inpos & (MAX_HISTORY - 1)] = hash_table[hash];
hash_table[hash] = inpos++;
} else {
/* Output offset, as 7-bit or 11-bit as appropriate */
int offset = inpos - longest_match_ofs;
int length = longest_match_len;
Expand All @@ -247,22 +251,20 @@ int lzs_compress(unsigned char *dst, int dstlen, const unsigned char *src, int s
}
PUT_BITS(4, length);
}
} else {
PUT_BITS(9, src[inpos]);
longest_match_len = 1;
}

/* Add byte(s) to the hash tables unless we're done */
if (inpos + longest_match_len >= srclen - 1) {
inpos += longest_match_len;
break;
}
/* Add byte(s) to the hash tables unless we're done */
if (inpos + longest_match_len >= srclen - 1) {
inpos += longest_match_len;
break;
}

while (longest_match_len--) {
hash = HASH(src + inpos);
hash_chain[inpos & (MAX_HISTORY - 1)] = hash_table[hash];
hash_table[hash] = inpos++;
while (longest_match_len--) {
hash = HASH(src + inpos);
hash_chain[inpos & (MAX_HISTORY - 1)] = hash_table[hash];
hash_table[hash] = inpos++;
}
}

}
if (inpos < srclen)
PUT_BITS(9, src[inpos]);
Expand Down

0 comments on commit c7042d5

Please sign in to comment.