LZ4_compress_HC_continue_destSize() now compatible with optimal parser
authorYann Collet <cyan@fb.com>
Fri, 3 Nov 2017 09:01:20 +0000 (02:01 -0700)
committerYann Collet <cyan@fb.com>
Fri, 3 Nov 2017 09:03:19 +0000 (02:03 -0700)
levels 11+

lib/lz4hc.c
lib/lz4hc.h
lib/lz4opt.h

index c1f8da6..643fbb2 100644 (file)
@@ -642,8 +642,11 @@ _dest_overflow:
 
 static int LZ4HC_getSearchNum(int compressionLevel)
 {
+    assert(compressionLevel >= 1);
+    assert(compressionLevel <= LZ4HC_CLEVEL_MAX);
     switch (compressionLevel) {
-        default: return 0; /* unused */
+        default: return 1 << (compressionLevel-1);
+        case 10: return 1 << 12;
         case 11: return 512;
         case 12: return 1<<13;
     }
@@ -709,8 +712,7 @@ int LZ4_compress_HC(const char* src, char* dst, int srcSize, int dstCapacity, in
 }
 
 /* LZ4_compress_HC_destSize() :
- * currently, only compatible with Hash Chain implementation,
- * hence limit compression level to LZ4HC_CLEVEL_OPT_MIN-1*/
+ * only compatible with Hash Chain match finder */
 int LZ4_compress_HC_destSize(void* LZ4HC_Data, const char* source, char* dest, int* sourceSizePtr, int targetDestSize, int cLevel)
 {
     LZ4HC_CCtx_internal* const ctx = &((LZ4_streamHC_t*)LZ4HC_Data)->internal_donotuse;
@@ -744,6 +746,7 @@ void LZ4_resetStreamHC (LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel)
 
 void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel)
 {
+    /* note : 1-10 / 11-12 separation might no longer be necessary since optimal parser uses hash chain too */
     int const currentCLevel = LZ4_streamHCPtr->internal_donotuse.compressionLevel;
     int const minCLevel = currentCLevel < LZ4HC_CLEVEL_OPT_MIN ? 1 : LZ4HC_CLEVEL_OPT_MIN;
     int const maxCLevel = currentCLevel < LZ4HC_CLEVEL_OPT_MIN ? LZ4HC_CLEVEL_OPT_MIN-1 : LZ4HC_CLEVEL_MAX;
@@ -824,8 +827,6 @@ int LZ4_compress_HC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* src,
 
 int LZ4_compress_HC_continue_destSize (LZ4_streamHC_t* LZ4_streamHCPtr, const char* src, char* dst, int* srcSizePtr, int targetDestSize)
 {
-    LZ4HC_CCtx_internal* const ctxPtr = &LZ4_streamHCPtr->internal_donotuse;
-    if (ctxPtr->compressionLevel >= LZ4HC_CLEVEL_OPT_MIN) LZ4HC_init(ctxPtr, (const BYTE*)src);   /* not compatible with btopt implementation */
     return LZ4_compressHC_continue_generic(LZ4_streamHCPtr, src, dst, srcSizePtr, targetDestSize, limitedDestSize);
 }
 
index 191ab0c..4fbe91e 100644 (file)
@@ -266,8 +266,8 @@ int LZ4_compress_HC_continue_destSize(LZ4_streamHC_t* LZ4_streamHCPtr,
                             int* srcSizePtr, int targetDstSize);
 
 /*! LZ4_setCompressionLevel() : v1.8.0 (experimental)
- *  It's possible to change compression level after LZ4_resetStreamHC(), between 2 invocations of LZ4_compress_HC_continue*(),
- *  but that requires to stay in the same mode (aka 1-10 or 11-12).
+ *  It's possible to change compression level between 2 invocations of LZ4_compress_HC_continue*(),
+ *  but it requires to stay in the same mode (aka 1-10 or 11-12).
  *  This function ensures this condition.
  */
 void LZ4_setCompressionLevel(LZ4_streamHC_t* LZ4_streamHCPtr, int compressionLevel);
index a1627f1..18bcd31 100644 (file)
@@ -160,7 +160,7 @@ static int LZ4HC_compress_optimal (
                 DEBUGLOG(7, "rPos:%3i => price:%3i (litlen=%i) -- initial setup",
                             rPos, cost, opt[rPos].litlen);
         }   }
-        /* set prices using matches found for rPos = 0 */
+        /* set prices using initial match */
         {   int mlen = MINMATCH;
             int const matchML = firstMatch.len;   /* necessarily < sufficient_len < LZ4_OPT_NUM */
             int const offset = firstMatch.off;
@@ -215,7 +215,7 @@ static int LZ4HC_compress_optimal (
                 goto encode;
             }
 
-            /* before first match : set price with literals at beginning */
+            /* before match : set price with literals at beginning */
             {   int const baseLitlen = opt[cur].litlen;
                 int litlen;
                 for (litlen = 1; litlen < MINMATCH; litlen++) {
@@ -230,10 +230,10 @@ static int LZ4HC_compress_optimal (
                                     pos, price, opt[pos].litlen);
             }   }   }
 
-            /* set prices using matches at position = cur */
+            /* set prices using match at position = cur */
             {   int const matchML = newMatch.len;
                 int ml = MINMATCH;
-                
+
                 assert(cur + newMatch.len < LZ4_OPT_NUM);
                 for ( ; ml <= matchML ; ml++) {
                     int const pos = cur + ml;
@@ -255,7 +255,7 @@ static int LZ4HC_compress_optimal (
                         DEBUGLOG(7, "rPos:%3i => price:%3i (matchlen=%i)",
                                     pos, price, ml);
                         assert(pos < LZ4_OPT_NUM);
-                        if ( (ml == matchML)  /* last post of last match */
+                        if ( (ml == matchML)  /* last pos of last match */
                           && (last_match_pos < pos) )
                             last_match_pos = pos;
                         opt[pos].mlen = ml;