From d9bf974023d85f68b32b8c36052f4926f0478182 Mon Sep 17 00:00:00 2001 From: "yann.collet.73@gmail.com" Date: Tue, 20 Sep 2011 10:01:23 +0000 Subject: [PATCH] Small compression speed improvement git-svn-id: https://lz4.googlecode.com/svn/trunk@24 650e7d94-2a16-8b24-b05c-7c0b3f6821cd --- lz4.c | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/lz4.c b/lz4.c index 534b42b..54895a6 100644 --- a/lz4.c +++ b/lz4.c @@ -67,6 +67,7 @@ //************************************** #define MINMATCH 4 #define SKIPSTRENGTH 6 +#define HEAPLIMIT 13 #define MAXD_LOG 16 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1) @@ -106,8 +107,12 @@ int LZ4_compressCtx(void** ctx, char* dest, int isize) { +#if HASH_LOG>HEAPLIMIT struct refTables *srt = (struct refTables *) (*ctx); const BYTE** HashTable; +#else + const BYTE* HashTable[HASHTABLESIZE] = {0}; +#endif const BYTE* ip = (BYTE*) source; const BYTE* anchor = ip; @@ -124,6 +129,7 @@ int LZ4_compressCtx(void** ctx, // Init +#if HASH_LOG>HEAPLIMIT if (*ctx == NULL) { srt = (struct refTables *) malloc ( sizeof(struct refTables) ); @@ -131,6 +137,7 @@ int LZ4_compressCtx(void** ctx, } HashTable = srt->hashTable; memset((void*)HashTable, 0, sizeof(srt->hashTable)); +#endif // First Byte @@ -138,26 +145,26 @@ int LZ4_compressCtx(void** ctx, ip++; forwardH = HASH_VALUE(ip); // Main Loop - for ( ; ; ) - { - int segmentSize = (1U << skipStrength) + 3; - const BYTE* forwardIp = ip; - const BYTE* ref; - + for ( ; ; ) + { + int segmentSize = (1U << skipStrength) + 3; + const BYTE* forwardIp = ip; + const BYTE* ref; + // Find a match - do { - U32 h = forwardH; - int skipped = segmentSize++ >> skipStrength; - ip = forwardIp; - forwardIp = ip + skipped; - - if (forwardIp > ilimit) { goto _last_literals; } + do { + U32 h = forwardH; + int skipped = segmentSize++ >> skipStrength; + ip = forwardIp; + forwardIp = ip + skipped; + + if (forwardIp > ilimit) { goto _last_literals; } forwardH = HASH_VALUE(forwardIp); ref = HashTable[h]; HashTable[h] = ip; - - } while ((ref < ip - MAX_DISTANCE) || (*(U32*)ref != *(U32*)ip)); + + } while ((ref < ip - MAX_DISTANCE) || (*(U32*)ref != *(U32*)ip)); // Catch up while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; } @@ -230,15 +237,19 @@ int LZ4_compress(char* source, char* dest, int isize) { +#if HASH_LOG>HEAPLIMIT void* ctx = malloc(sizeof(struct refTables)); int result = LZ4_compressCtx(&ctx, source, dest, isize); free(ctx); - return result; +#else + return LZ4_compressCtx(NULL, source, dest, isize); +#endif } + //**************************** // Decompression CODE //**************************** -- 2.7.4