Expose reference external sequence API
* Expose the reference external sequences API for zstdmt. Allows external sequences of any length, which get split when necessary. * Reset the LDM window when the context is reset. * Store the maximum number of LDM sequences. * Sequence generation now returns the number of last literals. * Fix sequence generation to not throw out the last literals when blocks of more than 1 MB are encountered.
This commit is contained in:
+79
-41
@@ -295,12 +295,11 @@ static void ZSTD_ldm_limitTableUpdate(ZSTD_matchState_t* ms, const BYTE* anchor)
|
||||
}
|
||||
|
||||
static size_t ZSTD_ldm_generateSequences_internal(
|
||||
ldmState_t* ldmState, rawSeq* sequences,
|
||||
ldmParams_t const* params, void const* src, size_t srcSize,
|
||||
int const extDict)
|
||||
ldmState_t* ldmState, rawSeqStore_t* rawSeqStore,
|
||||
ldmParams_t const* params, void const* src, size_t srcSize)
|
||||
{
|
||||
rawSeq const* const sequencesStart = sequences;
|
||||
/* LDM parameters */
|
||||
int const extDict = ZSTD_window_hasExtDict(ldmState->window);
|
||||
U32 const minMatchLength = params->minMatchLength;
|
||||
U64 const hashPower = ldmState->hashPower;
|
||||
U32 const hBits = params->hashLog - params->bucketSizeLog;
|
||||
@@ -424,11 +423,15 @@ static size_t ZSTD_ldm_generateSequences_internal(
|
||||
*/
|
||||
U32 const matchIndex = bestEntry->offset;
|
||||
U32 const offset = current - matchIndex;
|
||||
rawSeq* const seq = rawSeqStore->seq + rawSeqStore->size;
|
||||
|
||||
sequences->litLength = (U32)(ip - anchor);
|
||||
sequences->matchLength = (U32)mLength;
|
||||
sequences->offset = offset;
|
||||
++sequences;
|
||||
/* Out of sequence storage */
|
||||
if (rawSeqStore->size == rawSeqStore->capacity)
|
||||
return ERROR(dstSize_tooSmall);
|
||||
seq->litLength = (U32)(ip - anchor);
|
||||
seq->matchLength = (U32)mLength;
|
||||
seq->offset = offset;
|
||||
rawSeqStore->size++;
|
||||
}
|
||||
|
||||
/* Insert the current entry into the hash table */
|
||||
@@ -449,8 +452,7 @@ static size_t ZSTD_ldm_generateSequences_internal(
|
||||
ip += mLength;
|
||||
anchor = ip;
|
||||
}
|
||||
/* Return the number of sequences generated */
|
||||
return sequences - sequencesStart;
|
||||
return iend - anchor;
|
||||
}
|
||||
|
||||
/*! ZSTD_ldm_reduceTable() :
|
||||
@@ -466,35 +468,44 @@ static void ZSTD_ldm_reduceTable(ldmEntry_t* const table, U32 const size,
|
||||
}
|
||||
|
||||
size_t ZSTD_ldm_generateSequences(
|
||||
ldmState_t* ldmState, rawSeq* sequences,
|
||||
ldmParams_t const* params, void const* src, size_t srcSize,
|
||||
int const extDict)
|
||||
ldmState_t* ldmState, rawSeqStore_t* sequences,
|
||||
ldmParams_t const* params, void const* src, size_t srcSize)
|
||||
{
|
||||
U32 const maxDist = 1U << params->windowLog;
|
||||
BYTE const* const istart = (BYTE const*)src;
|
||||
size_t const kMaxChunkSize = 1 << 20;
|
||||
size_t const nbChunks = (srcSize / kMaxChunkSize) + ((srcSize % kMaxChunkSize) != 0);
|
||||
size_t nbSeq = 0;
|
||||
size_t chunk;
|
||||
size_t leftoverSize = 0;
|
||||
|
||||
assert(ZSTD_CHUNKSIZE_MAX >= kMaxChunkSize);
|
||||
/* Check that ZSTD_window_update() has been called for this chunk prior
|
||||
* to passing it to this function.
|
||||
*/
|
||||
assert(ldmState->window.nextSrc >= (BYTE const*)src + srcSize);
|
||||
for (chunk = 0; chunk < nbChunks; ++chunk) {
|
||||
/* The input could be very large (in zstdmt), so it must be broken up into
|
||||
* chunks to enforce the maximmum distance and handle overflow correction.
|
||||
*/
|
||||
assert(sequences->pos <= sequences->size);
|
||||
assert(sequences->size <= sequences->capacity);
|
||||
for (chunk = 0; chunk < nbChunks && sequences->size < sequences->capacity; ++chunk) {
|
||||
size_t const chunkStart = chunk * kMaxChunkSize;
|
||||
size_t const chunkEnd = MIN(chunkStart + kMaxChunkSize, srcSize);
|
||||
size_t const chunkSize = chunkEnd - chunkStart;
|
||||
size_t newLeftoverSize;
|
||||
size_t const prevSize = sequences->size;
|
||||
|
||||
assert(chunkStart < srcSize);
|
||||
/* 1. Perform overflow correction if necessary. */
|
||||
if (ZSTD_window_needOverflowCorrection(ldmState->window)) {
|
||||
U32 const ldmHSize = 1U << params->hashLog;
|
||||
U32 const correction = ZSTD_window_correctOverflow(
|
||||
&ldmState->window, /* cycleLog */ 0, maxDist, src);
|
||||
ZSTD_ldm_reduceTable(ldmState->hashTable, ldmHSize, correction);
|
||||
}
|
||||
/* kMaxChunkSize should be small enough that we don't lose too much of
|
||||
/* 2. We enforce the maximum offset allowed.
|
||||
*
|
||||
* kMaxChunkSize should be small enough that we don't lose too much of
|
||||
* the window through early invalidation.
|
||||
* TODO: * Test the chunk size.
|
||||
* * Try invalidation after the sequence generation and test the
|
||||
@@ -502,14 +513,28 @@ size_t ZSTD_ldm_generateSequences(
|
||||
*/
|
||||
ZSTD_window_enforceMaxDist(&ldmState->window, istart + chunkEnd,
|
||||
maxDist);
|
||||
nbSeq += ZSTD_ldm_generateSequences_internal(
|
||||
ldmState, sequences + nbSeq, params, istart + chunkStart, chunkSize,
|
||||
extDict);
|
||||
/* 3. Generate the sequences for the chunk, and get newLeftoverSize. */
|
||||
newLeftoverSize = ZSTD_ldm_generateSequences_internal(
|
||||
ldmState, sequences, params, istart + chunkStart,
|
||||
chunkSize);
|
||||
if (ZSTD_isError(newLeftoverSize))
|
||||
return newLeftoverSize;
|
||||
/* 4. We add the leftover literals from previous iterations to the first
|
||||
* newly generated sequence, or add the `newLeftoverSize` if none are
|
||||
* generated.
|
||||
*/
|
||||
/* Prepend the leftover literals from the last call */
|
||||
if (prevSize < sequences->size) {
|
||||
sequences->seq[prevSize].litLength += (U32)leftoverSize;
|
||||
leftoverSize = newLeftoverSize;
|
||||
} else {
|
||||
assert(newLeftoverSize == chunkSize);
|
||||
leftoverSize += chunkSize;
|
||||
}
|
||||
}
|
||||
return nbSeq;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/**
|
||||
* If the sequence length is longer than remaining then the sequence is split
|
||||
* between this block and the next.
|
||||
@@ -517,11 +542,11 @@ size_t ZSTD_ldm_generateSequences(
|
||||
* Returns the current sequence to handle, or if the rest of the block should
|
||||
* be literals, it returns a sequence with offset == 0.
|
||||
*/
|
||||
static rawSeq maybeSplitSequence(rawSeq* sequences, size_t* nbSeq,
|
||||
size_t const seq, size_t const remaining,
|
||||
U32 const minMatch)
|
||||
static rawSeq maybeSplitSequence(rawSeqStore_t* rawSeqStore,
|
||||
U32 const remaining, U32 const minMatch)
|
||||
{
|
||||
rawSeq sequence = sequences[seq];
|
||||
size_t const pos = rawSeqStore->pos;
|
||||
rawSeq sequence = rawSeqStore->seq[rawSeqStore->pos];
|
||||
assert(sequence.offset > 0);
|
||||
/* Handle partial sequences */
|
||||
if (remaining <= sequence.litLength) {
|
||||
@@ -529,8 +554,7 @@ static rawSeq maybeSplitSequence(rawSeq* sequences, size_t* nbSeq,
|
||||
* They will become the last literals of this block.
|
||||
* The next block starts off with the remaining literals.
|
||||
*/
|
||||
sequences[seq].litLength -= remaining;
|
||||
*nbSeq = seq;
|
||||
rawSeqStore->seq[pos].litLength -= remaining;
|
||||
sequence.offset = 0;
|
||||
} else if (remaining < sequence.litLength + sequence.matchLength) {
|
||||
/* Split the match up into two sequences. One in this block, and one
|
||||
@@ -543,31 +567,38 @@ static rawSeq maybeSplitSequence(rawSeq* sequences, size_t* nbSeq,
|
||||
assert(remaining > sequence.litLength);
|
||||
assert(matchPrefix < sequence.matchLength);
|
||||
assert(matchPrefix + matchSuffix == sequence.matchLength);
|
||||
/* Update the current sequence */
|
||||
/* Update the first sequence */
|
||||
sequence.matchLength = matchPrefix;
|
||||
/* Update the next sequence when long enough, otherwise omit it. */
|
||||
/* Update the second sequence */
|
||||
if (matchSuffix >= minMatch) {
|
||||
sequences[seq].litLength = 0;
|
||||
sequences[seq].matchLength = matchSuffix;
|
||||
*nbSeq = seq;
|
||||
/* Update the second sequence, since the suffix is long enough */
|
||||
rawSeqStore->seq[pos].litLength = 0;
|
||||
rawSeqStore->seq[pos].matchLength = matchSuffix;
|
||||
} else {
|
||||
sequences[seq + 1].litLength += matchSuffix;
|
||||
*nbSeq = seq + 1;
|
||||
/* Omit the second sequence since the match suffix is too short.
|
||||
* Add to the next sequences literals (if any).
|
||||
*/
|
||||
if (pos + 1 < rawSeqStore->size)
|
||||
rawSeqStore->seq[pos + 1].litLength += matchSuffix;
|
||||
rawSeqStore->pos++; /* Consume the sequence */
|
||||
}
|
||||
if (sequence.matchLength < minMatch) {
|
||||
/* Skip the current sequence if it is too short */
|
||||
sequence.offset = 0;
|
||||
}
|
||||
} else {
|
||||
/* No partial sequence */
|
||||
rawSeqStore->pos++; /* Consume the sequence */
|
||||
}
|
||||
return sequence;
|
||||
}
|
||||
#endif
|
||||
|
||||
size_t ZSTD_ldm_blockCompress(rawSeq const* sequences, size_t nbSeq,
|
||||
size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore,
|
||||
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
||||
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize,
|
||||
int const extDict)
|
||||
{
|
||||
unsigned const minMatch = cParams->searchLength;
|
||||
ZSTD_blockCompressor const blockCompressor =
|
||||
ZSTD_selectBlockCompressor(cParams->strategy, extDict);
|
||||
BYTE const* const base = ms->window.base;
|
||||
@@ -576,15 +607,20 @@ size_t ZSTD_ldm_blockCompress(rawSeq const* sequences, size_t nbSeq,
|
||||
BYTE const* const iend = istart + srcSize;
|
||||
/* Input positions */
|
||||
BYTE const* ip = istart;
|
||||
size_t seq;
|
||||
/* Loop through each sequence and apply the block compressor to the lits */
|
||||
for (seq = 0; seq < nbSeq; ++seq) {
|
||||
rawSeq const sequence = sequences[seq];
|
||||
int i;
|
||||
|
||||
assert(rawSeqStore->pos <= rawSeqStore->size);
|
||||
assert(rawSeqStore->size <= rawSeqStore->capacity);
|
||||
/* Loop through each sequence and apply the block compressor to the lits */
|
||||
while (rawSeqStore->pos < rawSeqStore->size && ip < iend) {
|
||||
/* maybeSplitSequence updates rawSeqStore->pos */
|
||||
rawSeq const sequence = maybeSplitSequence(rawSeqStore,
|
||||
(U32)(iend - ip), minMatch);
|
||||
int i;
|
||||
/* End signal */
|
||||
if (sequence.offset == 0)
|
||||
break;
|
||||
|
||||
assert(sequence.offset <= (1U << cParams->windowLog));
|
||||
assert(ip + sequence.litLength + sequence.matchLength <= iend);
|
||||
|
||||
/* Fill tables for block compressor */
|
||||
@@ -608,8 +644,10 @@ size_t ZSTD_ldm_blockCompress(rawSeq const* sequences, size_t nbSeq,
|
||||
ip += sequence.matchLength;
|
||||
}
|
||||
}
|
||||
/* Fill the tables for the block compressor */
|
||||
ZSTD_ldm_limitTableUpdate(ms, ip);
|
||||
ZSTD_ldm_fillFastTables(ms, cParams, ip);
|
||||
/* Compress the last literals */
|
||||
{
|
||||
size_t const lastLiterals = blockCompressor(ms, seqStore, rep, cParams,
|
||||
ip, iend - ip);
|
||||
|
||||
Reference in New Issue
Block a user