small compression speed improvement
authoryann.collet.73@gmail.com <yann.collet.73@gmail.com@650e7d94-2a16-8b24-b05c-7c0b3f6821cd>
Tue, 13 Sep 2011 22:55:49 +0000 (22:55 +0000)
committeryann.collet.73@gmail.com <yann.collet.73@gmail.com@650e7d94-2a16-8b24-b05c-7c0b3f6821cd>
Tue, 13 Sep 2011 22:55:49 +0000 (22:55 +0000)
git-svn-id: https://lz4.googlecode.com/svn/trunk@19 650e7d94-2a16-8b24-b05c-7c0b3f6821cd

lz4.c

diff --git a/lz4.c b/lz4.c
index bba9690..1fa9aa1 100644 (file)
--- a/lz4.c
+++ b/lz4.c
@@ -66,7 +66,7 @@
 // Constants\r
 //**************************************\r
 #define MINMATCH 4\r
-#define INCOMPRESSIBLE 128\r
+#define SKIPSTRENGTH 6\r
 \r
 #define MAXD_LOG 16\r
 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)\r
@@ -85,7 +85,7 @@
 //**************************************\r
 struct refTables\r
 {\r
-       BYTE* hashTable[HASHTABLESIZE];\r
+       const BYTE* hashTable[HASHTABLESIZE];\r
 };\r
 \r
 \r
@@ -107,20 +107,21 @@ int LZ4_compressCtx(void** ctx,
                                 int isize)\r
 {      \r
        struct refTables *srt = (struct refTables *) (*ctx);\r
-       BYTE**  HashTable;\r
+       const BYTE**  HashTable;\r
 \r
-       BYTE    *ip = (BYTE*) source,       \r
-                       *anchor = ip,\r
-                       *incompressible = anchor + INCOMPRESSIBLE,\r
-                       *iend = ip + isize,\r
-                       *ilimit = iend - MINMATCH;\r
+       const BYTE* ip = (BYTE*) source;       \r
+       const BYTE* anchor = ip;\r
+       const BYTE* ref;\r
+       const BYTE* const iend = ip + isize;\r
+       const BYTE* const ilimit = iend - MINMATCH;\r
 \r
-       BYTE    *op = (BYTE*) dest,  \r
-                       *ref,\r
-                       *orun, *l_end;\r
+       BYTE* op = (BYTE*) dest;\r
+       BYTE* orun;\r
+       BYTE* l_end;\r
        \r
-       int             len, length;\r
-       U32             step=1;\r
+       int len, length;\r
+       const int skipStrength = SKIPSTRENGTH;\r
+       U32 skipped = 1U << skipStrength;\r
 \r
 \r
        // Init \r
@@ -133,19 +134,14 @@ int LZ4_compressCtx(void** ctx,
        memset(HashTable, 0, sizeof(srt->hashTable));\r
 \r
        // Main Loop\r
-       while (ip < ilimit)\r
+       while (ip < ilimit+1)\r
        {\r
                ref = HashTable[HASH_VALUE(ip)];\r
                HashTable[HASH_VALUE(ip)] = ip;\r
 \r
                // Min Match\r
-               if ((ref < ip - MAX_DISTANCE) || (*(U32*)ref != *(U32*)ip))\r
-               { \r
-                       if (ip>incompressible) { incompressible += INCOMPRESSIBLE << (step >> 1); step++; }\r
-                       ip+=step; \r
-                       continue; \r
-               }\r
-               step=1;\r
+               if ((ref < ip - MAX_DISTANCE) || (*(U32*)ref != *(U32*)ip)) { ip += (skipped++) >> skipStrength ; continue; }\r
+               skipped = (1U << skipStrength) + 3;\r
 \r
                // Catch up\r
                while ((ip>anchor) && (ref>(BYTE*)source) && (ip[-1]==ref[-1])) { ip--; ref--; }  \r
@@ -161,6 +157,7 @@ int LZ4_compressCtx(void** ctx,
                do { *(U32*)op = *(U32*)anchor; op+=4; anchor+=4; } while (op<l_end) ;\r
                op = l_end;\r
 \r
+_next_match:\r
                // Encode Offset\r
                *(U16*)op = (ip-ref); op+=2;\r
 \r
@@ -181,11 +178,18 @@ _endCount:
                \r
                // Encode MatchLength\r
                if (len>=(int)ML_MASK) { *orun+=ML_MASK; len-=ML_MASK; for(; len > 509 ; len-=510) { *op++ = 255; *op++ = 255; } if (len > 254) { len-=255; *op++ = 255; } *op++ = (BYTE)len; } \r
-               else *orun += len;                      \r
+               else *orun += len;      \r
+\r
+               // Test end of chunk\r
+               if (ip > ilimit) { anchor = ip;  break; }\r
+\r
+               // Test next position\r
+               ref = HashTable[HASH_VALUE(ip)];\r
+               HashTable[HASH_VALUE(ip)] = ip;\r
+               if ((ref > ip - (MAX_DISTANCE + 1)) && (*(U32*)ref == *(U32*)ip)) { orun = op++; *orun=0; goto _next_match; }\r
 \r
                // Prepare next loop\r
-               anchor = ip; \r
-               incompressible = anchor + INCOMPRESSIBLE;\r
+               anchor = ip++; \r
        }\r
 \r
        // Encode Last Literals\r
@@ -204,6 +208,7 @@ _endCount:
 }\r
 \r
 \r
+\r
 int LZ4_compress(char* source, \r
                                 char* dest,\r
                                 int isize)\r