Skip to content

Commit a8f72d2

Browse files
committed
CR fixes and better entropy for hash salt
1 parent 2e02596 commit a8f72d2

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

lib/compress/zstd_compress.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,8 @@ ZSTD_reset_matchState(ZSTD_matchState_t* ms,
19391939
* 0 when we reset a Cdict */
19401940
if(forWho == ZSTD_resetTarget_CCtx) {
19411941
ms->tagTable = (U16 *) ZSTD_cwksp_reserve_aligned_init_once(ws, tagTableSize);
1942-
ms->hashSalt = ms->hashSalt * 6364136223846793005 + 1; /* based on MUSL rand */
1942+
ms->hashSalt = ZSTD_hashPtr(&ms->hashSalt, 32, sizeof(ms->hashSalt)) << 32 |
1943+
ZSTD_hashPtr(&ms->hashSaltEntropy, 32, sizeof(ms->hashSaltEntropy));
19431944
} else {
19441945
/* When we are not salting we want to always memset the memory */
19451946
ms->tagTable = (U16 *) ZSTD_cwksp_reserve_aligned(ws, tagTableSize);

lib/compress/zstd_compress_internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ struct ZSTD_matchState_t {
228228
U32 rowHashLog; /* For row-based matchfinder: Hashlog based on nb of rows in the hashTable.*/
229229
U16* tagTable; /* For row-based matchFinder: A row-based table containing the hashes and head index. */
230230
U32 hashCache[ZSTD_ROW_HASH_CACHE_SIZE]; /* For row-based matchFinder: a cache of hashes to improve speed */
231-
U64 hashSalt;
231+
U64 hashSalt; /* For row-based matchFinder: salts the hash for re-use of tag table */
232+
U32 hashSaltEntropy; /* For row-based matchFinder: collects entropy for salt generation */
232233

233234
U32* hashTable;
234235
U32* hashTable3;
@@ -845,7 +846,7 @@ size_t ZSTD_hashPtrSalted(const void* p, U32 hBits, U32 mls, const U64 hashSalt)
845846
switch(mls)
846847
{
847848
default:
848-
case 4: return ZSTD_hash4PtrS(p, hBits, (U32)(hashSalt >> 32));
849+
case 4: return ZSTD_hash4PtrS(p, hBits, (U32)hashSalt);
849850
case 5: return ZSTD_hash5PtrS(p, hBits, hashSalt);
850851
case 6: return ZSTD_hash6PtrS(p, hBits, hashSalt);
851852
case 7: return ZSTD_hash7PtrS(p, hBits, hashSalt);

lib/compress/zstd_lazy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ FORCE_INLINE_TEMPLATE void ZSTD_row_update_internalImpl(ZSTD_matchState_t* ms,
906906
assert(hash == ZSTD_hashPtrSalted(base + updateStartIdx, hashLog + ZSTD_ROW_HASH_TAG_BITS, mls, ms->hashSalt));
907907
((BYTE*)tagRow)[pos + ZSTD_ROW_HASH_TAG_OFFSET] = hash & ZSTD_ROW_HASH_TAG_MASK;
908908
row[pos] = updateStartIdx;
909+
ms->hashSaltEntropy += hash;
909910
}
910911
}
911912

0 commit comments

Comments
 (0)