Skip to content

Commit 80bf084

Browse files
committed
Fix Hash-Table Sizing of Small Inputs
For inputs smaller than `1 << ZSTD_WINDOWLOG_MIN` bytes, the pre-existing code in `ZSTD_adjustCParams_internal()` temporarily calculated an illegally small window log for the purposes of adjusting the hash- and chain-table sizes, before fixing that bad window log to comply with the minimums. The new code clamped the window log earlier and therefore didn't shrink the tables as much. This commit fixes that to mirror the pre-existing behavior. This resolves the remaining regression test differences.
1 parent 72c38c9 commit 80bf084

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

lib/compress/zstd_compress.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ static void ZSTD_dictAndWindowLog(ZSTD_CParams* cParams, U64 srcSize, U64 dictSi
14901490
if (windowSize >= dictSize + srcSize) {
14911491
/* Window size large enough already */
14921492
} else {
1493-
ZSTD_setMinimalWindowLogAndFrac(cParams, (U32)dictAndWindowSize);
1493+
ZSTD_setMinimalWindowLogAndFrac(cParams, (U32)dictAndWindowSize, ZSTD_WINDOWLOG_MIN);
14941494
}
14951495
}
14961496
}
@@ -1600,7 +1600,8 @@ ZSTD_adjustCParams_internal(ZSTD_CParams cPar,
16001600
if (ZSTD_windowSize(&cPar) > (1u << (ZSTD_highbit32(tSize - 1) + 1)))
16011601
#endif
16021602
{
1603-
ZSTD_setMinimalWindowLogAndFrac(&cPar, tSize);
1603+
const U32 tmpMinWindowLog = ZSTD_HASHLOG_MIN < ZSTD_WINDOWLOG_MIN ? ZSTD_HASHLOG_MIN : ZSTD_WINDOWLOG_MIN;
1604+
ZSTD_setMinimalWindowLogAndFrac(&cPar, tSize, tmpMinWindowLog);
16041605
}
16051606
}
16061607
}
@@ -6000,7 +6001,7 @@ static size_t ZSTD_compressBegin_usingCDict_internal(
60006001
if (pledgedSrcSize != ZSTD_CONTENTSIZE_UNKNOWN) {
60016002
U32 const limitedSrcSize = (U32)MIN(pledgedSrcSize, 1U << 19);
60026003
if (limitedSrcSize > 1 && ZSTD_windowSize(&cctxParams.cParams) < limitedSrcSize) {
6003-
ZSTD_setMinimalWindowLogAndFrac(&cctxParams.cParams, limitedSrcSize);
6004+
ZSTD_setMinimalWindowLogAndFrac(&cctxParams.cParams, limitedSrcSize, ZSTD_WINDOWLOG_MIN);
60046005
}
60056006
}
60066007
return ZSTD_compressBegin_internal(cctx,

lib/compress/zstd_compress_internal.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1152,11 +1152,11 @@ MEM_STATIC int ZSTD_windowLogAndFracAreMinimal(ZSTD_CParams* cParams, const U32
11521152
* Calculates the minimum legal window log and fraction that contain the
11531153
* provided source size.
11541154
*/
1155-
MEM_STATIC void ZSTD_setMinimalWindowLogAndFrac(ZSTD_CParams* cParams, const U32 srcSize) {
1156-
const U32 minSize = 1u << ZSTD_WINDOWLOG_ABSOLUTEMIN;
1155+
MEM_STATIC void ZSTD_setMinimalWindowLogAndFrac(ZSTD_CParams* cParams, const U32 srcSize, const U32 minWindowLog) {
1156+
const U32 minSize = 1u << minWindowLog;
11571157
#if ZSTD_WINDOW_ALLOW_PICKING_FRACTIONAL_SIZES
11581158
if (srcSize < minSize) {
1159-
cParams->windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN;
1159+
cParams->windowLog = minWindowLog;
11601160
cParams->windowFrac = 0;
11611161
} else {
11621162
const U32 srcSizeMinusOne = srcSize - 1;
@@ -1169,7 +1169,7 @@ MEM_STATIC void ZSTD_setMinimalWindowLogAndFrac(ZSTD_CParams* cParams, const U32
11691169
}
11701170
#else
11711171
if (srcSize < minSize) {
1172-
cParams->windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN;
1172+
cParams->windowLog = minWindowLog;
11731173
cParams->windowFrac = 0;
11741174
} else {
11751175
cParams->windowLog = ZSTD_highbit32(srcSize - 1) + 1;

0 commit comments

Comments
 (0)