1
0
mirror of https://github.com/facebook/zstd.git synced 2025-12-01 23:57:51 +03:00

constify MatchState* parameter when possible

turns out, it's possible to constify MatchState* parameter
in some parts of the binary tree algorithm,
making it a pure read-only parameter,
as opposed to a mutable state.

This is supposed to be helpful for both maintenance and the compiler.
This commit is contained in:
Yann Collet
2021-09-23 08:27:44 -07:00
parent c7afbec4c1
commit fa2a4d77c7
3 changed files with 16 additions and 20 deletions

View File

@@ -61,7 +61,7 @@ ZSTD_updateDUBT(ZSTD_matchState_t* ms,
* assumption : curr >= btlow == (curr - btmask) * assumption : curr >= btlow == (curr - btmask)
* doesn't fail */ * doesn't fail */
static void static void
ZSTD_insertDUBT1(ZSTD_matchState_t* ms, ZSTD_insertDUBT1(const ZSTD_matchState_t* ms,
U32 curr, const BYTE* inputEnd, U32 curr, const BYTE* inputEnd,
U32 nbCompares, U32 btLow, U32 nbCompares, U32 btLow,
const ZSTD_dictMode_e dictMode) const ZSTD_dictMode_e dictMode)
@@ -151,7 +151,7 @@ ZSTD_insertDUBT1(ZSTD_matchState_t* ms,
static size_t static size_t
ZSTD_DUBT_findBetterDictMatch ( ZSTD_DUBT_findBetterDictMatch (
ZSTD_matchState_t* ms, const ZSTD_matchState_t* ms,
const BYTE* const ip, const BYTE* const iend, const BYTE* const ip, const BYTE* const iend,
size_t* offsetPtr, size_t* offsetPtr,
size_t bestLength, size_t bestLength,

View File

@@ -366,7 +366,7 @@ MEM_STATIC U32 ZSTD_readMINMATCH(const void* memPtr, U32 length)
/* Update hashTable3 up to ip (excluded) /* Update hashTable3 up to ip (excluded)
Assumption : always within prefix (i.e. not within extDict) */ Assumption : always within prefix (i.e. not within extDict) */
static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_matchState_t* ms, static U32 ZSTD_insertAndFindFirstIndexHash3 (const ZSTD_matchState_t* ms,
U32* nextToUpdate3, U32* nextToUpdate3,
const BYTE* const ip) const BYTE* const ip)
{ {
@@ -396,7 +396,7 @@ static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_matchState_t* ms,
* @param target The target of ZSTD_updateTree_internal() - we are filling to this position * @param target The target of ZSTD_updateTree_internal() - we are filling to this position
* @return : nb of positions added */ * @return : nb of positions added */
static U32 ZSTD_insertBt1( static U32 ZSTD_insertBt1(
ZSTD_matchState_t* ms, const ZSTD_matchState_t* ms,
const BYTE* const ip, const BYTE* const iend, const BYTE* const ip, const BYTE* const iend,
U32 const target, U32 const target,
U32 const mls, const int extDict) U32 const mls, const int extDict)
@@ -421,8 +421,8 @@ static U32 ZSTD_insertBt1(
U32* smallerPtr = bt + 2*(curr&btMask); U32* smallerPtr = bt + 2*(curr&btMask);
U32* largerPtr = smallerPtr + 1; U32* largerPtr = smallerPtr + 1;
U32 dummy32; /* to be nullified at the end */ U32 dummy32; /* to be nullified at the end */
/* windowLow is based on target because we're only need positions that will be /* windowLow is based on target because
* in the window at the end of the tree update. * we only need positions that will be in the window at the end of the tree update.
*/ */
U32 const windowLow = ZSTD_getLowestMatchIndex(ms, target, cParams->windowLog); U32 const windowLow = ZSTD_getLowestMatchIndex(ms, target, cParams->windowLog);
U32 matchEndIdx = curr+8+1; U32 matchEndIdx = curr+8+1;
@@ -669,7 +669,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
return 1; return 1;
} } } } } }
/* no dictMatchState lookup: dicts don't have a populated HC3 table */ /* no dictMatchState lookup: dicts don't have a populated HC3 table */
} } /* if (mls == 3) */
hashTable[h] = curr; /* Update Hash Table */ hashTable[h] = curr; /* Update Hash Table */
@@ -706,8 +706,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
| (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */) { | (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */) {
if (dictMode == ZSTD_dictMatchState) nbCompares = 0; /* break should also skip searching dms */ if (dictMode == ZSTD_dictMatchState) nbCompares = 0; /* break should also skip searching dms */
break; /* drop, to preserve bt consistency (miss a little bit of compression) */ break; /* drop, to preserve bt consistency (miss a little bit of compression) */
} } }
}
if (match[matchLength] < ip[matchLength]) { if (match[matchLength] < ip[matchLength]) {
/* match smaller than current */ /* match smaller than current */
@@ -752,8 +751,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
if ( (matchLength > ZSTD_OPT_NUM) if ( (matchLength > ZSTD_OPT_NUM)
| (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */) { | (ip+matchLength == iLimit) /* equal : no way to know if inf or sup */) {
break; /* drop, to guarantee consistency (miss a little bit of compression) */ break; /* drop, to guarantee consistency (miss a little bit of compression) */
} } }
}
if (dictMatchIndex <= dmsBtLow) { break; } /* beyond tree size, stop the search */ if (dictMatchIndex <= dmsBtLow) { break; } /* beyond tree size, stop the search */
if (match[matchLength] < ip[matchLength]) { if (match[matchLength] < ip[matchLength]) {
@@ -763,9 +761,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
/* match is larger than current */ /* match is larger than current */
commonLengthLarger = matchLength; commonLengthLarger = matchLength;
dictMatchIndex = nextPtr[0]; dictMatchIndex = nextPtr[0];
} } } } /* if (dictMode == ZSTD_dictMatchState) */
}
}
assert(matchEndIdx > curr+8); assert(matchEndIdx > curr+8);
ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */ ms->nextToUpdate = matchEndIdx - 8; /* skip repetitive patterns */