ZSTD_splitBlock_4k() uses externally provided workspace

ideally, this workspace would be provided from the ZSTD_CCtx* state
This commit is contained in:
Yann Collet
2024-10-23 11:50:56 -07:00
parent 7f015c2fd7
commit 73a6653653
3 changed files with 37 additions and 23 deletions
+6 -3
View File
@@ -4489,8 +4489,11 @@ static void ZSTD_overflowCorrectIfNeeded(ZSTD_matchState_t* ms,
#include "zstd_preSplit.h" #include "zstd_preSplit.h"
static size_t ZSTD_optimalBlockSize(const void* src, size_t srcSize, size_t blockSizeMax, ZSTD_strategy strat, S64 savings)
static size_t ZSTD_optimalBlockSize(ZSTD_CCtx* cctx, const void* src, size_t srcSize, size_t blockSizeMax, ZSTD_strategy strat, S64 savings)
{ {
S64 workspace[ZSTD_SLIPBLOCK_WORKSPACESIZE / 8];
(void)cctx;
/* note: we currenly only split full blocks (128 KB) /* note: we currenly only split full blocks (128 KB)
* and when there is more than 128 KB input remaining * and when there is more than 128 KB input remaining
*/ */
@@ -4499,7 +4502,7 @@ static size_t ZSTD_optimalBlockSize(const void* src, size_t srcSize, size_t bloc
/* dynamic splitting has a cpu cost for analysis, /* dynamic splitting has a cpu cost for analysis,
* due to that cost it's only used for btlazy2+ strategies */ * due to that cost it's only used for btlazy2+ strategies */
if (strat >= ZSTD_btlazy2) if (strat >= ZSTD_btlazy2)
return ZSTD_splitBlock_4k(src, srcSize, blockSizeMax); return ZSTD_splitBlock_4k(src, srcSize, blockSizeMax, workspace, sizeof(workspace));
/* blind split strategy /* blind split strategy
* no cpu cost, but can over-split homegeneous data. * no cpu cost, but can over-split homegeneous data.
* heuristic, tested as being "generally better". * heuristic, tested as being "generally better".
@@ -4537,7 +4540,7 @@ static size_t ZSTD_compress_frameChunk(ZSTD_CCtx* cctx,
while (remaining) { while (remaining) {
ZSTD_matchState_t* const ms = &cctx->blockState.matchState; ZSTD_matchState_t* const ms = &cctx->blockState.matchState;
U32 const lastBlock = lastFrameChunk & (blockSizeMax >= remaining); U32 const lastBlock = lastFrameChunk & (blockSizeMax >= remaining);
size_t const blockSize = ZSTD_optimalBlockSize(ip, remaining, blockSizeMax, cctx->appliedParams.cParams.strategy, savings); size_t const blockSize = ZSTD_optimalBlockSize(cctx, ip, remaining, blockSizeMax, cctx->appliedParams.cParams.strategy, savings);
assert(blockSize <= remaining); assert(blockSize <= remaining);
/* TODO: See 3090. We reduced MIN_CBLOCK_SIZE from 3 to 2 so to compensate we are adding /* TODO: See 3090. We reduced MIN_CBLOCK_SIZE from 3 to 2 so to compensate we are adding
+24 -19
View File
@@ -10,6 +10,7 @@
#include "../common/mem.h" /* S64 */ #include "../common/mem.h" /* S64 */
#include "../common/zstd_deps.h" /* ZSTD_memset */ #include "../common/zstd_deps.h" /* ZSTD_memset */
#include "../common/zstd_internal.h" /* ZSTD_STATIC_ASSERT */
#include "zstd_preSplit.h" #include "zstd_preSplit.h"
@@ -30,22 +31,19 @@ static unsigned hash2(const void *p)
} }
/* ==================================== */
/* Global array -> for testing only !!! */
/* ==================================== */
typedef struct { typedef struct {
int events[HASHTABLESIZE]; int events[HASHTABLESIZE];
S64 nbEvents; S64 nbEvents;
} FingerPrint; } FingerPrint;
static FingerPrint pastEvents; typedef struct {
static FingerPrint newEvents; FingerPrint pastEvents;
FingerPrint newEvents;
} FPStats;
static void initStats(void) static void initStats(FPStats* fpstats)
{ {
ZSTD_memset(&pastEvents, 0, sizeof(pastEvents)); ZSTD_memset(fpstats, 0, sizeof(FPStats));
ZSTD_memset(&newEvents, 0, sizeof(newEvents));
} }
/* ==================================== */
static void addToFingerprint(FingerPrint* fp, const void* src, size_t s) static void addToFingerprint(FingerPrint* fp, const void* src, size_t s)
{ {
@@ -103,14 +101,14 @@ static void mergeEvents(FingerPrint* acc, const FingerPrint* newfp)
acc->nbEvents += newfp->nbEvents; acc->nbEvents += newfp->nbEvents;
} }
static void flushEvents(void) static void flushEvents(FPStats* fpstats)
{ {
size_t n; size_t n;
for (n = 0; n < HASHTABLESIZE; n++) { for (n = 0; n < HASHTABLESIZE; n++) {
pastEvents.events[n] = newEvents.events[n]; fpstats->pastEvents.events[n] = fpstats->newEvents.events[n];
} }
pastEvents.nbEvents = newEvents.nbEvents; fpstats->pastEvents.nbEvents = fpstats->newEvents.nbEvents;
ZSTD_memset(&newEvents, 0, sizeof(newEvents)); ZSTD_memset(&fpstats->newEvents, 0, sizeof(fpstats->newEvents));
} }
static void removeEvents(FingerPrint* acc, const FingerPrint* slice) static void removeEvents(FingerPrint* acc, const FingerPrint* slice)
@@ -125,23 +123,30 @@ static void removeEvents(FingerPrint* acc, const FingerPrint* slice)
#define CHUNKSIZE (8 << 10) #define CHUNKSIZE (8 << 10)
/* Note: technically, we use CHUNKSIZE, so that's 8 KB */ /* Note: technically, we use CHUNKSIZE, so that's 8 KB */
size_t ZSTD_splitBlock_4k(const void* src, size_t srcSize, size_t blockSizeMax) size_t ZSTD_splitBlock_4k(const void* src, size_t srcSize,
size_t blockSizeMax,
void* workspace, size_t wkspSize)
{ {
FPStats* const fpstats = (FPStats*)workspace;
const char* p = (const char*)src; const char* p = (const char*)src;
int penalty = THRESHOLD_PENALTY; int penalty = THRESHOLD_PENALTY;
size_t pos = 0; size_t pos = 0;
if (srcSize <= blockSizeMax) return srcSize; if (srcSize <= blockSizeMax) return srcSize;
assert(blockSizeMax == (128 << 10)); assert(blockSizeMax == (128 << 10));
assert(workspace != NULL);
assert((size_t)workspace % 8 == 0);
ZSTD_STATIC_ASSERT(ZSTD_SLIPBLOCK_WORKSPACESIZE == sizeof(FPStats));
assert(wkspSize >= sizeof(FPStats)); (void)wkspSize;
initStats(); initStats(fpstats);
for (pos = 0; pos < blockSizeMax;) { for (pos = 0; pos < blockSizeMax;) {
assert(pos <= blockSizeMax - CHUNKSIZE); assert(pos <= blockSizeMax - CHUNKSIZE);
recordFingerprint(&newEvents, p + pos, CHUNKSIZE); recordFingerprint(&fpstats->newEvents, p + pos, CHUNKSIZE);
if (compareFingerprints(&pastEvents, &newEvents, penalty)) { if (compareFingerprints(&fpstats->pastEvents, &fpstats->newEvents, penalty)) {
return pos; return pos;
} else { } else {
mergeEvents(&pastEvents, &newEvents); mergeEvents(&fpstats->pastEvents, &fpstats->newEvents);
ZSTD_memset(&newEvents, 0, sizeof(newEvents)); ZSTD_memset(&fpstats->newEvents, 0, sizeof(fpstats->newEvents));
penalty = penalty - 1 + (penalty == 0); penalty = penalty - 1 + (penalty == 0);
} }
pos += CHUNKSIZE; pos += CHUNKSIZE;
+7 -1
View File
@@ -17,7 +17,13 @@
extern "C" { extern "C" {
#endif #endif
size_t ZSTD_splitBlock_4k(const void* src, size_t srcSize, size_t blockSizeMax); #define ZSTD_SLIPBLOCK_WORKSPACESIZE 8208
/* note:
* @workspace must be aligned on 8-bytes boundaries
* @wkspSize must be at least >= ZSTD_SLIPBLOCK_WORKSPACESIZE
*/
size_t ZSTD_splitBlock_4k(const void* src, size_t srcSize, size_t blockSizeMax, void* workspace, size_t wkspSize);
#if defined (__cplusplus) #if defined (__cplusplus)
} }