From 8e16eb0cd164071d3fc4c18e0f10527487649e93 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 2 Nov 2017 14:53:06 -0700 Subject: [PATCH] fixed last lost bytes in maximal mode even gained 2 bytes on calgary.tar... added conditional traces `g_debuglog_enable` --- lib/lz4.c | 12 +++++++----- lib/lz4hc.c | 13 +++++++++---- lib/lz4opt.h | 7 ++++--- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index e3e9172..e21822d 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -324,11 +324,12 @@ static const int LZ4_minLength = (MFLIMIT+1); #if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) # include -# define DEBUGLOG(l, ...) { \ - if (l<=LZ4_DEBUG) { \ - fprintf(stderr, __FILE__ ": "); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, " \n"); \ +static int g_debuglog_enable = 1; +# define DEBUGLOG(l, ...) { \ + if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ + fprintf(stderr, __FILE__ ": "); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, " \n"); \ } } #else # define DEBUGLOG(l, ...) {} /* disabled */ @@ -978,6 +979,7 @@ LZ4_stream_t* LZ4_createStream(void) void LZ4_resetStream (LZ4_stream_t* LZ4_stream) { + DEBUGLOG(4, "LZ4_resetStream"); MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t)); } diff --git a/lib/lz4hc.c b/lib/lz4hc.c index eb31a9c..884f5d7 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -188,12 +188,15 @@ LZ4_FORCE_INLINE int LZ4HC_InsertAndGetWiderMatch ( repeat_state_e repeat = rep_untested; size_t srcPatternLength = 0; - + DEBUGLOG(7, "LZ4HC_InsertAndGetWiderMatch"); /* First Match */ LZ4HC_Insert(hc4, ip); matchIndex = HashTable[LZ4HC_hashPtr(ip)]; + DEBUGLOG(7, "First match at index %u / %u (lowLimit)", + matchIndex, lowLimit); while ((matchIndex>=lowLimit) && (nbAttempts)) { + DEBUGLOG(7, "remaining attempts : %i", nbAttempts); nbAttempts--; if (matchIndex >= dictLimit) { const BYTE* const matchPtr = base + matchIndex; @@ -360,16 +363,18 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( #if defined(LZ4_DEBUG) && (LZ4_DEBUG >= 2) static const BYTE* start = NULL; static U32 totalCost = 0; + U32 const pos = (U32)(*anchor - start); U32 const ll = (U32)(*ip - *anchor); U32 const llAdd = (ll>=15) ? ((ll-15) / 255) + 1 : 0; U32 const mlAdd = (matchLength>=19) ? ((matchLength-19) / 255) + 1 : 0; U32 const cost = 1 + llAdd + ll + 2 + mlAdd; if (start==NULL) start = *anchor; /* only works for single segment */ - totalCost += cost; - DEBUGLOG(2, "pos:%7u -- literals:%3u, match:%4i, offset:%5u, cost:%3u/%7u", - (U32)(*anchor - start), + //g_debuglog_enable = (pos >= 112705) & (pos <= 112760); + DEBUGLOG(2, "pos:%7u -- literals:%3u, match:%4i, offset:%5u, cost:%3u / %u", + pos, (U32)(*ip - *anchor), matchLength, (U32)(*ip-match), cost, totalCost); + totalCost += cost; #endif /* Encode Literal length */ diff --git a/lib/lz4opt.h b/lib/lz4opt.h index 8a223ec..25ceaad 100644 --- a/lib/lz4opt.h +++ b/lib/lz4opt.h @@ -299,7 +299,7 @@ static int LZ4HC_compress_optimal ( } } } last_match_pos = matches[nb_matches_initial-1].len; { int addLit; - for (addLit = 1; addLit <= 2; addLit ++) { + for (addLit = 1; addLit <= 3; addLit ++) { opt[last_match_pos+addLit].mlen = 1; /* literal */ opt[last_match_pos+addLit].off = 0; opt[last_match_pos+addLit].litlen = addLit; @@ -315,6 +315,7 @@ static int LZ4HC_compress_optimal ( if (curPtr >= mflimit) break; + DEBUGLOG(7, "search at rPos:%u", cur); //nb_matches = LZ4HC_BinTree_GetAllMatches(ctx, curPtr, matchlimit, MINMATCH-1, matches, fullUpdate); nb_matches = LZ4HC_HashChain_GetAllMatches(ctx, curPtr, matchlimit, MINMATCH-1, matches, fullUpdate); //nb_matches = LZ4HC_HashChain_GetAllMatches(ctx, curPtr, matchlimit, last_match_pos - cur + 1, matches, fullUpdate); /* only works if last_match_pos is really the last match pos */ @@ -367,7 +368,7 @@ static int LZ4HC_compress_optimal ( price = opt[cur].price + LZ4HC_sequencePrice(0, ml); } - if (pos > last_match_pos+2 || price <= opt[pos].price) { + if (pos > last_match_pos+3 || price <= opt[pos].price) { DEBUGLOG(7, "rPos:%3i => price:%3i (matchlen=%i)", pos, price, ml); assert(pos < LZ4_OPT_NUM); @@ -382,7 +383,7 @@ static int LZ4HC_compress_optimal ( } } } } /* complete following positions with literals */ { int addLit; - for (addLit = 1; addLit <= 2; addLit ++) { + for (addLit = 1; addLit <= 3; addLit ++) { opt[last_match_pos+addLit].mlen = 1; /* literal */ opt[last_match_pos+addLit].off = 0; opt[last_match_pos+addLit].litlen = addLit; -- 2.7.4