Small compression speed improvement
authoryann.collet.73@gmail.com <yann.collet.73@gmail.com@650e7d94-2a16-8b24-b05c-7c0b3f6821cd>
Tue, 20 Sep 2011 10:01:23 +0000 (10:01 +0000)
committeryann.collet.73@gmail.com <yann.collet.73@gmail.com@650e7d94-2a16-8b24-b05c-7c0b3f6821cd>
Tue, 20 Sep 2011 10:01:23 +0000 (10:01 +0000)
git-svn-id: https://lz4.googlecode.com/svn/trunk@24 650e7d94-2a16-8b24-b05c-7c0b3f6821cd

lz4.c

diff --git a/lz4.c b/lz4.c
index 534b42b..54895a6 100644 (file)
--- a/lz4.c
+++ b/lz4.c
@@ -67,6 +67,7 @@
 //**************************************\r
 #define MINMATCH 4\r
 #define SKIPSTRENGTH 6\r
+#define HEAPLIMIT 13\r
 \r
 #define MAXD_LOG 16\r
 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)\r
@@ -106,8 +107,12 @@ int LZ4_compressCtx(void** ctx,
                                 char* dest,\r
                                 int isize)\r
 {      \r
+#if HASH_LOG>HEAPLIMIT\r
        struct refTables *srt = (struct refTables *) (*ctx);\r
        const BYTE**  HashTable;\r
+#else\r
+       const BYTE* HashTable[HASHTABLESIZE] = {0};\r
+#endif\r
 \r
        const BYTE* ip = (BYTE*) source;       \r
        const BYTE* anchor = ip;\r
@@ -124,6 +129,7 @@ int LZ4_compressCtx(void** ctx,
 \r
 \r
        // Init \r
+#if HASH_LOG>HEAPLIMIT\r
        if (*ctx == NULL) \r
        {\r
                srt = (struct refTables *) malloc ( sizeof(struct refTables) );\r
@@ -131,6 +137,7 @@ int LZ4_compressCtx(void** ctx,
        }\r
        HashTable = srt->hashTable;\r
        memset((void*)HashTable, 0, sizeof(srt->hashTable));\r
+#endif\r
 \r
 \r
        // First Byte\r
@@ -138,26 +145,26 @@ int LZ4_compressCtx(void** ctx,
        ip++; forwardH = HASH_VALUE(ip);\r
        \r
        // Main Loop\r
-    for ( ; ; ) 
-       {
-               int segmentSize = (1U << skipStrength) + 3;
-               const BYTE* forwardIp = ip;
-               const BYTE* ref;
-
+    for ( ; ; ) \r
+       {\r
+               int segmentSize = (1U << skipStrength) + 3;\r
+               const BYTE* forwardIp = ip;\r
+               const BYTE* ref;\r
+\r
                // Find a match\r
-               do {
-                       U32 h = forwardH;
-                       int skipped = segmentSize++ >> skipStrength;
-                       ip = forwardIp;
-                       forwardIp = ip + skipped;
-
-                       if (forwardIp > ilimit) { goto _last_literals; }
+               do {\r
+                       U32 h = forwardH;\r
+                       int skipped = segmentSize++ >> skipStrength;\r
+                       ip = forwardIp;\r
+                       forwardIp = ip + skipped;\r
+\r
+                       if (forwardIp > ilimit) { goto _last_literals; }\r
 \r
                        forwardH = HASH_VALUE(forwardIp);\r
                        ref = HashTable[h];\r
                        HashTable[h] = ip;\r
-
-               } while ((ref < ip - MAX_DISTANCE) || (*(U32*)ref != *(U32*)ip));
+\r
+               } while ((ref < ip - MAX_DISTANCE) || (*(U32*)ref != *(U32*)ip));\r
 \r
                // Catch up\r
                while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; }  \r
@@ -230,15 +237,19 @@ int LZ4_compress(char* source,
                                 char* dest,\r
                                 int isize)\r
 {\r
+#if HASH_LOG>HEAPLIMIT\r
        void* ctx = malloc(sizeof(struct refTables));\r
        int result = LZ4_compressCtx(&ctx, source, dest, isize);\r
        free(ctx);\r
-\r
        return result;\r
+#else\r
+       return LZ4_compressCtx(NULL, source, dest, isize);\r
+#endif\r
 }\r
 \r
 \r
 \r
+\r
 //****************************\r
 // Decompression CODE\r
 //****************************\r