Skip to content

Commit 863b2f8

Browse files
authored
Merge pull request facebook#983 from terrelln/dict-wlog
Increase windowLog from CDict based on the srcSize when known
2 parents 9d65a5c + 4b7c4e5 commit 863b2f8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/compress/zstd_compress.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,6 +2498,15 @@ size_t ZSTD_compressBegin_usingCDict_advanced(
24982498
if (cdict==NULL) return ERROR(dictionary_wrong);
24992499
{ ZSTD_CCtx_params params = cctx->requestedParams;
25002500
params.cParams = ZSTD_getCParamsFromCDict(cdict);
2501+
/* Increase window log to fit the entire dictionary and source if the
2502+
* source size is known. Limit the increase to 19, which is the
2503+
* window log for compression level 1 with the largest source size.
2504+
*/
2505+
if (pledgedSrcSize != ZSTD_CONTENTSIZE_UNKNOWN) {
2506+
U32 const limitedSrcSize = (U32)MIN(pledgedSrcSize, 1U << 19);
2507+
U32 const limitedSrcLog = limitedSrcSize > 1 ? ZSTD_highbit32(limitedSrcSize - 1) + 1 : 1;
2508+
params.cParams.windowLog = MAX(params.cParams.windowLog, limitedSrcLog);
2509+
}
25012510
params.fParams = fParams;
25022511
return ZSTD_compressBegin_internal(cctx,
25032512
NULL, 0, ZSTD_dm_auto,

tests/fuzzer.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,28 @@ static int basicUnitTests(U32 seed, double compressibility)
12051205
if (strcmp("No error detected", ZSTD_getErrorName(ZSTD_error_GENERIC)) != 0) goto _output_error;
12061206
DISPLAYLEVEL(3, "OK \n");
12071207

1208+
DISPLAYLEVEL(4, "test%3i : testing ZSTD dictionary sizes : ", testNb++);
1209+
RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed);
1210+
{
1211+
size_t const size = MIN(128 KB, CNBuffSize);
1212+
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
1213+
ZSTD_CDict* const lgCDict = ZSTD_createCDict(CNBuffer, size, 1);
1214+
ZSTD_CDict* const smCDict = ZSTD_createCDict(CNBuffer, 1 KB, 1);
1215+
ZSTD_frameHeader lgHeader;
1216+
ZSTD_frameHeader smHeader;
1217+
1218+
CHECK_Z(ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, CNBuffer, size, lgCDict));
1219+
CHECK_Z(ZSTD_getFrameHeader(&lgHeader, compressedBuffer, compressedBufferSize));
1220+
CHECK_Z(ZSTD_compress_usingCDict(cctx, compressedBuffer, compressedBufferSize, CNBuffer, size, smCDict));
1221+
CHECK_Z(ZSTD_getFrameHeader(&smHeader, compressedBuffer, compressedBufferSize));
1222+
1223+
if (lgHeader.windowSize != smHeader.windowSize) goto _output_error;
1224+
1225+
ZSTD_freeCDict(smCDict);
1226+
ZSTD_freeCDict(lgCDict);
1227+
ZSTD_freeCCtx(cctx);
1228+
}
1229+
12081230
_end:
12091231
free(CNBuffer);
12101232
free(compressedBuffer);

0 commit comments

Comments
 (0)