Skip to content

Commit 9477f65

Browse files
authored
Merge pull request facebook#984 from terrelln/dict-load
Load more dictionary positions into table if empty
2 parents 863b2f8 + 9a211d1 commit 9477f65

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/compress/zstd_double_fast.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ void ZSTD_fillDoubleHashTable(ZSTD_CCtx* cctx, const void* end, const U32 mls)
2121
const BYTE* const base = cctx->base;
2222
const BYTE* ip = base + cctx->nextToUpdate;
2323
const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
24-
const size_t fastHashFillStep = 3;
25-
26-
while(ip <= iend) {
27-
hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base);
28-
hashLarge[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base);
29-
ip += fastHashFillStep;
24+
const U32 fastHashFillStep = 3;
25+
26+
/* Always insert every fastHashFillStep position into the hash tables.
27+
* Insert the other positions into the large hash table if their entry
28+
* is empty.
29+
*/
30+
for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
31+
U32 const current = (U32)(ip - base);
32+
U32 i;
33+
for (i = 0; i < fastHashFillStep; ++i) {
34+
size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
35+
size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
36+
if (i == 0)
37+
hashSmall[smHash] = current + i;
38+
if (i == 0 || hashLarge[lgHash] == 0)
39+
hashLarge[lgHash] = current + i;
40+
}
3041
}
3142
}
3243

lib/compress/zstd_fast.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ void ZSTD_fillHashTable (ZSTD_CCtx* zc, const void* end, const U32 mls)
1919
const BYTE* const base = zc->base;
2020
const BYTE* ip = base + zc->nextToUpdate;
2121
const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
22-
const size_t fastHashFillStep = 3;
23-
24-
while(ip <= iend) {
25-
hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base);
26-
ip += fastHashFillStep;
22+
const U32 fastHashFillStep = 3;
23+
24+
/* Always insert every fastHashFillStep position into the hash table.
25+
* Insert the other positions if their hash entry is empty.
26+
*/
27+
for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
28+
U32 const current = (U32)(ip - base);
29+
U32 i;
30+
for (i = 0; i < fastHashFillStep; ++i) {
31+
size_t const hash = ZSTD_hashPtr(ip + i, hBits, mls);
32+
if (i == 0 || hashTable[hash] == 0)
33+
hashTable[hash] = current + i;
34+
}
2735
}
2836
}
2937

0 commit comments

Comments
 (0)