From 9e1b4828e56a028efa0efdd7c30a58e6dd48c8c1 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 7 Jan 2022 13:51:28 -0800 Subject: [PATCH] enforce a minimum price of 1 bit per literal in the optimal parser --- lib/compress/zstd_opt.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/compress/zstd_opt.c b/lib/compress/zstd_opt.c index 1b1ddad4..2c48ae55 100644 --- a/lib/compress/zstd_opt.c +++ b/lib/compress/zstd_opt.c @@ -255,11 +255,13 @@ static U32 ZSTD_rawLiteralsCost(const BYTE* const literals, U32 const litLength, return (litLength*6) * BITCOST_MULTIPLIER; /* 6 bit per literal - no statistic used */ /* dynamic statistics */ - { U32 price = litLength * optPtr->litSumBasePrice; + { U32 price = 0; U32 u; for (u=0; u < litLength; u++) { + U32 litPrice = optPtr->litSumBasePrice - WEIGHT(optPtr->litFreq[literals[u]], optLevel); assert(WEIGHT(optPtr->litFreq[literals[u]], optLevel) <= optPtr->litSumBasePrice); /* literal cost should never be negative */ - price -= WEIGHT(optPtr->litFreq[literals[u]], optLevel); + if (litPrice < BITCOST_MULTIPLIER) litPrice = BITCOST_MULTIPLIER; + price += litPrice; } return price; }