External matchfinder API (#3333)
* First building commit with sample matchfinder * Set up ZSTD_externalMatchCtx struct * move seqBuffer to ZSTD_Sequence* * support non-contiguous dictionary * clean up parens * add clearExternalMatchfinder, handle allocation errors * Add useExternalMatchfinder cParam * validate useExternalMatchfinder cParam * Disable LDM + external matchfinder * Check for static CCtx * Validate mState and mStateDestructor * Improve LDM check to cover both branches * Error API with optional fallback * handle RLE properly for external matchfinder * nit * Move to a CDict-like model for resource ownership * Add hidden useExternalMatchfinder bool to CCtx_params_s * Eliminate malloc, move to cwksp allocation * Handle CCtx reset properly * Ensure seqStore has enough space for external sequences * fix capitalization * Add DEBUGLOG statements * Add compressionLevel param to matchfinder API * fix c99 issues and add a param combination error code * nits * Test external matchfinder API * C90 compat for simpleExternalMatchFinder * Fix some @nocommits and an ASAN bug * nit * nit * nits * forward declare copySequencesToSeqStore functions in zstd_compress_internal.h * nit * nit * nits * Update copyright headers * Fix CMake zstreamtest build * Fix copyright headers (again) * typo * Add externalMatchfinder demo program to make contrib * Reduce memory consumption for small blockSize * ZSTD_postProcessExternalMatchFinderResult nits * test sum(matchlen) + sum(litlen) == srcSize in debug builds * refExternalMatchFinder -> registerExternalMatchFinder * C90 nit * zstreamtest nits * contrib nits * contrib nits * allow block splitter + external matchfinder, refactor * add windowSize param * add contrib/externalMatchfinder/README.md * docs * go back to old RLE heuristic because of the first block issue * fix initializer element is not a constant expression * ref contrib from zstd.h * extremely pedantic compiler warning fix, meson fix, typo fix * Additional docs on API limitations * minor nits * Refactor maxNbSeq calculation into a helper function * Fix copyright
This commit is contained in:
+1
-1
@@ -169,7 +169,7 @@ fuzzer-dll : $(ZSTDDIR)/common/xxhash.c $(PRGDIR)/util.c $(PRGDIR)/timefn.c $(PR
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) $(LDFLAGS) -o $@$(EXT)
|
||||
|
||||
CLEAN += zstreamtest zstreamtest32
|
||||
ZSTREAM_LOCAL_FILES := $(PRGDIR)/datagen.c $(PRGDIR)/util.c $(PRGDIR)/timefn.c seqgen.c zstreamtest.c
|
||||
ZSTREAM_LOCAL_FILES := $(PRGDIR)/datagen.c $(PRGDIR)/util.c $(PRGDIR)/timefn.c seqgen.c zstreamtest.c external_matchfinder.c
|
||||
ZSTREAM_PROPER_FILES := $(ZDICT_FILES) $(ZSTREAM_LOCAL_FILES)
|
||||
ZSTREAMFILES := $(ZSTD_FILES) $(ZSTREAM_PROPER_FILES)
|
||||
zstreamtest32 : CFLAGS += -m32
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) Yann Collet, Meta Platforms, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under both the BSD-style license (found in the
|
||||
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
||||
* in the COPYING file in the root directory of this source tree).
|
||||
* You may select, at your option, one of the above-listed licenses.
|
||||
*/
|
||||
|
||||
#include "external_matchfinder.h"
|
||||
#include <string.h>
|
||||
#include "zstd_compress_internal.h"
|
||||
|
||||
#define HSIZE 1024
|
||||
static U32 const HLOG = 10;
|
||||
static U32 const MLS = 4;
|
||||
static U32 const BADIDX = 0xffffffff;
|
||||
|
||||
static size_t simpleExternalMatchFinder(
|
||||
void* externalMatchState,
|
||||
ZSTD_Sequence* outSeqs, size_t outSeqsCapacity,
|
||||
const void* src, size_t srcSize,
|
||||
const void* dict, size_t dictSize,
|
||||
int compressionLevel,
|
||||
size_t windowSize
|
||||
) {
|
||||
const BYTE* const istart = (const BYTE*)src;
|
||||
const BYTE* const iend = istart + srcSize;
|
||||
const BYTE* ip = istart;
|
||||
const BYTE* anchor = istart;
|
||||
size_t seqCount = 0;
|
||||
U32 hashTable[HSIZE];
|
||||
|
||||
(void)externalMatchState;
|
||||
(void)dict;
|
||||
(void)dictSize;
|
||||
(void)outSeqsCapacity;
|
||||
(void)compressionLevel;
|
||||
|
||||
{ int i;
|
||||
for (i=0; i < HSIZE; i++) {
|
||||
hashTable[i] = BADIDX;
|
||||
} }
|
||||
|
||||
while (ip + MLS < iend) {
|
||||
size_t const hash = ZSTD_hashPtr(ip, HLOG, MLS);
|
||||
U32 const matchIndex = hashTable[hash];
|
||||
hashTable[hash] = (U32)(ip - istart);
|
||||
|
||||
if (matchIndex != BADIDX) {
|
||||
const BYTE* const match = istart + matchIndex;
|
||||
U32 const matchLen = (U32)ZSTD_count(ip, match, iend);
|
||||
if (matchLen >= ZSTD_MINMATCH_MIN) {
|
||||
U32 const litLen = (U32)(ip - anchor);
|
||||
U32 const offset = (U32)(ip - match);
|
||||
ZSTD_Sequence const seq = {
|
||||
offset, litLen, matchLen, 0
|
||||
};
|
||||
|
||||
/* Note: it's crucial to stay within the window size! */
|
||||
if (offset <= windowSize) {
|
||||
outSeqs[seqCount++] = seq;
|
||||
ip += matchLen;
|
||||
anchor = ip;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ip++;
|
||||
}
|
||||
|
||||
{ ZSTD_Sequence const finalSeq = {
|
||||
0, (U32)(iend - anchor), 0, 0
|
||||
};
|
||||
outSeqs[seqCount++] = finalSeq;
|
||||
}
|
||||
|
||||
return seqCount;
|
||||
}
|
||||
|
||||
size_t zstreamExternalMatchFinder(
|
||||
void* externalMatchState,
|
||||
ZSTD_Sequence* outSeqs, size_t outSeqsCapacity,
|
||||
const void* src, size_t srcSize,
|
||||
const void* dict, size_t dictSize,
|
||||
int compressionLevel,
|
||||
size_t windowSize
|
||||
) {
|
||||
EMF_testCase const testCase = *((EMF_testCase*)externalMatchState);
|
||||
memset(outSeqs, 0, outSeqsCapacity);
|
||||
|
||||
switch (testCase) {
|
||||
case EMF_ZERO_SEQS:
|
||||
return 0;
|
||||
case EMF_ONE_BIG_SEQ:
|
||||
outSeqs[0].offset = 0;
|
||||
outSeqs[0].matchLength = 0;
|
||||
outSeqs[0].litLength = (U32)(srcSize);
|
||||
return 1;
|
||||
case EMF_LOTS_OF_SEQS:
|
||||
return simpleExternalMatchFinder(
|
||||
externalMatchState,
|
||||
outSeqs, outSeqsCapacity,
|
||||
src, srcSize,
|
||||
dict, dictSize,
|
||||
compressionLevel,
|
||||
windowSize
|
||||
);
|
||||
case EMF_SMALL_ERROR:
|
||||
return outSeqsCapacity + 1;
|
||||
case EMF_BIG_ERROR:
|
||||
default:
|
||||
return ZSTD_EXTERNAL_MATCHFINDER_ERROR;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) Yann Collet, Meta Platforms, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under both the BSD-style license (found in the
|
||||
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
||||
* in the COPYING file in the root directory of this source tree).
|
||||
* You may select, at your option, one of the above-listed licenses.
|
||||
*/
|
||||
|
||||
#ifndef EXTERNAL_MATCHFINDER
|
||||
#define EXTERNAL_MATCHFINDER
|
||||
|
||||
#define ZSTD_STATIC_LINKING_ONLY
|
||||
#include "zstd.h"
|
||||
|
||||
/* See external_matchfinder.c for details on each test case */
|
||||
typedef enum {
|
||||
EMF_ZERO_SEQS = 0,
|
||||
EMF_ONE_BIG_SEQ = 1,
|
||||
EMF_LOTS_OF_SEQS = 2,
|
||||
EMF_BIG_ERROR = 3,
|
||||
EMF_SMALL_ERROR = 4
|
||||
} EMF_testCase;
|
||||
|
||||
size_t zstreamExternalMatchFinder(
|
||||
void* externalMatchState,
|
||||
ZSTD_Sequence* outSeqs, size_t outSeqsCapacity,
|
||||
const void* src, size_t srcSize,
|
||||
const void* dict, size_t dictSize,
|
||||
int compressionLevel,
|
||||
size_t windowSize
|
||||
);
|
||||
|
||||
#endif // EXTERNAL_MATCHFINDER
|
||||
+92
-1
@@ -39,7 +39,7 @@
|
||||
#include "seqgen.h"
|
||||
#include "util.h"
|
||||
#include "timefn.h" /* UTIL_time_t, UTIL_clockSpanMicro, UTIL_getTime */
|
||||
|
||||
#include "external_matchfinder.h" /* zstreamExternalMatchFinder, EMF_testCase */
|
||||
|
||||
/*-************************************
|
||||
* Constants
|
||||
@@ -1834,6 +1834,97 @@ static int basicUnitTests(U32 seed, double compressibility, int bigTests)
|
||||
}
|
||||
DISPLAYLEVEL(3, "OK \n");
|
||||
|
||||
DISPLAYLEVEL(3, "test%3i : External matchfinder API: ", testNb++);
|
||||
{
|
||||
size_t const dstBufSize = ZSTD_compressBound(CNBufferSize);
|
||||
BYTE* const dstBuf = (BYTE*)malloc(ZSTD_compressBound(dstBufSize));
|
||||
size_t const checkBufSize = CNBufferSize;
|
||||
BYTE* const checkBuf = (BYTE*)malloc(checkBufSize);
|
||||
int enableFallback;
|
||||
EMF_testCase externalMatchState;
|
||||
|
||||
CHECK(dstBuf == NULL || checkBuf == NULL, "allocation failed");
|
||||
|
||||
ZSTD_CCtx_reset(zc, ZSTD_reset_session_and_parameters);
|
||||
|
||||
/* Reference external matchfinder outside the test loop to
|
||||
* check that the reference is preserved across compressions */
|
||||
ZSTD_registerExternalMatchFinder(
|
||||
zc,
|
||||
&externalMatchState,
|
||||
zstreamExternalMatchFinder
|
||||
);
|
||||
|
||||
for (enableFallback = 0; enableFallback < 1; enableFallback++) {
|
||||
size_t testCaseId;
|
||||
|
||||
EMF_testCase const EMF_successCases[] = {
|
||||
EMF_ONE_BIG_SEQ,
|
||||
EMF_LOTS_OF_SEQS,
|
||||
};
|
||||
size_t const EMF_numSuccessCases = 2;
|
||||
|
||||
EMF_testCase const EMF_failureCases[] = {
|
||||
EMF_ZERO_SEQS,
|
||||
EMF_BIG_ERROR,
|
||||
EMF_SMALL_ERROR,
|
||||
};
|
||||
size_t const EMF_numFailureCases = 3;
|
||||
|
||||
/* Test external matchfinder success scenarios */
|
||||
for (testCaseId = 0; testCaseId < EMF_numSuccessCases; testCaseId++) {
|
||||
size_t res;
|
||||
externalMatchState = EMF_successCases[testCaseId];
|
||||
ZSTD_CCtx_reset(zc, ZSTD_reset_session_only);
|
||||
CHECK_Z(ZSTD_CCtx_setParameter(zc, ZSTD_c_enableMatchFinderFallback, enableFallback));
|
||||
res = ZSTD_compress2(zc, dstBuf, dstBufSize, CNBuffer, CNBufferSize);
|
||||
CHECK(ZSTD_isError(res), "EMF: Compression error: %s", ZSTD_getErrorName(res));
|
||||
CHECK_Z(ZSTD_decompress(checkBuf, checkBufSize, dstBuf, res));
|
||||
CHECK(memcmp(CNBuffer, checkBuf, CNBufferSize) != 0, "EMF: Corruption!");
|
||||
}
|
||||
|
||||
/* Test external matchfinder failure scenarios */
|
||||
for (testCaseId = 0; testCaseId < EMF_numFailureCases; testCaseId++) {
|
||||
size_t res;
|
||||
externalMatchState = EMF_failureCases[testCaseId];
|
||||
ZSTD_CCtx_reset(zc, ZSTD_reset_session_only);
|
||||
CHECK_Z(ZSTD_CCtx_setParameter(zc, ZSTD_c_enableMatchFinderFallback, enableFallback));
|
||||
res = ZSTD_compress2(zc, dstBuf, dstBufSize, CNBuffer, CNBufferSize);
|
||||
if (enableFallback) {
|
||||
CHECK_Z(ZSTD_decompress(checkBuf, checkBufSize, dstBuf, res));
|
||||
CHECK(memcmp(CNBuffer, checkBuf, CNBufferSize) != 0, "EMF: Corruption!");
|
||||
} else {
|
||||
CHECK(!ZSTD_isError(res), "EMF: Should have raised an error!");
|
||||
CHECK(
|
||||
ZSTD_getErrorCode(res) != ZSTD_error_externalMatchFinder_failed,
|
||||
"EMF: Wrong error code: %s", ZSTD_getErrorName(res)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/* Test compression with external matchfinder + empty src buffer */
|
||||
{
|
||||
size_t res;
|
||||
externalMatchState = EMF_ZERO_SEQS;
|
||||
ZSTD_CCtx_reset(zc, ZSTD_reset_session_only);
|
||||
CHECK_Z(ZSTD_CCtx_setParameter(zc, ZSTD_c_enableMatchFinderFallback, enableFallback));
|
||||
res = ZSTD_compress2(zc, dstBuf, dstBufSize, CNBuffer, 0);
|
||||
CHECK(ZSTD_isError(res), "EMF: Compression error: %s", ZSTD_getErrorName(res));
|
||||
CHECK(ZSTD_decompress(checkBuf, checkBufSize, dstBuf, res) != 0, "EMF: Empty src round trip failed!");
|
||||
}
|
||||
}
|
||||
|
||||
/* Test that reset clears the external matchfinder */
|
||||
ZSTD_CCtx_reset(zc, ZSTD_reset_session_and_parameters);
|
||||
externalMatchState = EMF_BIG_ERROR; /* ensure zstd will fail if the matchfinder wasn't cleared */
|
||||
CHECK_Z(ZSTD_CCtx_setParameter(zc, ZSTD_c_enableMatchFinderFallback, 0));
|
||||
CHECK_Z(ZSTD_compress2(zc, dstBuf, dstBufSize, CNBuffer, CNBufferSize));
|
||||
|
||||
free(dstBuf);
|
||||
free(checkBuf);
|
||||
}
|
||||
DISPLAYLEVEL(3, "OK \n");
|
||||
|
||||
_end:
|
||||
FUZ_freeDictionary(dictionary);
|
||||
ZSTD_freeCStream(zc);
|
||||
|
||||
Reference in New Issue
Block a user