/src/zstd/lib/compress/zstd_ldm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This source code is licensed under both the BSD-style license (found in the |
6 | | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
7 | | * in the COPYING file in the root directory of this source tree). |
8 | | * You may select, at your option, one of the above-listed licenses. |
9 | | */ |
10 | | |
11 | | #include "zstd_ldm.h" |
12 | | |
13 | | #include "../common/debug.h" |
14 | | #include "../common/xxhash.h" |
15 | | #include "zstd_fast.h" /* ZSTD_fillHashTable() */ |
16 | | #include "zstd_double_fast.h" /* ZSTD_fillDoubleHashTable() */ |
17 | | #include "zstd_ldm_geartab.h" |
18 | | |
19 | 121k | #define LDM_BUCKET_SIZE_LOG 3 |
20 | 0 | #define LDM_MIN_MATCH_LENGTH 64 |
21 | | #define LDM_HASH_RLOG 7 |
22 | | |
23 | | typedef struct { |
24 | | U64 rolling; |
25 | | U64 stopMask; |
26 | | } ldmRollingHashState_t; |
27 | | |
28 | | /** ZSTD_ldm_gear_init(): |
29 | | * |
30 | | * Initializes the rolling hash state such that it will honor the |
31 | | * settings in params. */ |
32 | | static void ZSTD_ldm_gear_init(ldmRollingHashState_t* state, ldmParams_t const* params) |
33 | 744k | { |
34 | 744k | unsigned maxBitsInMask = MIN(params->minMatchLength, 64); |
35 | 744k | unsigned hashRateLog = params->hashRateLog; |
36 | | |
37 | 744k | state->rolling = ~(U32)0; |
38 | | |
39 | | /* The choice of the splitting criterion is subject to two conditions: |
40 | | * 1. it has to trigger on average every 2^(hashRateLog) bytes; |
41 | | * 2. ideally, it has to depend on a window of minMatchLength bytes. |
42 | | * |
43 | | * In the gear hash algorithm, bit n depends on the last n bytes; |
44 | | * so in order to obtain a good quality splitting criterion it is |
45 | | * preferable to use bits with high weight. |
46 | | * |
47 | | * To match condition 1 we use a mask with hashRateLog bits set |
48 | | * and, because of the previous remark, we make sure these bits |
49 | | * have the highest possible weight while still respecting |
50 | | * condition 2. |
51 | | */ |
52 | 744k | if (hashRateLog > 0 && hashRateLog <= maxBitsInMask) { |
53 | 613k | state->stopMask = (((U64)1 << hashRateLog) - 1) << (maxBitsInMask - hashRateLog); |
54 | 613k | } else { |
55 | | /* In this degenerate case we simply honor the hash rate. */ |
56 | 131k | state->stopMask = ((U64)1 << hashRateLog) - 1; |
57 | 131k | } |
58 | 744k | } |
59 | | |
60 | | /** ZSTD_ldm_gear_reset() |
61 | | * Feeds [data, data + minMatchLength) into the hash without registering any |
62 | | * splits. This effectively resets the hash state. This is used when skipping |
63 | | * over data, either at the beginning of a block, or skipping sections. |
64 | | */ |
65 | | static void ZSTD_ldm_gear_reset(ldmRollingHashState_t* state, |
66 | | BYTE const* data, size_t minMatchLength) |
67 | 1.71M | { |
68 | 1.71M | U64 hash = state->rolling; |
69 | 1.71M | size_t n = 0; |
70 | | |
71 | 114M | #define GEAR_ITER_ONCE() do { \ |
72 | 114M | hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; \ |
73 | 114M | n += 1; \ |
74 | 114M | } while (0) |
75 | 30.1M | while (n + 3 < minMatchLength) { |
76 | 28.4M | GEAR_ITER_ONCE(); |
77 | 28.4M | GEAR_ITER_ONCE(); |
78 | 28.4M | GEAR_ITER_ONCE(); |
79 | 28.4M | GEAR_ITER_ONCE(); |
80 | 28.4M | } |
81 | 2.31M | while (n < minMatchLength) { |
82 | 603k | GEAR_ITER_ONCE(); |
83 | 603k | } |
84 | 1.71M | #undef GEAR_ITER_ONCE |
85 | 1.71M | } |
86 | | |
87 | | /** ZSTD_ldm_gear_feed(): |
88 | | * |
89 | | * Registers in the splits array all the split points found in the first |
90 | | * size bytes following the data pointer. This function terminates when |
91 | | * either all the data has been processed or LDM_BATCH_SIZE splits are |
92 | | * present in the splits array. |
93 | | * |
94 | | * Precondition: The splits array must not be full. |
95 | | * Returns: The number of bytes processed. */ |
96 | | static size_t ZSTD_ldm_gear_feed(ldmRollingHashState_t* state, |
97 | | BYTE const* data, size_t size, |
98 | | size_t* splits, unsigned* numSplits) |
99 | 5.47M | { |
100 | 5.47M | size_t n; |
101 | 5.47M | U64 hash, mask; |
102 | | |
103 | 5.47M | hash = state->rolling; |
104 | 5.47M | mask = state->stopMask; |
105 | 5.47M | n = 0; |
106 | | |
107 | 1.70G | #define GEAR_ITER_ONCE() do { \ |
108 | 1.70G | hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; \ |
109 | 1.70G | n += 1; \ |
110 | 1.70G | if (UNLIKELY((hash & mask) == 0)) { \ |
111 | 321M | splits[*numSplits] = n; \ |
112 | 321M | *numSplits += 1; \ |
113 | 321M | if (*numSplits == LDM_BATCH_SIZE) \ |
114 | 321M | goto done; \ |
115 | 321M | } \ |
116 | 1.70G | } while (0) |
117 | | |
118 | 426M | while (n + 3 < size) { |
119 | 426M | GEAR_ITER_ONCE(); |
120 | 425M | GEAR_ITER_ONCE(); |
121 | 424M | GEAR_ITER_ONCE(); |
122 | 424M | GEAR_ITER_ONCE(); |
123 | 424M | } |
124 | 1.56M | while (n < size) { |
125 | 860k | GEAR_ITER_ONCE(); |
126 | 860k | } |
127 | | |
128 | 702k | #undef GEAR_ITER_ONCE |
129 | | |
130 | 5.47M | done: |
131 | 5.47M | state->rolling = hash; |
132 | 5.47M | return n; |
133 | 704k | } |
134 | | |
135 | | void ZSTD_ldm_adjustParameters(ldmParams_t* params, |
136 | | ZSTD_compressionParameters const* cParams) |
137 | 284k | { |
138 | 284k | params->windowLog = cParams->windowLog; |
139 | 284k | ZSTD_STATIC_ASSERT(LDM_BUCKET_SIZE_LOG <= ZSTD_LDM_BUCKETSIZELOG_MAX); |
140 | 284k | DEBUGLOG(4, "ZSTD_ldm_adjustParameters"); |
141 | 284k | if (!params->bucketSizeLog) params->bucketSizeLog = LDM_BUCKET_SIZE_LOG; |
142 | 284k | if (!params->minMatchLength) params->minMatchLength = LDM_MIN_MATCH_LENGTH; |
143 | 284k | if (params->hashLog == 0) { |
144 | 0 | params->hashLog = MAX(ZSTD_HASHLOG_MIN, params->windowLog - LDM_HASH_RLOG); |
145 | 0 | assert(params->hashLog <= ZSTD_HASHLOG_MAX); |
146 | 0 | } |
147 | 284k | if (params->hashRateLog == 0) { |
148 | 85.7k | params->hashRateLog = params->windowLog < params->hashLog |
149 | 85.7k | ? 0 |
150 | 85.7k | : params->windowLog - params->hashLog; |
151 | 85.7k | } |
152 | 284k | params->bucketSizeLog = MIN(params->bucketSizeLog, params->hashLog); |
153 | 284k | } |
154 | | |
155 | | size_t ZSTD_ldm_getTableSize(ldmParams_t params) |
156 | 5.50M | { |
157 | 5.50M | size_t const ldmHSize = ((size_t)1) << params.hashLog; |
158 | 5.50M | size_t const ldmBucketSizeLog = MIN(params.bucketSizeLog, params.hashLog); |
159 | 5.50M | size_t const ldmBucketSize = ((size_t)1) << (params.hashLog - ldmBucketSizeLog); |
160 | 5.50M | size_t const totalSize = ZSTD_cwksp_alloc_size(ldmBucketSize) |
161 | 5.50M | + ZSTD_cwksp_alloc_size(ldmHSize * sizeof(ldmEntry_t)); |
162 | 5.50M | return params.enableLdm == ZSTD_ps_enable ? totalSize : 0; |
163 | 5.50M | } |
164 | | |
165 | | size_t ZSTD_ldm_getMaxNbSeq(ldmParams_t params, size_t maxChunkSize) |
166 | 11.0M | { |
167 | 11.0M | return params.enableLdm == ZSTD_ps_enable ? (maxChunkSize / params.minMatchLength) : 0; |
168 | 11.0M | } |
169 | | |
170 | | /** ZSTD_ldm_getBucket() : |
171 | | * Returns a pointer to the start of the bucket associated with hash. */ |
172 | | static ldmEntry_t* ZSTD_ldm_getBucket( |
173 | | ldmState_t* ldmState, size_t hash, ldmParams_t const ldmParams) |
174 | 625M | { |
175 | 625M | return ldmState->hashTable + (hash << ldmParams.bucketSizeLog); |
176 | 625M | } |
177 | | |
178 | | /** ZSTD_ldm_insertEntry() : |
179 | | * Insert the entry with corresponding hash into the hash table */ |
180 | | static void ZSTD_ldm_insertEntry(ldmState_t* ldmState, |
181 | | size_t const hash, const ldmEntry_t entry, |
182 | | ldmParams_t const ldmParams) |
183 | 310M | { |
184 | 310M | BYTE* const pOffset = ldmState->bucketOffsets + hash; |
185 | 310M | unsigned const offset = *pOffset; |
186 | | |
187 | 310M | *(ZSTD_ldm_getBucket(ldmState, hash, ldmParams) + offset) = entry; |
188 | 310M | *pOffset = (BYTE)((offset + 1) & ((1u << ldmParams.bucketSizeLog) - 1)); |
189 | | |
190 | 310M | } |
191 | | |
192 | | /** ZSTD_ldm_countBackwardsMatch() : |
193 | | * Returns the number of bytes that match backwards before pIn and pMatch. |
194 | | * |
195 | | * We count only bytes where pMatch >= pBase and pIn >= pAnchor. */ |
196 | | static size_t ZSTD_ldm_countBackwardsMatch( |
197 | | const BYTE* pIn, const BYTE* pAnchor, |
198 | | const BYTE* pMatch, const BYTE* pMatchBase) |
199 | 88.4M | { |
200 | 88.4M | size_t matchLength = 0; |
201 | 164M | while (pIn > pAnchor && pMatch > pMatchBase && pIn[-1] == pMatch[-1]) { |
202 | 75.7M | pIn--; |
203 | 75.7M | pMatch--; |
204 | 75.7M | matchLength++; |
205 | 75.7M | } |
206 | 88.4M | return matchLength; |
207 | 88.4M | } |
208 | | |
209 | | /** ZSTD_ldm_countBackwardsMatch_2segments() : |
210 | | * Returns the number of bytes that match backwards from pMatch, |
211 | | * even with the backwards match spanning 2 different segments. |
212 | | * |
213 | | * On reaching `pMatchBase`, start counting from mEnd */ |
214 | | static size_t ZSTD_ldm_countBackwardsMatch_2segments( |
215 | | const BYTE* pIn, const BYTE* pAnchor, |
216 | | const BYTE* pMatch, const BYTE* pMatchBase, |
217 | | const BYTE* pExtDictStart, const BYTE* pExtDictEnd) |
218 | 3.47M | { |
219 | 3.47M | size_t matchLength = ZSTD_ldm_countBackwardsMatch(pIn, pAnchor, pMatch, pMatchBase); |
220 | 3.47M | if (pMatch - matchLength != pMatchBase || pMatchBase == pExtDictStart) { |
221 | | /* If backwards match is entirely in the extDict or prefix, immediately return */ |
222 | 3.47M | return matchLength; |
223 | 3.47M | } |
224 | 2.54k | DEBUGLOG(7, "ZSTD_ldm_countBackwardsMatch_2segments: found 2-parts backwards match (length in prefix==%zu)", matchLength); |
225 | 2.54k | matchLength += ZSTD_ldm_countBackwardsMatch(pIn - matchLength, pAnchor, pExtDictEnd, pExtDictStart); |
226 | 2.54k | DEBUGLOG(7, "final backwards match length = %zu", matchLength); |
227 | 2.54k | return matchLength; |
228 | 3.47M | } |
229 | | |
230 | | /** ZSTD_ldm_fillFastTables() : |
231 | | * |
232 | | * Fills the relevant tables for the ZSTD_fast and ZSTD_dfast strategies. |
233 | | * This is similar to ZSTD_loadDictionaryContent. |
234 | | * |
235 | | * The tables for the other strategies are filled within their |
236 | | * block compressors. */ |
237 | | static size_t ZSTD_ldm_fillFastTables(ZSTD_matchState_t* ms, |
238 | | void const* end) |
239 | 19.3M | { |
240 | 19.3M | const BYTE* const iend = (const BYTE*)end; |
241 | | |
242 | 19.3M | switch(ms->cParams.strategy) |
243 | 19.3M | { |
244 | 4.10M | case ZSTD_fast: |
245 | 4.10M | ZSTD_fillHashTable(ms, iend, ZSTD_dtlm_fast, ZSTD_tfp_forCCtx); |
246 | 4.10M | break; |
247 | | |
248 | 5.32M | case ZSTD_dfast: |
249 | 5.32M | #ifndef ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR |
250 | 5.32M | ZSTD_fillDoubleHashTable(ms, iend, ZSTD_dtlm_fast, ZSTD_tfp_forCCtx); |
251 | | #else |
252 | | assert(0); /* shouldn't be called: cparams should've been adjusted. */ |
253 | | #endif |
254 | 5.32M | break; |
255 | | |
256 | 1.82M | case ZSTD_greedy: |
257 | 4.72M | case ZSTD_lazy: |
258 | 8.99M | case ZSTD_lazy2: |
259 | 9.96M | case ZSTD_btlazy2: |
260 | 9.96M | case ZSTD_btopt: |
261 | 9.96M | case ZSTD_btultra: |
262 | 9.96M | case ZSTD_btultra2: |
263 | 9.96M | break; |
264 | 0 | default: |
265 | 0 | assert(0); /* not possible : not a valid strategy id */ |
266 | 19.3M | } |
267 | | |
268 | 19.3M | return 0; |
269 | 19.3M | } |
270 | | |
271 | | void ZSTD_ldm_fillHashTable( |
272 | | ldmState_t* ldmState, const BYTE* ip, |
273 | | const BYTE* iend, ldmParams_t const* params) |
274 | 8.38k | { |
275 | 8.38k | U32 const minMatchLength = params->minMatchLength; |
276 | 8.38k | U32 const hBits = params->hashLog - params->bucketSizeLog; |
277 | 8.38k | BYTE const* const base = ldmState->window.base; |
278 | 8.38k | BYTE const* const istart = ip; |
279 | 8.38k | ldmRollingHashState_t hashState; |
280 | 8.38k | size_t* const splits = ldmState->splitIndices; |
281 | 8.38k | unsigned numSplits; |
282 | | |
283 | 8.38k | DEBUGLOG(5, "ZSTD_ldm_fillHashTable"); |
284 | | |
285 | 8.38k | ZSTD_ldm_gear_init(&hashState, params); |
286 | 116k | while (ip < iend) { |
287 | 108k | size_t hashed; |
288 | 108k | unsigned n; |
289 | | |
290 | 108k | numSplits = 0; |
291 | 108k | hashed = ZSTD_ldm_gear_feed(&hashState, ip, iend - ip, splits, &numSplits); |
292 | | |
293 | 6.61M | for (n = 0; n < numSplits; n++) { |
294 | 6.50M | if (ip + splits[n] >= istart + minMatchLength) { |
295 | 6.25M | BYTE const* const split = ip + splits[n] - minMatchLength; |
296 | 6.25M | U64 const xxhash = XXH64(split, minMatchLength, 0); |
297 | 6.25M | U32 const hash = (U32)(xxhash & (((U32)1 << hBits) - 1)); |
298 | 6.25M | ldmEntry_t entry; |
299 | | |
300 | 6.25M | entry.offset = (U32)(split - base); |
301 | 6.25M | entry.checksum = (U32)(xxhash >> 32); |
302 | 6.25M | ZSTD_ldm_insertEntry(ldmState, hash, entry, *params); |
303 | 6.25M | } |
304 | 6.50M | } |
305 | | |
306 | 108k | ip += hashed; |
307 | 108k | } |
308 | 8.38k | } |
309 | | |
310 | | |
311 | | /** ZSTD_ldm_limitTableUpdate() : |
312 | | * |
313 | | * Sets cctx->nextToUpdate to a position corresponding closer to anchor |
314 | | * if it is far way |
315 | | * (after a long match, only update tables a limited amount). */ |
316 | | static void ZSTD_ldm_limitTableUpdate(ZSTD_matchState_t* ms, const BYTE* anchor) |
317 | 19.3M | { |
318 | 19.3M | U32 const curr = (U32)(anchor - ms->window.base); |
319 | 19.3M | if (curr > ms->nextToUpdate + 1024) { |
320 | 284k | ms->nextToUpdate = |
321 | 284k | curr - MIN(512, curr - ms->nextToUpdate - 1024); |
322 | 284k | } |
323 | 19.3M | } |
324 | | |
325 | | static |
326 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
327 | | size_t ZSTD_ldm_generateSequences_internal( |
328 | | ldmState_t* ldmState, rawSeqStore_t* rawSeqStore, |
329 | | ldmParams_t const* params, void const* src, size_t srcSize) |
330 | 2.37M | { |
331 | | /* LDM parameters */ |
332 | 2.37M | int const extDict = ZSTD_window_hasExtDict(ldmState->window); |
333 | 2.37M | U32 const minMatchLength = params->minMatchLength; |
334 | 2.37M | U32 const entsPerBucket = 1U << params->bucketSizeLog; |
335 | 2.37M | U32 const hBits = params->hashLog - params->bucketSizeLog; |
336 | | /* Prefix and extDict parameters */ |
337 | 2.37M | U32 const dictLimit = ldmState->window.dictLimit; |
338 | 2.37M | U32 const lowestIndex = extDict ? ldmState->window.lowLimit : dictLimit; |
339 | 2.37M | BYTE const* const base = ldmState->window.base; |
340 | 2.37M | BYTE const* const dictBase = extDict ? ldmState->window.dictBase : NULL; |
341 | 2.37M | BYTE const* const dictStart = extDict ? dictBase + lowestIndex : NULL; |
342 | 2.37M | BYTE const* const dictEnd = extDict ? dictBase + dictLimit : NULL; |
343 | 2.37M | BYTE const* const lowPrefixPtr = base + dictLimit; |
344 | | /* Input bounds */ |
345 | 2.37M | BYTE const* const istart = (BYTE const*)src; |
346 | 2.37M | BYTE const* const iend = istart + srcSize; |
347 | 2.37M | BYTE const* const ilimit = iend - HASH_READ_SIZE; |
348 | | /* Input positions */ |
349 | 2.37M | BYTE const* anchor = istart; |
350 | 2.37M | BYTE const* ip = istart; |
351 | | /* Rolling hash state */ |
352 | 2.37M | ldmRollingHashState_t hashState; |
353 | | /* Arrays for staged-processing */ |
354 | 2.37M | size_t* const splits = ldmState->splitIndices; |
355 | 2.37M | ldmMatchCandidate_t* const candidates = ldmState->matchCandidates; |
356 | 2.37M | unsigned numSplits; |
357 | | |
358 | 2.37M | if (srcSize < minMatchLength) |
359 | 1.64M | return iend - anchor; |
360 | | |
361 | | /* Initialize the rolling hash state with the first minMatchLength bytes */ |
362 | 735k | ZSTD_ldm_gear_init(&hashState, params); |
363 | 735k | ZSTD_ldm_gear_reset(&hashState, ip, minMatchLength); |
364 | 735k | ip += minMatchLength; |
365 | | |
366 | 6.09M | while (ip < ilimit) { |
367 | 5.36M | size_t hashed; |
368 | 5.36M | unsigned n; |
369 | | |
370 | 5.36M | numSplits = 0; |
371 | 5.36M | hashed = ZSTD_ldm_gear_feed(&hashState, ip, ilimit - ip, |
372 | 5.36M | splits, &numSplits); |
373 | | |
374 | 320M | for (n = 0; n < numSplits; n++) { |
375 | 315M | BYTE const* const split = ip + splits[n] - minMatchLength; |
376 | 315M | U64 const xxhash = XXH64(split, minMatchLength, 0); |
377 | 315M | U32 const hash = (U32)(xxhash & (((U32)1 << hBits) - 1)); |
378 | | |
379 | 315M | candidates[n].split = split; |
380 | 315M | candidates[n].hash = hash; |
381 | 315M | candidates[n].checksum = (U32)(xxhash >> 32); |
382 | 315M | candidates[n].bucket = ZSTD_ldm_getBucket(ldmState, hash, *params); |
383 | 315M | PREFETCH_L1(candidates[n].bucket); |
384 | 315M | } |
385 | | |
386 | 308M | for (n = 0; n < numSplits; n++) { |
387 | 304M | size_t forwardMatchLength = 0, backwardMatchLength = 0, |
388 | 304M | bestMatchLength = 0, mLength; |
389 | 304M | U32 offset; |
390 | 304M | BYTE const* const split = candidates[n].split; |
391 | 304M | U32 const checksum = candidates[n].checksum; |
392 | 304M | U32 const hash = candidates[n].hash; |
393 | 304M | ldmEntry_t* const bucket = candidates[n].bucket; |
394 | 304M | ldmEntry_t const* cur; |
395 | 304M | ldmEntry_t const* bestEntry = NULL; |
396 | 304M | ldmEntry_t newEntry; |
397 | | |
398 | 304M | newEntry.offset = (U32)(split - base); |
399 | 304M | newEntry.checksum = checksum; |
400 | | |
401 | | /* If a split point would generate a sequence overlapping with |
402 | | * the previous one, we merely register it in the hash table and |
403 | | * move on */ |
404 | 304M | if (split < anchor) { |
405 | 69.6M | ZSTD_ldm_insertEntry(ldmState, hash, newEntry, *params); |
406 | 69.6M | continue; |
407 | 69.6M | } |
408 | | |
409 | 7.95G | for (cur = bucket; cur < bucket + entsPerBucket; cur++) { |
410 | 7.71G | size_t curForwardMatchLength, curBackwardMatchLength, |
411 | 7.71G | curTotalMatchLength; |
412 | 7.71G | if (cur->checksum != checksum || cur->offset <= lowestIndex) { |
413 | 7.63G | continue; |
414 | 7.63G | } |
415 | 88.4M | if (extDict) { |
416 | 3.47M | BYTE const* const curMatchBase = |
417 | 3.47M | cur->offset < dictLimit ? dictBase : base; |
418 | 3.47M | BYTE const* const pMatch = curMatchBase + cur->offset; |
419 | 3.47M | BYTE const* const matchEnd = |
420 | 3.47M | cur->offset < dictLimit ? dictEnd : iend; |
421 | 3.47M | BYTE const* const lowMatchPtr = |
422 | 3.47M | cur->offset < dictLimit ? dictStart : lowPrefixPtr; |
423 | 3.47M | curForwardMatchLength = |
424 | 3.47M | ZSTD_count_2segments(split, pMatch, iend, matchEnd, lowPrefixPtr); |
425 | 3.47M | if (curForwardMatchLength < minMatchLength) { |
426 | 294 | continue; |
427 | 294 | } |
428 | 3.47M | curBackwardMatchLength = ZSTD_ldm_countBackwardsMatch_2segments( |
429 | 3.47M | split, anchor, pMatch, lowMatchPtr, dictStart, dictEnd); |
430 | 85.0M | } else { /* !extDict */ |
431 | 85.0M | BYTE const* const pMatch = base + cur->offset; |
432 | 85.0M | curForwardMatchLength = ZSTD_count(split, pMatch, iend); |
433 | 85.0M | if (curForwardMatchLength < minMatchLength) { |
434 | 787 | continue; |
435 | 787 | } |
436 | 85.0M | curBackwardMatchLength = |
437 | 85.0M | ZSTD_ldm_countBackwardsMatch(split, anchor, pMatch, lowPrefixPtr); |
438 | 85.0M | } |
439 | 88.4M | curTotalMatchLength = curForwardMatchLength + curBackwardMatchLength; |
440 | | |
441 | 88.4M | if (curTotalMatchLength > bestMatchLength) { |
442 | 25.2M | bestMatchLength = curTotalMatchLength; |
443 | 25.2M | forwardMatchLength = curForwardMatchLength; |
444 | 25.2M | backwardMatchLength = curBackwardMatchLength; |
445 | 25.2M | bestEntry = cur; |
446 | 25.2M | } |
447 | 88.4M | } |
448 | | |
449 | | /* No match found -- insert an entry into the hash table |
450 | | * and process the next candidate match */ |
451 | 234M | if (bestEntry == NULL) { |
452 | 215M | ZSTD_ldm_insertEntry(ldmState, hash, newEntry, *params); |
453 | 215M | continue; |
454 | 215M | } |
455 | | |
456 | | /* Match found */ |
457 | 19.2M | offset = (U32)(split - base) - bestEntry->offset; |
458 | 19.2M | mLength = forwardMatchLength + backwardMatchLength; |
459 | 19.2M | { |
460 | 19.2M | rawSeq* const seq = rawSeqStore->seq + rawSeqStore->size; |
461 | | |
462 | | /* Out of sequence storage */ |
463 | 19.2M | if (rawSeqStore->size == rawSeqStore->capacity) |
464 | 0 | return ERROR(dstSize_tooSmall); |
465 | 19.2M | seq->litLength = (U32)(split - backwardMatchLength - anchor); |
466 | 19.2M | seq->matchLength = (U32)mLength; |
467 | 19.2M | seq->offset = offset; |
468 | 19.2M | rawSeqStore->size++; |
469 | 19.2M | } |
470 | | |
471 | | /* Insert the current entry into the hash table --- it must be |
472 | | * done after the previous block to avoid clobbering bestEntry */ |
473 | 0 | ZSTD_ldm_insertEntry(ldmState, hash, newEntry, *params); |
474 | | |
475 | 19.2M | anchor = split + forwardMatchLength; |
476 | | |
477 | | /* If we find a match that ends after the data that we've hashed |
478 | | * then we have a repeating, overlapping, pattern. E.g. all zeros. |
479 | | * If one repetition of the pattern matches our `stopMask` then all |
480 | | * repetitions will. We don't need to insert them all into out table, |
481 | | * only the first one. So skip over overlapping matches. |
482 | | * This is a major speed boost (20x) for compressing a single byte |
483 | | * repeated, when that byte ends up in the table. |
484 | | */ |
485 | 19.2M | if (anchor > ip + hashed) { |
486 | 980k | ZSTD_ldm_gear_reset(&hashState, anchor - minMatchLength, minMatchLength); |
487 | | /* Continue the outer loop at anchor (ip + hashed == anchor). */ |
488 | 980k | ip = anchor - hashed; |
489 | 980k | break; |
490 | 980k | } |
491 | 19.2M | } |
492 | | |
493 | 5.36M | ip += hashed; |
494 | 5.36M | } |
495 | | |
496 | 735k | return iend - anchor; |
497 | 735k | } |
498 | | |
499 | | /*! ZSTD_ldm_reduceTable() : |
500 | | * reduce table indexes by `reducerValue` */ |
501 | | static void ZSTD_ldm_reduceTable(ldmEntry_t* const table, U32 const size, |
502 | | U32 const reducerValue) |
503 | 77.0k | { |
504 | 77.0k | U32 u; |
505 | 412M | for (u = 0; u < size; u++) { |
506 | 412M | if (table[u].offset < reducerValue) table[u].offset = 0; |
507 | 15.3M | else table[u].offset -= reducerValue; |
508 | 412M | } |
509 | 77.0k | } |
510 | | |
511 | | size_t ZSTD_ldm_generateSequences( |
512 | | ldmState_t* ldmState, rawSeqStore_t* sequences, |
513 | | ldmParams_t const* params, void const* src, size_t srcSize) |
514 | 2.66M | { |
515 | 2.66M | U32 const maxDist = 1U << params->windowLog; |
516 | 2.66M | BYTE const* const istart = (BYTE const*)src; |
517 | 2.66M | BYTE const* const iend = istart + srcSize; |
518 | 2.66M | size_t const kMaxChunkSize = 1 << 20; |
519 | 2.66M | size_t const nbChunks = (srcSize / kMaxChunkSize) + ((srcSize % kMaxChunkSize) != 0); |
520 | 2.66M | size_t chunk; |
521 | 2.66M | size_t leftoverSize = 0; |
522 | | |
523 | 2.66M | assert(ZSTD_CHUNKSIZE_MAX >= kMaxChunkSize); |
524 | | /* Check that ZSTD_window_update() has been called for this chunk prior |
525 | | * to passing it to this function. |
526 | | */ |
527 | 2.66M | assert(ldmState->window.nextSrc >= (BYTE const*)src + srcSize); |
528 | | /* The input could be very large (in zstdmt), so it must be broken up into |
529 | | * chunks to enforce the maximum distance and handle overflow correction. |
530 | | */ |
531 | 2.66M | assert(sequences->pos <= sequences->size); |
532 | 2.66M | assert(sequences->size <= sequences->capacity); |
533 | 5.04M | for (chunk = 0; chunk < nbChunks && sequences->size < sequences->capacity; ++chunk) { |
534 | 2.37M | BYTE const* const chunkStart = istart + chunk * kMaxChunkSize; |
535 | 2.37M | size_t const remaining = (size_t)(iend - chunkStart); |
536 | 2.37M | BYTE const *const chunkEnd = |
537 | 2.37M | (remaining < kMaxChunkSize) ? iend : chunkStart + kMaxChunkSize; |
538 | 2.37M | size_t const chunkSize = chunkEnd - chunkStart; |
539 | 2.37M | size_t newLeftoverSize; |
540 | 2.37M | size_t const prevSize = sequences->size; |
541 | | |
542 | 2.37M | assert(chunkStart < iend); |
543 | | /* 1. Perform overflow correction if necessary. */ |
544 | 2.37M | if (ZSTD_window_needOverflowCorrection(ldmState->window, 0, maxDist, ldmState->loadedDictEnd, chunkStart, chunkEnd)) { |
545 | 77.0k | U32 const ldmHSize = 1U << params->hashLog; |
546 | 77.0k | U32 const correction = ZSTD_window_correctOverflow( |
547 | 77.0k | &ldmState->window, /* cycleLog */ 0, maxDist, chunkStart); |
548 | 77.0k | ZSTD_ldm_reduceTable(ldmState->hashTable, ldmHSize, correction); |
549 | | /* invalidate dictionaries on overflow correction */ |
550 | 77.0k | ldmState->loadedDictEnd = 0; |
551 | 77.0k | } |
552 | | /* 2. We enforce the maximum offset allowed. |
553 | | * |
554 | | * kMaxChunkSize should be small enough that we don't lose too much of |
555 | | * the window through early invalidation. |
556 | | * TODO: * Test the chunk size. |
557 | | * * Try invalidation after the sequence generation and test the |
558 | | * offset against maxDist directly. |
559 | | * |
560 | | * NOTE: Because of dictionaries + sequence splitting we MUST make sure |
561 | | * that any offset used is valid at the END of the sequence, since it may |
562 | | * be split into two sequences. This condition holds when using |
563 | | * ZSTD_window_enforceMaxDist(), but if we move to checking offsets |
564 | | * against maxDist directly, we'll have to carefully handle that case. |
565 | | */ |
566 | 2.37M | ZSTD_window_enforceMaxDist(&ldmState->window, chunkEnd, maxDist, &ldmState->loadedDictEnd, NULL); |
567 | | /* 3. Generate the sequences for the chunk, and get newLeftoverSize. */ |
568 | 2.37M | newLeftoverSize = ZSTD_ldm_generateSequences_internal( |
569 | 2.37M | ldmState, sequences, params, chunkStart, chunkSize); |
570 | 2.37M | if (ZSTD_isError(newLeftoverSize)) |
571 | 0 | return newLeftoverSize; |
572 | | /* 4. We add the leftover literals from previous iterations to the first |
573 | | * newly generated sequence, or add the `newLeftoverSize` if none are |
574 | | * generated. |
575 | | */ |
576 | | /* Prepend the leftover literals from the last call */ |
577 | 2.37M | if (prevSize < sequences->size) { |
578 | 535k | sequences->seq[prevSize].litLength += (U32)leftoverSize; |
579 | 535k | leftoverSize = newLeftoverSize; |
580 | 1.84M | } else { |
581 | 1.84M | assert(newLeftoverSize == chunkSize); |
582 | 1.84M | leftoverSize += chunkSize; |
583 | 1.84M | } |
584 | 2.37M | } |
585 | 2.66M | return 0; |
586 | 2.66M | } |
587 | | |
588 | | void |
589 | | ZSTD_ldm_skipSequences(rawSeqStore_t* rawSeqStore, size_t srcSize, U32 const minMatch) |
590 | 25.3M | { |
591 | 25.3M | while (srcSize > 0 && rawSeqStore->pos < rawSeqStore->size) { |
592 | 139k | rawSeq* seq = rawSeqStore->seq + rawSeqStore->pos; |
593 | 139k | if (srcSize <= seq->litLength) { |
594 | | /* Skip past srcSize literals */ |
595 | 135k | seq->litLength -= (U32)srcSize; |
596 | 135k | return; |
597 | 135k | } |
598 | 3.20k | srcSize -= seq->litLength; |
599 | 3.20k | seq->litLength = 0; |
600 | 3.20k | if (srcSize < seq->matchLength) { |
601 | | /* Skip past the first srcSize of the match */ |
602 | 3.18k | seq->matchLength -= (U32)srcSize; |
603 | 3.18k | if (seq->matchLength < minMatch) { |
604 | | /* The match is too short, omit it */ |
605 | 592 | if (rawSeqStore->pos + 1 < rawSeqStore->size) { |
606 | 532 | seq[1].litLength += seq[0].matchLength; |
607 | 532 | } |
608 | 592 | rawSeqStore->pos++; |
609 | 592 | } |
610 | 3.18k | return; |
611 | 3.18k | } |
612 | 23 | srcSize -= seq->matchLength; |
613 | 23 | seq->matchLength = 0; |
614 | 23 | rawSeqStore->pos++; |
615 | 23 | } |
616 | 25.3M | } |
617 | | |
618 | | /** |
619 | | * If the sequence length is longer than remaining then the sequence is split |
620 | | * between this block and the next. |
621 | | * |
622 | | * Returns the current sequence to handle, or if the rest of the block should |
623 | | * be literals, it returns a sequence with offset == 0. |
624 | | */ |
625 | | static rawSeq maybeSplitSequence(rawSeqStore_t* rawSeqStore, |
626 | | U32 const remaining, U32 const minMatch) |
627 | 18.4M | { |
628 | 18.4M | rawSeq sequence = rawSeqStore->seq[rawSeqStore->pos]; |
629 | 18.4M | assert(sequence.offset > 0); |
630 | | /* Likely: No partial sequence */ |
631 | 18.4M | if (remaining >= sequence.litLength + sequence.matchLength) { |
632 | 18.3M | rawSeqStore->pos++; |
633 | 18.3M | return sequence; |
634 | 18.3M | } |
635 | | /* Cut the sequence short (offset == 0 ==> rest is literals). */ |
636 | 138k | if (remaining <= sequence.litLength) { |
637 | 135k | sequence.offset = 0; |
638 | 135k | } else if (remaining < sequence.litLength + sequence.matchLength) { |
639 | 3.18k | sequence.matchLength = remaining - sequence.litLength; |
640 | 3.18k | if (sequence.matchLength < minMatch) { |
641 | 551 | sequence.offset = 0; |
642 | 551 | } |
643 | 3.18k | } |
644 | | /* Skip past `remaining` bytes for the future sequences. */ |
645 | 138k | ZSTD_ldm_skipSequences(rawSeqStore, remaining, minMatch); |
646 | 138k | return sequence; |
647 | 18.4M | } |
648 | | |
649 | 2.36M | void ZSTD_ldm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t nbBytes) { |
650 | 2.36M | U32 currPos = (U32)(rawSeqStore->posInSequence + nbBytes); |
651 | 3.31M | while (currPos && rawSeqStore->pos < rawSeqStore->size) { |
652 | 1.01M | rawSeq currSeq = rawSeqStore->seq[rawSeqStore->pos]; |
653 | 1.01M | if (currPos >= currSeq.litLength + currSeq.matchLength) { |
654 | 948k | currPos -= currSeq.litLength + currSeq.matchLength; |
655 | 948k | rawSeqStore->pos++; |
656 | 948k | } else { |
657 | 62.7k | rawSeqStore->posInSequence = currPos; |
658 | 62.7k | break; |
659 | 62.7k | } |
660 | 1.01M | } |
661 | 2.36M | if (currPos == 0 || rawSeqStore->pos == rawSeqStore->size) { |
662 | 2.30M | rawSeqStore->posInSequence = 0; |
663 | 2.30M | } |
664 | 2.36M | } |
665 | | |
666 | | size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore, |
667 | | ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], |
668 | | ZSTD_paramSwitch_e useRowMatchFinder, |
669 | | void const* src, size_t srcSize) |
670 | 1.28M | { |
671 | 1.28M | const ZSTD_compressionParameters* const cParams = &ms->cParams; |
672 | 1.28M | unsigned const minMatch = cParams->minMatch; |
673 | 1.28M | ZSTD_blockCompressor const blockCompressor = |
674 | 1.28M | ZSTD_selectBlockCompressor(cParams->strategy, useRowMatchFinder, ZSTD_matchState_dictMode(ms)); |
675 | | /* Input bounds */ |
676 | 1.28M | BYTE const* const istart = (BYTE const*)src; |
677 | 1.28M | BYTE const* const iend = istart + srcSize; |
678 | | /* Input positions */ |
679 | 1.28M | BYTE const* ip = istart; |
680 | | |
681 | 1.28M | DEBUGLOG(5, "ZSTD_ldm_blockCompress: srcSize=%zu", srcSize); |
682 | | /* If using opt parser, use LDMs only as candidates rather than always accepting them */ |
683 | 1.28M | if (cParams->strategy >= ZSTD_btopt) { |
684 | 217k | size_t lastLLSize; |
685 | 217k | ms->ldmSeqStore = rawSeqStore; |
686 | 217k | lastLLSize = blockCompressor(ms, seqStore, rep, src, srcSize); |
687 | 217k | ZSTD_ldm_skipRawSeqStoreBytes(rawSeqStore, srcSize); |
688 | 217k | return lastLLSize; |
689 | 217k | } |
690 | | |
691 | 1.07M | assert(rawSeqStore->pos <= rawSeqStore->size); |
692 | 1.07M | assert(rawSeqStore->size <= rawSeqStore->capacity); |
693 | | /* Loop through each sequence and apply the block compressor to the literals */ |
694 | 19.3M | while (rawSeqStore->pos < rawSeqStore->size && ip < iend) { |
695 | | /* maybeSplitSequence updates rawSeqStore->pos */ |
696 | 18.4M | rawSeq const sequence = maybeSplitSequence(rawSeqStore, |
697 | 18.4M | (U32)(iend - ip), minMatch); |
698 | | /* End signal */ |
699 | 18.4M | if (sequence.offset == 0) |
700 | 136k | break; |
701 | | |
702 | 18.3M | assert(ip + sequence.litLength + sequence.matchLength <= iend); |
703 | | |
704 | | /* Fill tables for block compressor */ |
705 | 18.3M | ZSTD_ldm_limitTableUpdate(ms, ip); |
706 | 18.3M | ZSTD_ldm_fillFastTables(ms, ip); |
707 | | /* Run the block compressor */ |
708 | 18.3M | DEBUGLOG(5, "pos %u : calling block compressor on segment of size %u", (unsigned)(ip-istart), sequence.litLength); |
709 | 18.3M | { |
710 | 18.3M | int i; |
711 | 18.3M | size_t const newLitLength = |
712 | 18.3M | blockCompressor(ms, seqStore, rep, ip, sequence.litLength); |
713 | 18.3M | ip += sequence.litLength; |
714 | | /* Update the repcodes */ |
715 | 54.9M | for (i = ZSTD_REP_NUM - 1; i > 0; i--) |
716 | 36.6M | rep[i] = rep[i-1]; |
717 | 18.3M | rep[0] = sequence.offset; |
718 | | /* Store the sequence */ |
719 | 18.3M | ZSTD_storeSeq(seqStore, newLitLength, ip - newLitLength, iend, |
720 | 18.3M | OFFSET_TO_OFFBASE(sequence.offset), |
721 | 0 | sequence.matchLength); |
722 | 0 | ip += sequence.matchLength; |
723 | 18.3M | } |
724 | 18.3M | } |
725 | | /* Fill the tables for the block compressor */ |
726 | 1.07M | ZSTD_ldm_limitTableUpdate(ms, ip); |
727 | 1.07M | ZSTD_ldm_fillFastTables(ms, ip); |
728 | | /* Compress the last literals */ |
729 | 1.07M | return blockCompressor(ms, seqStore, rep, ip, iend - ip); |
730 | 1.07M | } |