From 61c2d70c869f68e93a7af62327df2f0cb6022c75 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 15 Nov 2017 14:37:40 -0800 Subject: [PATCH] shortened repcode match finder implementation --- lib/compress/zstd_opt.c | 79 +++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 51 deletions(-) diff --git a/lib/compress/zstd_opt.c b/lib/compress/zstd_opt.c index 5c65ed720..f8e9c92a5 100644 --- a/lib/compress/zstd_opt.c +++ b/lib/compress/zstd_opt.c @@ -265,59 +265,36 @@ FORCE_INLINE_TEMPLATE size_t bestLength = minMatchLen-1; /* check repCode */ -#if 1 - if (!extDict) /*static*/ { - U32 const lastR = ZSTD_REP_NUM + ll0; + { U32 const lastR = ZSTD_REP_NUM + ll0; U32 repCode; for (repCode = ll0; repCode < lastR; repCode++) { - S32 const repOffset = (repCode==ZSTD_REP_NUM) ? (rep[0] - 1) : rep[repCode]; - if ( (repOffset > 0) - && (repOffset < (S32)(ip-prefixStart)) /* within current mem segment */ - && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(ip - repOffset, minMatch))) { - U32 const repLen = (U32)ZSTD_count(ip+minMatch, ip+minMatch-repOffset, iLimit) + minMatch; - /* save longer solution */ - if (repLen > bestLength) { - DEBUGLOG(8, "current %u : found rep%u match of size%3u", - current, repCode - ll0, repLen); - bestLength = repLen; - matches[mnum].off = repCode - ll0; - matches[mnum].len = (U32)repLen; - mnum++; - if ( (repLen > ZSTD_OPT_NUM) - | (ip+repLen == iLimit) ) { /* best possible */ - return mnum; - } } } } - } else { /* extDict */ - U32 const lastR = ZSTD_REP_NUM + ll0; - U32 repCode; - for (repCode=ll0; repCode 0 && repCur <= (S32)current) - && (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex>windowLow)) /* intentional overflow */ - && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch)) ) { - /* repcode detected we should take it */ - const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iLimit; - U32 const repLen = (U32)ZSTD_count_2segments(ip+minMatch, repMatch+minMatch, iLimit, repEnd, prefixStart) + minMatch; - - /* save longer solution */ - if (repLen > bestLength) { - DEBUGLOG(8, "current %u : found rep%u match of size%3u", - current, repCode - ll0, repLen); - bestLength = repLen; - matches[mnum].off = repCode - ll0; - matches[mnum].len = (U32)repLen; - mnum++; - if ( (repLen > ZSTD_OPT_NUM) - | (ip+repLen == iLimit) ) { /* best possible */ - return mnum; - } } } } - } -#else - (void)ll0; (void)rep; (void)minMatch; -#endif + U32 const repOffset = (repCode==ZSTD_REP_NUM) ? (rep[0] - 1) : rep[repCode]; + U32 const repIndex = current - repOffset; + U32 repLen = 0; + assert(repOffset <= current); + assert(current >= dictLimit); + if (!extDict /*static*/ || (repIndex>=dictLimit)) { + if ( (repOffset-1 /* intentional overflow, discards 0 and -1 */ < current-dictLimit) /* within current mem segment */ + && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(ip - repOffset, minMatch))) { + repLen = (U32)ZSTD_count(ip+minMatch, ip+minMatch-repOffset, iLimit) + minMatch; + } + } else { /* extDict */ + const BYTE* const repMatch = dictBase + repIndex; + if ( ((repOffset-1) /*intentional overflow*/ < current) + & (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ & (repIndex > windowLow)) + && (ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch)) ) { + repLen = (U32)ZSTD_count_2segments(ip+minMatch, repMatch+minMatch, iLimit, dictEnd, prefixStart) + minMatch; + } } + /* save longer solution */ + if (repLen > bestLength) { + bestLength = repLen; + matches[mnum].off = repCode - ll0; + matches[mnum].len = (U32)repLen; + mnum++; + if ( (repLen > ZSTD_OPT_NUM) + | (ip+repLen == iLimit) ) { /* best possible */ + return mnum; + } } } } /* HC3 match finder */ if ((mls == 3) /*static*/ && (bestLength < mls)) {