2017-10-26 20:41:47 +00:00
|
|
|
/*
|
2020-09-18 19:38:36 +00:00
|
|
|
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
|
2017-10-26 20:41:47 +00:00
|
|
|
* 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).
|
2020-09-18 19:38:36 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2017-10-26 20:41:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "zstd_ldm.h"
|
|
|
|
|
2020-09-18 19:38:36 +00:00
|
|
|
#include "../common/debug.h"
|
2017-10-26 20:41:47 +00:00
|
|
|
#include "zstd_fast.h" /* ZSTD_fillHashTable() */
|
|
|
|
#include "zstd_double_fast.h" /* ZSTD_fillDoubleHashTable() */
|
|
|
|
|
|
|
|
#define LDM_BUCKET_SIZE_LOG 3
|
|
|
|
#define LDM_MIN_MATCH_LENGTH 64
|
|
|
|
#define LDM_HASH_RLOG 7
|
|
|
|
#define LDM_HASH_CHAR_OFFSET 10
|
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
void ZSTD_ldm_adjustParameters(ldmParams_t* params,
|
|
|
|
ZSTD_compressionParameters const* cParams)
|
2017-10-26 20:41:47 +00:00
|
|
|
{
|
2019-01-04 00:30:03 +00:00
|
|
|
params->windowLog = cParams->windowLog;
|
2017-10-26 20:41:47 +00:00
|
|
|
ZSTD_STATIC_ASSERT(LDM_BUCKET_SIZE_LOG <= ZSTD_LDM_BUCKETSIZELOG_MAX);
|
2018-05-15 17:45:22 +00:00
|
|
|
DEBUGLOG(4, "ZSTD_ldm_adjustParameters");
|
|
|
|
if (!params->bucketSizeLog) params->bucketSizeLog = LDM_BUCKET_SIZE_LOG;
|
|
|
|
if (!params->minMatchLength) params->minMatchLength = LDM_MIN_MATCH_LENGTH;
|
2017-10-26 20:41:47 +00:00
|
|
|
if (params->hashLog == 0) {
|
2019-01-04 00:30:03 +00:00
|
|
|
params->hashLog = MAX(ZSTD_HASHLOG_MIN, params->windowLog - LDM_HASH_RLOG);
|
2017-10-26 20:41:47 +00:00
|
|
|
assert(params->hashLog <= ZSTD_HASHLOG_MAX);
|
|
|
|
}
|
2019-01-04 00:30:03 +00:00
|
|
|
if (params->hashRateLog == 0) {
|
|
|
|
params->hashRateLog = params->windowLog < params->hashLog
|
|
|
|
? 0
|
|
|
|
: params->windowLog - params->hashLog;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
params->bucketSizeLog = MIN(params->bucketSizeLog, params->hashLog);
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
size_t ZSTD_ldm_getTableSize(ldmParams_t params)
|
|
|
|
{
|
|
|
|
size_t const ldmHSize = ((size_t)1) << params.hashLog;
|
|
|
|
size_t const ldmBucketSizeLog = MIN(params.bucketSizeLog, params.hashLog);
|
2019-11-10 04:31:00 +00:00
|
|
|
size_t const ldmBucketSize = ((size_t)1) << (params.hashLog - ldmBucketSizeLog);
|
|
|
|
size_t const totalSize = ZSTD_cwksp_alloc_size(ldmBucketSize)
|
|
|
|
+ ZSTD_cwksp_alloc_size(ldmHSize * sizeof(ldmEntry_t));
|
2018-05-15 17:45:22 +00:00
|
|
|
return params.enableLdm ? totalSize : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZSTD_ldm_getMaxNbSeq(ldmParams_t params, size_t maxChunkSize)
|
|
|
|
{
|
|
|
|
return params.enableLdm ? (maxChunkSize / params.minMatchLength) : 0;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** ZSTD_ldm_getSmallHash() :
|
|
|
|
* numBits should be <= 32
|
|
|
|
* If numBits==0, returns 0.
|
|
|
|
* @return : the most significant numBits of value. */
|
|
|
|
static U32 ZSTD_ldm_getSmallHash(U64 value, U32 numBits)
|
|
|
|
{
|
|
|
|
assert(numBits <= 32);
|
|
|
|
return numBits == 0 ? 0 : (U32)(value >> (64 - numBits));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ZSTD_ldm_getChecksum() :
|
|
|
|
* numBitsToDiscard should be <= 32
|
|
|
|
* @return : the next most significant 32 bits after numBitsToDiscard */
|
|
|
|
static U32 ZSTD_ldm_getChecksum(U64 hash, U32 numBitsToDiscard)
|
|
|
|
{
|
|
|
|
assert(numBitsToDiscard <= 32);
|
|
|
|
return (hash >> (64 - 32 - numBitsToDiscard)) & 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ZSTD_ldm_getTag() ;
|
|
|
|
* Given the hash, returns the most significant numTagBits bits
|
|
|
|
* after (32 + hbits) bits.
|
|
|
|
*
|
|
|
|
* If there are not enough bits remaining, return the last
|
|
|
|
* numTagBits bits. */
|
|
|
|
static U32 ZSTD_ldm_getTag(U64 hash, U32 hbits, U32 numTagBits)
|
|
|
|
{
|
|
|
|
assert(numTagBits < 32 && hbits <= 32);
|
|
|
|
if (32 - hbits < numTagBits) {
|
|
|
|
return hash & (((U32)1 << numTagBits) - 1);
|
|
|
|
} else {
|
|
|
|
return (hash >> (32 - hbits - numTagBits)) & (((U32)1 << numTagBits) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ZSTD_ldm_getBucket() :
|
|
|
|
* Returns a pointer to the start of the bucket associated with hash. */
|
|
|
|
static ldmEntry_t* ZSTD_ldm_getBucket(
|
|
|
|
ldmState_t* ldmState, size_t hash, ldmParams_t const ldmParams)
|
|
|
|
{
|
|
|
|
return ldmState->hashTable + (hash << ldmParams.bucketSizeLog);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ZSTD_ldm_insertEntry() :
|
|
|
|
* Insert the entry with corresponding hash into the hash table */
|
|
|
|
static void ZSTD_ldm_insertEntry(ldmState_t* ldmState,
|
|
|
|
size_t const hash, const ldmEntry_t entry,
|
|
|
|
ldmParams_t const ldmParams)
|
|
|
|
{
|
|
|
|
BYTE* const bucketOffsets = ldmState->bucketOffsets;
|
|
|
|
*(ZSTD_ldm_getBucket(ldmState, hash, ldmParams) + bucketOffsets[hash]) = entry;
|
|
|
|
bucketOffsets[hash]++;
|
|
|
|
bucketOffsets[hash] &= ((U32)1 << ldmParams.bucketSizeLog) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ZSTD_ldm_makeEntryAndInsertByTag() :
|
|
|
|
*
|
|
|
|
* Gets the small hash, checksum, and tag from the rollingHash.
|
|
|
|
*
|
2019-01-04 00:30:03 +00:00
|
|
|
* If the tag matches (1 << ldmParams.hashRateLog)-1, then
|
2017-10-26 20:41:47 +00:00
|
|
|
* creates an ldmEntry from the offset, and inserts it into the hash table.
|
|
|
|
*
|
|
|
|
* hBits is the length of the small hash, which is the most significant hBits
|
|
|
|
* of rollingHash. The checksum is the next 32 most significant bits, followed
|
2019-01-04 00:30:03 +00:00
|
|
|
* by ldmParams.hashRateLog bits that make up the tag. */
|
2017-10-26 20:41:47 +00:00
|
|
|
static void ZSTD_ldm_makeEntryAndInsertByTag(ldmState_t* ldmState,
|
|
|
|
U64 const rollingHash,
|
|
|
|
U32 const hBits,
|
|
|
|
U32 const offset,
|
|
|
|
ldmParams_t const ldmParams)
|
|
|
|
{
|
2019-01-04 00:30:03 +00:00
|
|
|
U32 const tag = ZSTD_ldm_getTag(rollingHash, hBits, ldmParams.hashRateLog);
|
|
|
|
U32 const tagMask = ((U32)1 << ldmParams.hashRateLog) - 1;
|
2017-10-26 20:41:47 +00:00
|
|
|
if (tag == tagMask) {
|
|
|
|
U32 const hash = ZSTD_ldm_getSmallHash(rollingHash, hBits);
|
|
|
|
U32 const checksum = ZSTD_ldm_getChecksum(rollingHash, hBits);
|
|
|
|
ldmEntry_t entry;
|
|
|
|
entry.offset = offset;
|
|
|
|
entry.checksum = checksum;
|
|
|
|
ZSTD_ldm_insertEntry(ldmState, hash, entry, ldmParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ZSTD_ldm_countBackwardsMatch() :
|
|
|
|
* Returns the number of bytes that match backwards before pIn and pMatch.
|
|
|
|
*
|
|
|
|
* We count only bytes where pMatch >= pBase and pIn >= pAnchor. */
|
|
|
|
static size_t ZSTD_ldm_countBackwardsMatch(
|
|
|
|
const BYTE* pIn, const BYTE* pAnchor,
|
2021-01-08 10:21:43 +00:00
|
|
|
const BYTE* pMatch, const BYTE* pMatchBase)
|
2017-10-26 20:41:47 +00:00
|
|
|
{
|
|
|
|
size_t matchLength = 0;
|
2021-01-08 10:21:43 +00:00
|
|
|
while (pIn > pAnchor && pMatch > pMatchBase && pIn[-1] == pMatch[-1]) {
|
2017-10-26 20:41:47 +00:00
|
|
|
pIn--;
|
|
|
|
pMatch--;
|
|
|
|
matchLength++;
|
|
|
|
}
|
|
|
|
return matchLength;
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:21:43 +00:00
|
|
|
/** ZSTD_ldm_countBackwardsMatch_2segments() :
|
|
|
|
* Returns the number of bytes that match backwards from pMatch,
|
|
|
|
* even with the backwards match spanning 2 different segments.
|
|
|
|
*
|
|
|
|
* On reaching `pMatchBase`, start counting from mEnd */
|
|
|
|
static size_t ZSTD_ldm_countBackwardsMatch_2segments(
|
|
|
|
const BYTE* pIn, const BYTE* pAnchor,
|
|
|
|
const BYTE* pMatch, const BYTE* pMatchBase,
|
|
|
|
const BYTE* pExtDictStart, const BYTE* pExtDictEnd)
|
|
|
|
{
|
|
|
|
size_t matchLength = ZSTD_ldm_countBackwardsMatch(pIn, pAnchor, pMatch, pMatchBase);
|
|
|
|
if (pMatch - matchLength != pMatchBase || pMatchBase == pExtDictStart) {
|
|
|
|
/* If backwards match is entirely in the extDict or prefix, immediately return */
|
|
|
|
return matchLength;
|
|
|
|
}
|
|
|
|
DEBUGLOG(7, "ZSTD_ldm_countBackwardsMatch_2segments: found 2-parts backwards match (length in prefix==%zu)", matchLength);
|
|
|
|
matchLength += ZSTD_ldm_countBackwardsMatch(pIn - matchLength, pAnchor, pExtDictEnd, pExtDictStart);
|
|
|
|
DEBUGLOG(7, "final backwards match length = %zu", matchLength);
|
|
|
|
return matchLength;
|
|
|
|
}
|
|
|
|
|
2017-10-26 20:41:47 +00:00
|
|
|
/** ZSTD_ldm_fillFastTables() :
|
|
|
|
*
|
|
|
|
* Fills the relevant tables for the ZSTD_fast and ZSTD_dfast strategies.
|
|
|
|
* This is similar to ZSTD_loadDictionaryContent.
|
|
|
|
*
|
|
|
|
* The tables for the other strategies are filled within their
|
|
|
|
* block compressors. */
|
2018-05-15 17:45:22 +00:00
|
|
|
static size_t ZSTD_ldm_fillFastTables(ZSTD_matchState_t* ms,
|
|
|
|
void const* end)
|
2017-10-26 20:41:47 +00:00
|
|
|
{
|
|
|
|
const BYTE* const iend = (const BYTE*)end;
|
|
|
|
|
2019-01-04 00:30:03 +00:00
|
|
|
switch(ms->cParams.strategy)
|
2017-10-26 20:41:47 +00:00
|
|
|
{
|
|
|
|
case ZSTD_fast:
|
2019-01-04 00:30:03 +00:00
|
|
|
ZSTD_fillHashTable(ms, iend, ZSTD_dtlm_fast);
|
2017-10-26 20:41:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ZSTD_dfast:
|
2019-01-04 00:30:03 +00:00
|
|
|
ZSTD_fillDoubleHashTable(ms, iend, ZSTD_dtlm_fast);
|
2017-10-26 20:41:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ZSTD_greedy:
|
|
|
|
case ZSTD_lazy:
|
|
|
|
case ZSTD_lazy2:
|
|
|
|
case ZSTD_btlazy2:
|
|
|
|
case ZSTD_btopt:
|
|
|
|
case ZSTD_btultra:
|
2019-01-04 00:30:03 +00:00
|
|
|
case ZSTD_btultra2:
|
2017-10-26 20:41:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0); /* not possible : not a valid strategy id */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ZSTD_ldm_fillLdmHashTable() :
|
|
|
|
*
|
|
|
|
* Fills hashTable from (lastHashed + 1) to iend (non-inclusive).
|
|
|
|
* lastHash is the rolling hash that corresponds to lastHashed.
|
|
|
|
*
|
|
|
|
* Returns the rolling hash corresponding to position iend-1. */
|
|
|
|
static U64 ZSTD_ldm_fillLdmHashTable(ldmState_t* state,
|
|
|
|
U64 lastHash, const BYTE* lastHashed,
|
|
|
|
const BYTE* iend, const BYTE* base,
|
|
|
|
U32 hBits, ldmParams_t const ldmParams)
|
|
|
|
{
|
|
|
|
U64 rollingHash = lastHash;
|
|
|
|
const BYTE* cur = lastHashed + 1;
|
|
|
|
|
|
|
|
while (cur < iend) {
|
2019-01-04 00:30:03 +00:00
|
|
|
rollingHash = ZSTD_rollingHash_rotate(rollingHash, cur[-1],
|
|
|
|
cur[ldmParams.minMatchLength-1],
|
|
|
|
state->hashPower);
|
2017-10-26 20:41:47 +00:00
|
|
|
ZSTD_ldm_makeEntryAndInsertByTag(state,
|
|
|
|
rollingHash, hBits,
|
|
|
|
(U32)(cur - base), ldmParams);
|
|
|
|
++cur;
|
|
|
|
}
|
|
|
|
return rollingHash;
|
|
|
|
}
|
|
|
|
|
2020-09-18 19:38:36 +00:00
|
|
|
void ZSTD_ldm_fillHashTable(
|
|
|
|
ldmState_t* state, const BYTE* ip,
|
|
|
|
const BYTE* iend, ldmParams_t const* params)
|
|
|
|
{
|
|
|
|
DEBUGLOG(5, "ZSTD_ldm_fillHashTable");
|
|
|
|
if ((size_t)(iend - ip) >= params->minMatchLength) {
|
|
|
|
U64 startingHash = ZSTD_rollingHash_compute(ip, params->minMatchLength);
|
|
|
|
ZSTD_ldm_fillLdmHashTable(
|
|
|
|
state, startingHash, ip, iend - params->minMatchLength, state->window.base,
|
|
|
|
params->hashLog - params->bucketSizeLog,
|
|
|
|
*params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 20:41:47 +00:00
|
|
|
|
|
|
|
/** ZSTD_ldm_limitTableUpdate() :
|
|
|
|
*
|
|
|
|
* Sets cctx->nextToUpdate to a position corresponding closer to anchor
|
|
|
|
* if it is far way
|
|
|
|
* (after a long match, only update tables a limited amount). */
|
2018-05-15 17:45:22 +00:00
|
|
|
static void ZSTD_ldm_limitTableUpdate(ZSTD_matchState_t* ms, const BYTE* anchor)
|
2017-10-26 20:41:47 +00:00
|
|
|
{
|
2021-01-08 10:21:43 +00:00
|
|
|
U32 const curr = (U32)(anchor - ms->window.base);
|
|
|
|
if (curr > ms->nextToUpdate + 1024) {
|
2018-05-15 17:45:22 +00:00
|
|
|
ms->nextToUpdate =
|
2021-01-08 10:21:43 +00:00
|
|
|
curr - MIN(512, curr - ms->nextToUpdate - 1024);
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
static size_t ZSTD_ldm_generateSequences_internal(
|
|
|
|
ldmState_t* ldmState, rawSeqStore_t* rawSeqStore,
|
|
|
|
ldmParams_t const* params, void const* src, size_t srcSize)
|
2017-10-26 20:41:47 +00:00
|
|
|
{
|
2018-05-15 17:45:22 +00:00
|
|
|
/* 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;
|
|
|
|
U32 const ldmBucketSize = 1U << params->bucketSizeLog;
|
2019-01-04 00:30:03 +00:00
|
|
|
U32 const hashRateLog = params->hashRateLog;
|
|
|
|
U32 const ldmTagMask = (1U << params->hashRateLog) - 1;
|
2018-05-15 17:45:22 +00:00
|
|
|
/* Prefix and extDict parameters */
|
|
|
|
U32 const dictLimit = ldmState->window.dictLimit;
|
|
|
|
U32 const lowestIndex = extDict ? ldmState->window.lowLimit : dictLimit;
|
|
|
|
BYTE const* const base = ldmState->window.base;
|
|
|
|
BYTE const* const dictBase = extDict ? ldmState->window.dictBase : NULL;
|
|
|
|
BYTE const* const dictStart = extDict ? dictBase + lowestIndex : NULL;
|
|
|
|
BYTE const* const dictEnd = extDict ? dictBase + dictLimit : NULL;
|
|
|
|
BYTE const* const lowPrefixPtr = base + dictLimit;
|
|
|
|
/* Input bounds */
|
|
|
|
BYTE const* const istart = (BYTE const*)src;
|
|
|
|
BYTE const* const iend = istart + srcSize;
|
|
|
|
BYTE const* const ilimit = iend - MAX(minMatchLength, HASH_READ_SIZE);
|
|
|
|
/* Input positions */
|
|
|
|
BYTE const* anchor = istart;
|
|
|
|
BYTE const* ip = istart;
|
|
|
|
/* Rolling hash */
|
|
|
|
BYTE const* lastHashed = NULL;
|
2017-10-26 20:41:47 +00:00
|
|
|
U64 rollingHash = 0;
|
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
while (ip <= ilimit) {
|
2017-10-26 20:41:47 +00:00
|
|
|
size_t mLength;
|
2021-01-08 10:21:43 +00:00
|
|
|
U32 const curr = (U32)(ip - base);
|
2017-10-26 20:41:47 +00:00
|
|
|
size_t forwardMatchLength = 0, backwardMatchLength = 0;
|
|
|
|
ldmEntry_t* bestEntry = NULL;
|
|
|
|
if (ip != istart) {
|
2019-01-04 00:30:03 +00:00
|
|
|
rollingHash = ZSTD_rollingHash_rotate(rollingHash, lastHashed[0],
|
|
|
|
lastHashed[minMatchLength],
|
|
|
|
hashPower);
|
2017-10-26 20:41:47 +00:00
|
|
|
} else {
|
2019-01-04 00:30:03 +00:00
|
|
|
rollingHash = ZSTD_rollingHash_compute(ip, minMatchLength);
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
lastHashed = ip;
|
|
|
|
|
|
|
|
/* Do not insert and do not look for a match */
|
2019-01-04 00:30:03 +00:00
|
|
|
if (ZSTD_ldm_getTag(rollingHash, hBits, hashRateLog) != ldmTagMask) {
|
2017-10-26 20:41:47 +00:00
|
|
|
ip++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the best entry and compute the match lengths */
|
|
|
|
{
|
|
|
|
ldmEntry_t* const bucket =
|
|
|
|
ZSTD_ldm_getBucket(ldmState,
|
|
|
|
ZSTD_ldm_getSmallHash(rollingHash, hBits),
|
2018-05-15 17:45:22 +00:00
|
|
|
*params);
|
2017-10-26 20:41:47 +00:00
|
|
|
ldmEntry_t* cur;
|
|
|
|
size_t bestMatchLength = 0;
|
|
|
|
U32 const checksum = ZSTD_ldm_getChecksum(rollingHash, hBits);
|
|
|
|
|
|
|
|
for (cur = bucket; cur < bucket + ldmBucketSize; ++cur) {
|
|
|
|
size_t curForwardMatchLength, curBackwardMatchLength,
|
|
|
|
curTotalMatchLength;
|
|
|
|
if (cur->checksum != checksum || cur->offset <= lowestIndex) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
if (extDict) {
|
|
|
|
BYTE const* const curMatchBase =
|
|
|
|
cur->offset < dictLimit ? dictBase : base;
|
|
|
|
BYTE const* const pMatch = curMatchBase + cur->offset;
|
|
|
|
BYTE const* const matchEnd =
|
|
|
|
cur->offset < dictLimit ? dictEnd : iend;
|
|
|
|
BYTE const* const lowMatchPtr =
|
|
|
|
cur->offset < dictLimit ? dictStart : lowPrefixPtr;
|
|
|
|
|
|
|
|
curForwardMatchLength = ZSTD_count_2segments(
|
|
|
|
ip, pMatch, iend,
|
|
|
|
matchEnd, lowPrefixPtr);
|
|
|
|
if (curForwardMatchLength < minMatchLength) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
curBackwardMatchLength =
|
2021-01-08 10:21:43 +00:00
|
|
|
ZSTD_ldm_countBackwardsMatch_2segments(ip, anchor,
|
|
|
|
pMatch, lowMatchPtr,
|
|
|
|
dictStart, dictEnd);
|
2018-05-15 17:45:22 +00:00
|
|
|
curTotalMatchLength = curForwardMatchLength +
|
|
|
|
curBackwardMatchLength;
|
|
|
|
} else { /* !extDict */
|
|
|
|
BYTE const* const pMatch = base + cur->offset;
|
|
|
|
curForwardMatchLength = ZSTD_count(ip, pMatch, iend);
|
|
|
|
if (curForwardMatchLength < minMatchLength) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
curBackwardMatchLength =
|
|
|
|
ZSTD_ldm_countBackwardsMatch(ip, anchor, pMatch,
|
|
|
|
lowPrefixPtr);
|
|
|
|
curTotalMatchLength = curForwardMatchLength +
|
|
|
|
curBackwardMatchLength;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (curTotalMatchLength > bestMatchLength) {
|
|
|
|
bestMatchLength = curTotalMatchLength;
|
|
|
|
forwardMatchLength = curForwardMatchLength;
|
|
|
|
backwardMatchLength = curBackwardMatchLength;
|
|
|
|
bestEntry = cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No match found -- continue searching */
|
|
|
|
if (bestEntry == NULL) {
|
|
|
|
ZSTD_ldm_makeEntryAndInsertByTag(ldmState, rollingHash,
|
2021-01-08 10:21:43 +00:00
|
|
|
hBits, curr,
|
2018-05-15 17:45:22 +00:00
|
|
|
*params);
|
2017-10-26 20:41:47 +00:00
|
|
|
ip++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Match found */
|
|
|
|
mLength = forwardMatchLength + backwardMatchLength;
|
|
|
|
ip -= backwardMatchLength;
|
|
|
|
|
|
|
|
{
|
2018-05-15 17:45:22 +00:00
|
|
|
/* Store the sequence:
|
2021-01-08 10:21:43 +00:00
|
|
|
* ip = curr - backwardMatchLength
|
2018-05-15 17:45:22 +00:00
|
|
|
* The match is at (bestEntry->offset - backwardMatchLength)
|
|
|
|
*/
|
2017-10-26 20:41:47 +00:00
|
|
|
U32 const matchIndex = bestEntry->offset;
|
2021-01-08 10:21:43 +00:00
|
|
|
U32 const offset = curr - matchIndex;
|
2018-05-15 17:45:22 +00:00
|
|
|
rawSeq* const seq = rawSeqStore->seq + rawSeqStore->size;
|
|
|
|
|
|
|
|
/* 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++;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert the current entry into the hash table */
|
|
|
|
ZSTD_ldm_makeEntryAndInsertByTag(ldmState, rollingHash, hBits,
|
|
|
|
(U32)(lastHashed - base),
|
2018-05-15 17:45:22 +00:00
|
|
|
*params);
|
2017-10-26 20:41:47 +00:00
|
|
|
|
|
|
|
assert(ip + backwardMatchLength == lastHashed);
|
|
|
|
|
|
|
|
/* Fill the hash table from lastHashed+1 to ip+mLength*/
|
|
|
|
/* Heuristic: don't need to fill the entire table at end of block */
|
2018-05-15 17:45:22 +00:00
|
|
|
if (ip + mLength <= ilimit) {
|
2017-10-26 20:41:47 +00:00
|
|
|
rollingHash = ZSTD_ldm_fillLdmHashTable(
|
|
|
|
ldmState, rollingHash, lastHashed,
|
2018-05-15 17:45:22 +00:00
|
|
|
ip + mLength, base, hBits, *params);
|
2017-10-26 20:41:47 +00:00
|
|
|
lastHashed = ip + mLength - 1;
|
|
|
|
}
|
|
|
|
ip += mLength;
|
|
|
|
anchor = ip;
|
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
return iend - anchor;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
/*! ZSTD_ldm_reduceTable() :
|
|
|
|
* reduce table indexes by `reducerValue` */
|
|
|
|
static void ZSTD_ldm_reduceTable(ldmEntry_t* const table, U32 const size,
|
|
|
|
U32 const reducerValue)
|
2017-10-26 20:41:47 +00:00
|
|
|
{
|
2018-05-15 17:45:22 +00:00
|
|
|
U32 u;
|
|
|
|
for (u = 0; u < size; u++) {
|
|
|
|
if (table[u].offset < reducerValue) table[u].offset = 0;
|
|
|
|
else table[u].offset -= reducerValue;
|
|
|
|
}
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
size_t ZSTD_ldm_generateSequences(
|
|
|
|
ldmState_t* ldmState, rawSeqStore_t* sequences,
|
|
|
|
ldmParams_t const* params, void const* src, size_t srcSize)
|
2017-10-26 20:41:47 +00:00
|
|
|
{
|
2018-05-15 17:45:22 +00:00
|
|
|
U32 const maxDist = 1U << params->windowLog;
|
|
|
|
BYTE const* const istart = (BYTE const*)src;
|
|
|
|
BYTE const* const iend = istart + srcSize;
|
|
|
|
size_t const kMaxChunkSize = 1 << 20;
|
|
|
|
size_t const nbChunks = (srcSize / kMaxChunkSize) + ((srcSize % kMaxChunkSize) != 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);
|
|
|
|
/* The input could be very large (in zstdmt), so it must be broken up into
|
2019-04-18 09:53:29 +00:00
|
|
|
* chunks to enforce the maximum distance and handle overflow correction.
|
2018-05-15 17:45:22 +00:00
|
|
|
*/
|
|
|
|
assert(sequences->pos <= sequences->size);
|
|
|
|
assert(sequences->size <= sequences->capacity);
|
|
|
|
for (chunk = 0; chunk < nbChunks && sequences->size < sequences->capacity; ++chunk) {
|
|
|
|
BYTE const* const chunkStart = istart + chunk * kMaxChunkSize;
|
|
|
|
size_t const remaining = (size_t)(iend - chunkStart);
|
|
|
|
BYTE const *const chunkEnd =
|
|
|
|
(remaining < kMaxChunkSize) ? iend : chunkStart + kMaxChunkSize;
|
|
|
|
size_t const chunkSize = chunkEnd - chunkStart;
|
|
|
|
size_t newLeftoverSize;
|
|
|
|
size_t const prevSize = sequences->size;
|
|
|
|
|
|
|
|
assert(chunkStart < iend);
|
|
|
|
/* 1. Perform overflow correction if necessary. */
|
|
|
|
if (ZSTD_window_needOverflowCorrection(ldmState->window, chunkEnd)) {
|
|
|
|
U32 const ldmHSize = 1U << params->hashLog;
|
|
|
|
U32 const correction = ZSTD_window_correctOverflow(
|
2019-07-20 18:47:07 +00:00
|
|
|
&ldmState->window, /* cycleLog */ 0, maxDist, chunkStart);
|
2018-05-15 17:45:22 +00:00
|
|
|
ZSTD_ldm_reduceTable(ldmState->hashTable, ldmHSize, correction);
|
2020-09-18 19:38:36 +00:00
|
|
|
/* invalidate dictionaries on overflow correction */
|
|
|
|
ldmState->loadedDictEnd = 0;
|
2018-05-15 17:45:22 +00:00
|
|
|
}
|
|
|
|
/* 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
|
|
|
|
* the offset against maxDist directly.
|
2020-09-18 19:38:36 +00:00
|
|
|
*
|
|
|
|
* NOTE: Because of dictionaries + sequence splitting we MUST make sure
|
|
|
|
* that any offset used is valid at the END of the sequence, since it may
|
|
|
|
* be split into two sequences. This condition holds when using
|
|
|
|
* ZSTD_window_enforceMaxDist(), but if we move to checking offsets
|
|
|
|
* against maxDist directly, we'll have to carefully handle that case.
|
2018-05-15 17:45:22 +00:00
|
|
|
*/
|
2020-09-18 19:38:36 +00:00
|
|
|
ZSTD_window_enforceMaxDist(&ldmState->window, chunkEnd, maxDist, &ldmState->loadedDictEnd, NULL);
|
2018-05-15 17:45:22 +00:00
|
|
|
/* 3. Generate the sequences for the chunk, and get newLeftoverSize. */
|
|
|
|
newLeftoverSize = ZSTD_ldm_generateSequences_internal(
|
|
|
|
ldmState, sequences, params, 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;
|
2017-10-26 20:41:47 +00:00
|
|
|
} else {
|
2018-05-15 17:45:22 +00:00
|
|
|
assert(newLeftoverSize == chunkSize);
|
|
|
|
leftoverSize += chunkSize;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-26 20:41:47 +00:00
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
void ZSTD_ldm_skipSequences(rawSeqStore_t* rawSeqStore, size_t srcSize, U32 const minMatch) {
|
|
|
|
while (srcSize > 0 && rawSeqStore->pos < rawSeqStore->size) {
|
|
|
|
rawSeq* seq = rawSeqStore->seq + rawSeqStore->pos;
|
|
|
|
if (srcSize <= seq->litLength) {
|
|
|
|
/* Skip past srcSize literals */
|
|
|
|
seq->litLength -= (U32)srcSize;
|
|
|
|
return;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
srcSize -= seq->litLength;
|
|
|
|
seq->litLength = 0;
|
|
|
|
if (srcSize < seq->matchLength) {
|
|
|
|
/* Skip past the first srcSize of the match */
|
|
|
|
seq->matchLength -= (U32)srcSize;
|
|
|
|
if (seq->matchLength < minMatch) {
|
|
|
|
/* The match is too short, omit it */
|
|
|
|
if (rawSeqStore->pos + 1 < rawSeqStore->size) {
|
|
|
|
seq[1].litLength += seq[0].matchLength;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
rawSeqStore->pos++;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
return;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
srcSize -= seq->matchLength;
|
|
|
|
seq->matchLength = 0;
|
|
|
|
rawSeqStore->pos++;
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 20:41:47 +00:00
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
/**
|
|
|
|
* If the sequence length is longer than remaining then the sequence is split
|
|
|
|
* between this block and the next.
|
|
|
|
*
|
|
|
|
* 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(rawSeqStore_t* rawSeqStore,
|
|
|
|
U32 const remaining, U32 const minMatch)
|
|
|
|
{
|
|
|
|
rawSeq sequence = rawSeqStore->seq[rawSeqStore->pos];
|
|
|
|
assert(sequence.offset > 0);
|
|
|
|
/* Likely: No partial sequence */
|
|
|
|
if (remaining >= sequence.litLength + sequence.matchLength) {
|
|
|
|
rawSeqStore->pos++;
|
|
|
|
return sequence;
|
|
|
|
}
|
|
|
|
/* Cut the sequence short (offset == 0 ==> rest is literals). */
|
|
|
|
if (remaining <= sequence.litLength) {
|
|
|
|
sequence.offset = 0;
|
|
|
|
} else if (remaining < sequence.litLength + sequence.matchLength) {
|
|
|
|
sequence.matchLength = remaining - sequence.litLength;
|
|
|
|
if (sequence.matchLength < minMatch) {
|
|
|
|
sequence.offset = 0;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
}
|
|
|
|
/* Skip past `remaining` bytes for the future sequences. */
|
|
|
|
ZSTD_ldm_skipSequences(rawSeqStore, remaining, minMatch);
|
|
|
|
return sequence;
|
|
|
|
}
|
2017-10-26 20:41:47 +00:00
|
|
|
|
2021-01-08 10:21:43 +00:00
|
|
|
void ZSTD_ldm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t nbBytes) {
|
|
|
|
U32 currPos = (U32)(rawSeqStore->posInSequence + nbBytes);
|
|
|
|
while (currPos && rawSeqStore->pos < rawSeqStore->size) {
|
|
|
|
rawSeq currSeq = rawSeqStore->seq[rawSeqStore->pos];
|
|
|
|
if (currPos >= currSeq.litLength + currSeq.matchLength) {
|
|
|
|
currPos -= currSeq.litLength + currSeq.matchLength;
|
|
|
|
rawSeqStore->pos++;
|
|
|
|
} else {
|
|
|
|
rawSeqStore->posInSequence = currPos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (currPos == 0 || rawSeqStore->pos == rawSeqStore->size) {
|
|
|
|
rawSeqStore->posInSequence = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore,
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
2019-01-04 00:30:03 +00:00
|
|
|
void const* src, size_t srcSize)
|
2018-05-15 17:45:22 +00:00
|
|
|
{
|
2019-01-04 00:30:03 +00:00
|
|
|
const ZSTD_compressionParameters* const cParams = &ms->cParams;
|
|
|
|
unsigned const minMatch = cParams->minMatch;
|
2018-05-15 17:45:22 +00:00
|
|
|
ZSTD_blockCompressor const blockCompressor =
|
2019-01-04 00:30:03 +00:00
|
|
|
ZSTD_selectBlockCompressor(cParams->strategy, ZSTD_matchState_dictMode(ms));
|
2018-05-15 17:45:22 +00:00
|
|
|
/* Input bounds */
|
|
|
|
BYTE const* const istart = (BYTE const*)src;
|
|
|
|
BYTE const* const iend = istart + srcSize;
|
|
|
|
/* Input positions */
|
|
|
|
BYTE const* ip = istart;
|
|
|
|
|
2019-01-04 00:30:03 +00:00
|
|
|
DEBUGLOG(5, "ZSTD_ldm_blockCompress: srcSize=%zu", srcSize);
|
2021-01-08 10:21:43 +00:00
|
|
|
/* If using opt parser, use LDMs only as candidates rather than always accepting them */
|
|
|
|
if (cParams->strategy >= ZSTD_btopt) {
|
|
|
|
size_t lastLLSize;
|
|
|
|
ms->ldmSeqStore = rawSeqStore;
|
|
|
|
lastLLSize = blockCompressor(ms, seqStore, rep, src, srcSize);
|
|
|
|
ZSTD_ldm_skipRawSeqStoreBytes(rawSeqStore, srcSize);
|
|
|
|
return lastLLSize;
|
|
|
|
}
|
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
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;
|
2017-10-26 20:41:47 +00:00
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
assert(ip + sequence.litLength + sequence.matchLength <= iend);
|
2017-10-26 20:41:47 +00:00
|
|
|
|
2018-05-15 17:45:22 +00:00
|
|
|
/* Fill tables for block compressor */
|
|
|
|
ZSTD_ldm_limitTableUpdate(ms, ip);
|
2019-01-04 00:30:03 +00:00
|
|
|
ZSTD_ldm_fillFastTables(ms, ip);
|
2018-05-15 17:45:22 +00:00
|
|
|
/* Run the block compressor */
|
2020-09-18 19:38:36 +00:00
|
|
|
DEBUGLOG(5, "pos %u : calling block compressor on segment of size %u", (unsigned)(ip-istart), sequence.litLength);
|
2018-05-15 17:45:22 +00:00
|
|
|
{
|
|
|
|
size_t const newLitLength =
|
2019-01-04 00:30:03 +00:00
|
|
|
blockCompressor(ms, seqStore, rep, ip, sequence.litLength);
|
2018-05-15 17:45:22 +00:00
|
|
|
ip += sequence.litLength;
|
|
|
|
/* Update the repcodes */
|
2017-10-26 20:41:47 +00:00
|
|
|
for (i = ZSTD_REP_NUM - 1; i > 0; i--)
|
2018-05-15 17:45:22 +00:00
|
|
|
rep[i] = rep[i-1];
|
|
|
|
rep[0] = sequence.offset;
|
|
|
|
/* Store the sequence */
|
2019-11-10 04:31:00 +00:00
|
|
|
ZSTD_storeSeq(seqStore, newLitLength, ip - newLitLength, iend,
|
2018-05-15 17:45:22 +00:00
|
|
|
sequence.offset + ZSTD_REP_MOVE,
|
|
|
|
sequence.matchLength - MINMATCH);
|
|
|
|
ip += sequence.matchLength;
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-15 17:45:22 +00:00
|
|
|
/* Fill the tables for the block compressor */
|
|
|
|
ZSTD_ldm_limitTableUpdate(ms, ip);
|
2019-01-04 00:30:03 +00:00
|
|
|
ZSTD_ldm_fillFastTables(ms, ip);
|
2018-05-15 17:45:22 +00:00
|
|
|
/* Compress the last literals */
|
2019-01-04 00:30:03 +00:00
|
|
|
return blockCompressor(ms, seqStore, rep, ip, iend - ip);
|
2017-10-26 20:41:47 +00:00
|
|
|
}
|