|
10 | 10 |
|
11 | 11 | #include "zstd_compress_internal.h" |
12 | 12 | #include "zstd_opt.h" |
13 | | -#include "zstd_lazy.h" /* ZSTD_updateTree, ZSTD_updateTree_extDict */ |
14 | 13 |
|
15 | 14 |
|
16 | 15 | #define ZSTD_LITFREQ_ADD 2 /* scaling factor for litFreq, so that frequencies adapt faster to new stats. Also used for matchSum (?) */ |
@@ -265,6 +264,147 @@ static U32 ZSTD_insertAndFindFirstIndexHash3 (ZSTD_CCtx* const cctx, const BYTE* |
265 | 264 | /*-************************************* |
266 | 265 | * Binary Tree search |
267 | 266 | ***************************************/ |
| 267 | +/** ZSTD_insertBt1() : add one or multiple positions to tree. |
| 268 | + * ip : assumed <= iend-8 . |
| 269 | + * @return : nb of positions added */ |
| 270 | +static U32 ZSTD_insertBt1(ZSTD_CCtx* zc, |
| 271 | + const BYTE* const ip, const BYTE* const iend, |
| 272 | + U32 nbCompares, U32 const mls, U32 const extDict) |
| 273 | +{ |
| 274 | + U32* const hashTable = zc->hashTable; |
| 275 | + U32 const hashLog = zc->appliedParams.cParams.hashLog; |
| 276 | + size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
| 277 | + U32* const bt = zc->chainTable; |
| 278 | + U32 const btLog = zc->appliedParams.cParams.chainLog - 1; |
| 279 | + U32 const btMask = (1 << btLog) - 1; |
| 280 | + U32 matchIndex = hashTable[h]; |
| 281 | + size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 282 | + const BYTE* const base = zc->base; |
| 283 | + const BYTE* const dictBase = zc->dictBase; |
| 284 | + const U32 dictLimit = zc->dictLimit; |
| 285 | + const BYTE* const dictEnd = dictBase + dictLimit; |
| 286 | + const BYTE* const prefixStart = base + dictLimit; |
| 287 | + const BYTE* match; |
| 288 | + const U32 current = (U32)(ip-base); |
| 289 | + const U32 btLow = btMask >= current ? 0 : current - btMask; |
| 290 | + U32* smallerPtr = bt + 2*(current&btMask); |
| 291 | + U32* largerPtr = smallerPtr + 1; |
| 292 | + U32 dummy32; /* to be nullified at the end */ |
| 293 | + U32 const windowLow = zc->lowLimit; |
| 294 | + U32 matchEndIdx = current+8+1; |
| 295 | + size_t bestLength = 8; |
| 296 | +#ifdef ZSTD_C_PREDICT |
| 297 | + U32 predictedSmall = *(bt + 2*((current-1)&btMask) + 0); |
| 298 | + U32 predictedLarge = *(bt + 2*((current-1)&btMask) + 1); |
| 299 | + predictedSmall += (predictedSmall>0); |
| 300 | + predictedLarge += (predictedLarge>0); |
| 301 | +#endif /* ZSTD_C_PREDICT */ |
| 302 | + |
| 303 | + DEBUGLOG(8, "ZSTD_insertBt1 (%u)", current); |
| 304 | + |
| 305 | + assert(ip <= iend-8); /* required for h calculation */ |
| 306 | + hashTable[h] = current; /* Update Hash Table */ |
| 307 | + |
| 308 | + while (nbCompares-- && (matchIndex > windowLow)) { |
| 309 | + U32* const nextPtr = bt + 2*(matchIndex & btMask); |
| 310 | + size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 311 | + assert(matchIndex < current); |
| 312 | + |
| 313 | +#ifdef ZSTD_C_PREDICT /* note : can create issues when hlog small <= 11 */ |
| 314 | + const U32* predictPtr = bt + 2*((matchIndex-1) & btMask); /* written this way, as bt is a roll buffer */ |
| 315 | + if (matchIndex == predictedSmall) { |
| 316 | + /* no need to check length, result known */ |
| 317 | + *smallerPtr = matchIndex; |
| 318 | + if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 319 | + smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 320 | + matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
| 321 | + predictedSmall = predictPtr[1] + (predictPtr[1]>0); |
| 322 | + continue; |
| 323 | + } |
| 324 | + if (matchIndex == predictedLarge) { |
| 325 | + *largerPtr = matchIndex; |
| 326 | + if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 327 | + largerPtr = nextPtr; |
| 328 | + matchIndex = nextPtr[0]; |
| 329 | + predictedLarge = predictPtr[0] + (predictPtr[0]>0); |
| 330 | + continue; |
| 331 | + } |
| 332 | +#endif |
| 333 | + |
| 334 | + if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { |
| 335 | + assert(matchIndex+matchLength >= dictLimit); /* might be wrong if extDict is incorrectly set to 0 */ |
| 336 | + match = base + matchIndex; |
| 337 | + matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend); |
| 338 | + } else { |
| 339 | + match = dictBase + matchIndex; |
| 340 | + matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
| 341 | + if (matchIndex+matchLength >= dictLimit) |
| 342 | + match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
| 343 | + } |
| 344 | + |
| 345 | + if (matchLength > bestLength) { |
| 346 | + bestLength = matchLength; |
| 347 | + if (matchLength > matchEndIdx - matchIndex) |
| 348 | + matchEndIdx = matchIndex + (U32)matchLength; |
| 349 | + } |
| 350 | + |
| 351 | + if (ip+matchLength == iend) { /* equal : no way to know if inf or sup */ |
| 352 | + break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt tree */ |
| 353 | + } |
| 354 | + |
| 355 | + if (match[matchLength] < ip[matchLength]) { /* necessarily within buffer */ |
| 356 | + /* match is smaller than current */ |
| 357 | + *smallerPtr = matchIndex; /* update smaller idx */ |
| 358 | + commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
| 359 | + if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop searching */ |
| 360 | + smallerPtr = nextPtr+1; /* new "candidate" => larger than match, which was smaller than target */ |
| 361 | + matchIndex = nextPtr[1]; /* new matchIndex, larger than previous and closer to current */ |
| 362 | + } else { |
| 363 | + /* match is larger than current */ |
| 364 | + *largerPtr = matchIndex; |
| 365 | + commonLengthLarger = matchLength; |
| 366 | + if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop searching */ |
| 367 | + largerPtr = nextPtr; |
| 368 | + matchIndex = nextPtr[0]; |
| 369 | + } } |
| 370 | + |
| 371 | + *smallerPtr = *largerPtr = 0; |
| 372 | + if (bestLength > 384) return MIN(192, (U32)(bestLength - 384)); /* speed optimization */ |
| 373 | + assert(matchEndIdx > current + 8); |
| 374 | + return matchEndIdx - (current + 8); |
| 375 | +} |
| 376 | + |
| 377 | +FORCE_INLINE_TEMPLATE |
| 378 | +void ZSTD_updateTree_internal(ZSTD_CCtx* zc, |
| 379 | + const BYTE* const ip, const BYTE* const iend, |
| 380 | + const U32 nbCompares, const U32 mls, const U32 extDict) |
| 381 | +{ |
| 382 | + const BYTE* const base = zc->base; |
| 383 | + U32 const target = (U32)(ip - base); |
| 384 | + U32 idx = zc->nextToUpdate; |
| 385 | + DEBUGLOG(7, "ZSTD_updateTree_internal, from %u to %u (extDict:%u)", |
| 386 | + idx, target, extDict); |
| 387 | + |
| 388 | + while(idx < target) |
| 389 | + idx += ZSTD_insertBt1(zc, base+idx, iend, nbCompares, mls, extDict); |
| 390 | + zc->nextToUpdate = target; |
| 391 | +} |
| 392 | + |
| 393 | +void ZSTD_updateTree(ZSTD_CCtx* zc, |
| 394 | + const BYTE* ip, const BYTE* iend, |
| 395 | + U32 nbCompares, U32 mls) |
| 396 | +{ |
| 397 | + ZSTD_updateTree_internal(zc, ip, iend, nbCompares, mls, 0 /*extDict*/); |
| 398 | +} |
| 399 | + |
| 400 | +static void ZSTD_updateTree_extDict(ZSTD_CCtx* zc, |
| 401 | + const BYTE* const ip, const BYTE* const iend, |
| 402 | + const U32 nbCompares, const U32 mls) |
| 403 | +{ |
| 404 | + ZSTD_updateTree_internal(zc, ip, iend, nbCompares, mls, 1 /*extDict*/); |
| 405 | +} |
| 406 | + |
| 407 | + |
268 | 408 | FORCE_INLINE_TEMPLATE |
269 | 409 | U32 ZSTD_insertBtAndGetAllMatches ( |
270 | 410 | ZSTD_CCtx* zc, |
|
0 commit comments