From b5a519f43cfc1cb3502f9471d68a4b7df8027e29 Mon Sep 17 00:00:00 2001 From: inikep Date: Wed, 9 Mar 2016 15:45:01 +0100 Subject: [PATCH] improved compression speed introduced cache in ZSTD_setLog2Prices --- lib/zstd_internal.h | 6 ++++++ lib/zstd_opt.h | 36 +++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/zstd_internal.h b/lib/zstd_internal.h index 462161bd..130eb210 100644 --- a/lib/zstd_internal.h +++ b/lib/zstd_internal.h @@ -207,6 +207,12 @@ typedef struct { U32 litLengthSum; U32 litSum; U32 offCodeSum; + U32 log2matchLengthSum; + U32 log2matchSum; + U32 log2litLengthSum; + U32 log2litSum; + U32 log2offCodeSum; + U32 factor; #if ZSTD_OPT_DEBUG == 3 U32 realMatchSum; U32 realLitSum; diff --git a/lib/zstd_opt.h b/lib/zstd_opt.h index 961964bd..f89b737a 100644 --- a/lib/zstd_opt.h +++ b/lib/zstd_opt.h @@ -39,16 +39,26 @@ /*-************************************* * Price functions for optimal parser ***************************************/ +FORCE_INLINE void ZSTD_setLog2Prices(seqStore_t* ssPtr) +{ + ssPtr->log2matchLengthSum = ZSTD_highbit(ssPtr->matchLengthSum+1); + ssPtr->log2litLengthSum = ZSTD_highbit(ssPtr->litLengthSum+1); + ssPtr->log2litSum = ZSTD_highbit(ssPtr->litSum+1); + ssPtr->log2offCodeSum = ZSTD_highbit(ssPtr->offCodeSum+1); + ssPtr->factor = 1 + ((ssPtr->litSum>>5) / ssPtr->litLengthSum) + ((ssPtr->litSum<<1) / (ssPtr->litSum + ssPtr->matchSum)); +} + + MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) { unsigned u; if (ssPtr->litLengthSum == 0) { - ssPtr->litSum = 2*(1<litLengthSum = 1*(1<matchLengthSum = 1*(1<offCodeSum = 1*(1<matchSum = 2*(1<litSum = (2<litLengthSum = (1<matchLengthSum = (1<offCodeSum = (1<matchSum = (2<litFreq[u] = 2; @@ -83,6 +93,8 @@ MEM_STATIC void ZSTD_rescaleFreqs(seqStore_t* ssPtr) ssPtr->offCodeSum += ssPtr->offCodeFreq[u]; } } + + ZSTD_setLog2Prices(ssPtr); } @@ -91,17 +103,17 @@ FORCE_INLINE U32 ZSTD_getLiteralPrice(seqStore_t* seqStorePtr, U32 litLength, co U32 price, u; if (litLength == 0) - return ZSTD_highbit(seqStorePtr->litLengthSum+1) - ZSTD_highbit(seqStorePtr->litLengthFreq[0]+1); + return seqStorePtr->log2litLengthSum - ZSTD_highbit(seqStorePtr->litLengthFreq[0]+1); /* literals */ - price = litLength * ZSTD_highbit(seqStorePtr->litSum+1); + price = litLength * seqStorePtr->log2litSum; for (u=0; u < litLength; u++) price -= ZSTD_highbit(seqStorePtr->litFreq[literals[u]]+1); /* literal Length */ price += ((litLength >= MaxLL)<<3) + ((litLength >= 255+MaxLL)<<4) + ((litLength>=(1<<15))<<3); if (litLength >= MaxLL) litLength = MaxLL; - price += ZSTD_highbit(seqStorePtr->litLengthSum+1) - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]+1); + price += seqStorePtr->log2litLengthSum - ZSTD_highbit(seqStorePtr->litLengthFreq[litLength]+1); return price; } @@ -111,12 +123,12 @@ FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYT { /* offset */ BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset+1) + 1 : 0; - U32 price = (offCode-1) + (!offCode) + ZSTD_highbit(seqStorePtr->offCodeSum+1) - ZSTD_highbit(seqStorePtr->offCodeFreq[offCode]+1); + U32 price = (offCode-1) + (!offCode) + seqStorePtr->log2offCodeSum - ZSTD_highbit(seqStorePtr->offCodeFreq[offCode]+1); /* match Length */ price += ((matchLength >= MaxML)<<3) + ((matchLength >= 255+MaxML)<<4) + ((matchLength>=(1<<15))<<3); if (matchLength >= MaxML) matchLength = MaxML; - price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + ZSTD_highbit(seqStorePtr->matchLengthSum+1) - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]+1); + price += ZSTD_getLiteralPrice(seqStorePtr, litLength, literals) + seqStorePtr->log2matchLengthSum - ZSTD_highbit(seqStorePtr->matchLengthFreq[matchLength]+1); #if ZSTD_OPT_DEBUG == 3 switch (seqStorePtr->priceFunc) { @@ -127,7 +139,7 @@ FORCE_INLINE U32 ZSTD_getPrice(seqStore_t* seqStorePtr, U32 litLength, const BYT return 1 + price; } #else - return 1 + price + ((seqStorePtr->litSum>>5) / seqStorePtr->litLengthSum) + ((seqStorePtr->litSum<<1) / (seqStorePtr->litSum + seqStorePtr->matchSum)); + return price + seqStorePtr->factor; #endif } @@ -159,6 +171,8 @@ MEM_STATIC void ZSTD_updatePrice(seqStore_t* seqStorePtr, U32 litLength, const B seqStorePtr->matchLengthFreq[MaxML]++; else seqStorePtr->matchLengthFreq[matchLength]++; + + ZSTD_setLog2Prices(seqStorePtr); }