From cedf9cd57f142327cb3b92a574125f46975d7a48 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 7 Sep 2022 22:50:28 -0700 Subject: allocation optimization for lz4frame compression as noted by @yixiutt, the temporary buffer inside lz4frame compression is being invalidated at end of compression, forcing it to be re-allocated at next compression job. This shouldn't be necessary. This change was introduced in #236, as a way to fix #232, but neither the issue is explained, nor why the patch fixes it. This patch revert to previous behavior, where temporary buffer is kept between compression calls. This results in a net reduction of allocation workload. Additionally, the temporary buffer should only need malloc(), not calloc(), thus saving initialization. This diff implements both changes. Long fuzzer tests will be run to ensure that no sanitizer warning get triggered. Additionally : fixed a minor ubsan warning in LZ4F_decompress(). --- lib/lz4frame.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 174f9ae..b4caff4 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -264,7 +264,7 @@ typedef struct LZ4F_cctx_s LZ4F_CustomMem cmem; LZ4F_preferences_t prefs; U32 version; - U32 cStage; + U32 cStage; /* 0 : compression uninitialized ; 1 : initialized, can compress */ const LZ4F_CDict* cdict; size_t maxBlockSize; size_t maxBufferSize; @@ -732,7 +732,7 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, if (cctxPtr->maxBufferSize < requiredBuffSize) { cctxPtr->maxBufferSize = 0; LZ4F_free(cctxPtr->tmpBuff, cctxPtr->cmem); - cctxPtr->tmpBuff = (BYTE*)LZ4F_calloc(requiredBuffSize, cctxPtr->cmem); + cctxPtr->tmpBuff = (BYTE*)LZ4F_malloc(requiredBuffSize, cctxPtr->cmem); RETURN_ERROR_IF(cctxPtr->tmpBuff == NULL, allocation_failed); cctxPtr->maxBufferSize = requiredBuffSize; } } @@ -1176,7 +1176,6 @@ size_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr, } cctxPtr->cStage = 0; /* state is now re-usable (with identical preferences) */ - cctxPtr->maxBufferSize = 0; /* reuse HC context */ if (cctxPtr->prefs.frameInfo.contentSize) { if (cctxPtr->prefs.frameInfo.contentSize != cctxPtr->totalInSize) @@ -1722,10 +1721,10 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, /* history management (linked blocks only)*/ if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0); - } } - - srcPtr += sizeToCopy; - dstPtr += sizeToCopy; + } + srcPtr += sizeToCopy; + dstPtr += sizeToCopy; + } if (sizeToCopy == dctx->tmpInTarget) { /* all done */ if (dctx->frameInfo.blockChecksumFlag) { dctx->tmpInSize = 0; -- cgit v0.12