Skip to content

Commit c48889f

Browse files
authored
Merge pull request facebook#2538 from senhuang42/monotonicity_test
Add memory monotonicity test over srcSize
2 parents ebc2dfa + dff4a0e commit c48889f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/compress/zstd_compress.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,15 +1390,23 @@ size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams)
13901390

13911391
static size_t ZSTD_estimateCCtxSize_internal(int compressionLevel)
13921392
{
1393-
ZSTD_compressionParameters const cParams = ZSTD_getCParams_internal(compressionLevel, ZSTD_CONTENTSIZE_UNKNOWN, 0, ZSTD_cpm_noAttachDict);
1394-
return ZSTD_estimateCCtxSize_usingCParams(cParams);
1393+
int tier = 0;
1394+
size_t largestSize = 0;
1395+
static const unsigned long long srcSizeTiers[4] = {16 KB, 128 KB, 256 KB, ZSTD_CONTENTSIZE_UNKNOWN};
1396+
for (; tier < 4; ++tier) {
1397+
/* Choose the set of cParams for a given level across all srcSizes that give the largest cctxSize */
1398+
ZSTD_compressionParameters const cParams = ZSTD_getCParams_internal(compressionLevel, srcSizeTiers[tier], 0, ZSTD_cpm_noAttachDict);
1399+
largestSize = MAX(ZSTD_estimateCCtxSize_usingCParams(cParams), largestSize);
1400+
}
1401+
return largestSize;
13951402
}
13961403

13971404
size_t ZSTD_estimateCCtxSize(int compressionLevel)
13981405
{
13991406
int level;
14001407
size_t memBudget = 0;
14011408
for (level=MIN(compressionLevel, 1); level<=compressionLevel; level++) {
1409+
/* Ensure monotonically increasing memory usage as compression level increases */
14021410
size_t const newMB = ZSTD_estimateCCtxSize_internal(level);
14031411
if (newMB > memBudget) memBudget = newMB;
14041412
}

tests/fuzzer.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,6 +3181,48 @@ static int basicUnitTests(U32 const seed, double compressibility)
31813181
}
31823182
DISPLAYLEVEL(3, "OK \n");
31833183

3184+
DISPLAYLEVEL(3, "test%3i : check compression mem usage monotonicity over levels for estimateCCtxSize() : ", testNb++);
3185+
{
3186+
int level = 1;
3187+
size_t prevSize = 0;
3188+
for (; level < ZSTD_maxCLevel(); ++level) {
3189+
size_t const currSize = ZSTD_estimateCCtxSize(level);
3190+
if (prevSize > currSize) {
3191+
DISPLAYLEVEL(3, "Error! previous cctx size: %zu at level: %d is larger than current cctx size: %zu at level: %d",
3192+
prevSize, level-1, currSize, level);
3193+
goto _output_error;
3194+
}
3195+
prevSize = currSize;
3196+
}
3197+
}
3198+
DISPLAYLEVEL(3, "OK \n");
3199+
3200+
DISPLAYLEVEL(3, "test%3i : check estimateCCtxSize() always larger or equal to ZSTD_estimateCCtxSize_usingCParams() : ", testNb++);
3201+
{
3202+
size_t const kSizeIncrement = 2 KB;
3203+
int level = -3;
3204+
3205+
for (; level <= ZSTD_maxCLevel(); ++level) {
3206+
size_t dictSize = 0;
3207+
for (; dictSize <= 256 KB; dictSize += 8 * kSizeIncrement) {
3208+
size_t srcSize = 2 KB;
3209+
for (; srcSize < 300 KB; srcSize += kSizeIncrement) {
3210+
ZSTD_compressionParameters const cParams = ZSTD_getCParams(level, srcSize, dictSize);
3211+
size_t const cctxSizeUsingCParams = ZSTD_estimateCCtxSize_usingCParams(cParams);
3212+
size_t const cctxSizeUsingLevel = ZSTD_estimateCCtxSize(level);
3213+
if (cctxSizeUsingLevel < cctxSizeUsingCParams
3214+
|| ZSTD_isError(cctxSizeUsingCParams)
3215+
|| ZSTD_isError(cctxSizeUsingLevel)) {
3216+
DISPLAYLEVEL(3, "error! l: %d dict: %zu srcSize: %zu cctx size cpar: %zu, cctx size level: %zu\n",
3217+
level, dictSize, srcSize, cctxSizeUsingCParams, cctxSizeUsingLevel);
3218+
goto _output_error;
3219+
}
3220+
}
3221+
}
3222+
}
3223+
}
3224+
DISPLAYLEVEL(3, "OK \n");
3225+
31843226
#endif
31853227

31863228
_end:

0 commit comments

Comments
 (0)