From: senhuang42 Date: Fri, 13 Nov 2020 14:41:44 +0000 (-0500) Subject: Reduce number of memcpy() calls X-Git-Tag: v1.4.7~32^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2393%2Fhead;p=thirdparty%2Fzstd.git Reduce number of memcpy() calls --- diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 98caf5171..c21a8c982 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -2498,11 +2498,11 @@ static void ZSTD_copyBlockSequences(ZSTD_CCtx* zc) ZSTD_Sequence* outSeqs = &zc->seqCollector.seqStart[zc->seqCollector.seqIndex]; size_t i; repcodes_t updatedRepcodes; - U32* rep = zc->blockState.prevCBlock->rep; assert(zc->seqCollector.seqIndex + 1 < zc->seqCollector.maxSequences); /* Ensure we have enough space for last literals "sequence" */ assert(zc->seqCollector.maxSequences >= seqStoreSeqSize + 1); + ZSTD_memcpy(updatedRepcodes.rep, zc->blockState.prevCBlock->rep, sizeof(repcodes_t)); for (i = 0; i < seqStoreSeqSize; ++i) { U32 rawOffset = seqStoreSeqs[i].offset - ZSTD_REP_NUM; outSeqs[i].litLength = seqStoreSeqs[i].litLength; @@ -2521,22 +2521,23 @@ static void ZSTD_copyBlockSequences(ZSTD_CCtx* zc) /* Derive the correct offset corresponding to a repcode */ outSeqs[i].rep = seqStoreSeqs[i].offset; if (outSeqs[i].litLength != 0) { - rawOffset = rep[outSeqs[i].rep - 1]; + rawOffset = updatedRepcodes.rep[outSeqs[i].rep - 1]; } else { if (outSeqs[i].rep == 3) { - rawOffset = rep[0] - 1; + rawOffset = updatedRepcodes.rep[0] - 1; } else { - rawOffset = rep[outSeqs[i].rep]; + rawOffset = updatedRepcodes.rep[outSeqs[i].rep]; } } } outSeqs[i].offset = rawOffset; - updatedRepcodes = ZSTD_updateRep(rep, seqStoreSeqs[i].offset - 1, seqStoreSeqs[i].litLength == 0); - ZSTD_memcpy(rep, updatedRepcodes.rep, sizeof(repcodes_t)); - + /* seqStoreSeqs[i].offset == offCode+1, and ZSTD_updateRep() expects offCode + so we provide seqStoreSeqs[i].offset - 1 */ + updatedRepcodes = ZSTD_updateRep(updatedRepcodes.rep, + seqStoreSeqs[i].offset - 1, + seqStoreSeqs[i].litLength == 0); literalsRead += outSeqs[i].litLength; } - /* Insert last literals (if any exist) in the block as a sequence with ml == off == 0. * If there are no last literals, then we'll emit (of: 0, ml: 0, ll: 0), which is a marker * for the block boundary, according to the API. @@ -2546,9 +2547,6 @@ static void ZSTD_copyBlockSequences(ZSTD_CCtx* zc) outSeqs[i].litLength = (U32)lastLLSize; outSeqs[i].matchLength = outSeqs[i].offset = outSeqs[i].rep = 0; seqStoreSeqSize++; - - ZSTD_memcpy(zc->blockState.nextCBlock->rep, zc->blockState.prevCBlock->rep, - sizeof(zc->blockState.nextCBlock->rep)); zc->seqCollector.seqIndex += seqStoreSeqSize; } diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 3a3fb6a10..2afd10935 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -2721,7 +2721,7 @@ static int basicUnitTests(U32 const seed, double compressibility) if (seqs == NULL) goto _output_error; assert(cctx != NULL); - + ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 19); /* Populate src with random data */ RDG_genBuffer(CNBuffer, srcSize, compressibility, 0.5, seed);