zstd-ruby 1.5.1.1 → 1.5.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/README.md +78 -5
  4. data/Rakefile +8 -2
  5. data/ext/zstdruby/common.h +15 -0
  6. data/ext/zstdruby/extconf.rb +1 -1
  7. data/ext/zstdruby/libzstd/common/allocations.h +55 -0
  8. data/ext/zstdruby/libzstd/common/bits.h +200 -0
  9. data/ext/zstdruby/libzstd/common/bitstream.h +19 -60
  10. data/ext/zstdruby/libzstd/common/compiler.h +26 -3
  11. data/ext/zstdruby/libzstd/common/cpu.h +1 -1
  12. data/ext/zstdruby/libzstd/common/debug.c +1 -1
  13. data/ext/zstdruby/libzstd/common/debug.h +1 -1
  14. data/ext/zstdruby/libzstd/common/entropy_common.c +12 -40
  15. data/ext/zstdruby/libzstd/common/error_private.c +9 -2
  16. data/ext/zstdruby/libzstd/common/error_private.h +1 -1
  17. data/ext/zstdruby/libzstd/common/fse.h +5 -83
  18. data/ext/zstdruby/libzstd/common/fse_decompress.c +7 -99
  19. data/ext/zstdruby/libzstd/common/huf.h +65 -156
  20. data/ext/zstdruby/libzstd/common/mem.h +39 -46
  21. data/ext/zstdruby/libzstd/common/pool.c +37 -16
  22. data/ext/zstdruby/libzstd/common/pool.h +9 -3
  23. data/ext/zstdruby/libzstd/common/portability_macros.h +28 -3
  24. data/ext/zstdruby/libzstd/common/threading.c +68 -14
  25. data/ext/zstdruby/libzstd/common/threading.h +5 -10
  26. data/ext/zstdruby/libzstd/common/xxhash.c +2 -2
  27. data/ext/zstdruby/libzstd/common/xxhash.h +8 -8
  28. data/ext/zstdruby/libzstd/common/zstd_common.c +1 -36
  29. data/ext/zstdruby/libzstd/common/zstd_deps.h +1 -1
  30. data/ext/zstdruby/libzstd/common/zstd_internal.h +20 -122
  31. data/ext/zstdruby/libzstd/common/zstd_trace.h +3 -3
  32. data/ext/zstdruby/libzstd/compress/clevels.h +1 -1
  33. data/ext/zstdruby/libzstd/compress/fse_compress.c +7 -124
  34. data/ext/zstdruby/libzstd/compress/hist.c +1 -1
  35. data/ext/zstdruby/libzstd/compress/hist.h +1 -1
  36. data/ext/zstdruby/libzstd/compress/huf_compress.c +234 -169
  37. data/ext/zstdruby/libzstd/compress/zstd_compress.c +1317 -594
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +272 -165
  39. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +115 -39
  40. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +16 -8
  41. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +13 -13
  42. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +1 -1
  43. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +25 -21
  44. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +1 -1
  45. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +162 -82
  46. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +95 -33
  47. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +3 -2
  48. data/ext/zstdruby/libzstd/compress/zstd_fast.c +434 -149
  49. data/ext/zstdruby/libzstd/compress/zstd_fast.h +3 -2
  50. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +405 -348
  51. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +4 -2
  52. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +9 -7
  53. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +1 -1
  54. data/ext/zstdruby/libzstd/compress/zstd_ldm_geartab.h +1 -1
  55. data/ext/zstdruby/libzstd/compress/zstd_opt.c +149 -100
  56. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  57. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +32 -16
  58. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +5 -2
  59. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +434 -441
  60. data/ext/zstdruby/libzstd/decompress/huf_decompress_amd64.S +42 -37
  61. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +4 -4
  62. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +1 -1
  63. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +205 -80
  64. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +201 -81
  65. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +6 -1
  66. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +4 -2
  67. data/ext/zstdruby/libzstd/dictBuilder/cover.c +19 -15
  68. data/ext/zstdruby/libzstd/dictBuilder/cover.h +1 -1
  69. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +2 -2
  70. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +13 -91
  71. data/ext/zstdruby/libzstd/zdict.h +53 -31
  72. data/ext/zstdruby/libzstd/zstd.h +580 -135
  73. data/ext/zstdruby/libzstd/zstd_errors.h +27 -8
  74. data/ext/zstdruby/main.c +20 -0
  75. data/ext/zstdruby/skippable_frame.c +63 -0
  76. data/ext/zstdruby/streaming_compress.c +177 -0
  77. data/ext/zstdruby/streaming_compress.h +5 -0
  78. data/ext/zstdruby/streaming_decompress.c +123 -0
  79. data/ext/zstdruby/zstdruby.c +113 -31
  80. data/lib/zstd-ruby/version.rb +1 -1
  81. data/lib/zstd-ruby.rb +0 -1
  82. data/zstd-ruby.gemspec +1 -1
  83. metadata +11 -37
  84. data/.github/dependabot.yml +0 -8
  85. data/.github/workflows/ruby.yml +0 -35
  86. data/ext/zstdruby/libzstd/.gitignore +0 -3
  87. data/ext/zstdruby/libzstd/BUCK +0 -232
  88. data/ext/zstdruby/libzstd/Makefile +0 -357
  89. data/ext/zstdruby/libzstd/README.md +0 -217
  90. data/ext/zstdruby/libzstd/deprecated/zbuff.h +0 -214
  91. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +0 -26
  92. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +0 -167
  93. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +0 -75
  94. data/ext/zstdruby/libzstd/dll/example/Makefile +0 -48
  95. data/ext/zstdruby/libzstd/dll/example/README.md +0 -63
  96. data/ext/zstdruby/libzstd/dll/example/build_package.bat +0 -20
  97. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +0 -25
  98. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +0 -181
  99. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +0 -415
  100. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +0 -2158
  101. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +0 -94
  102. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +0 -3518
  103. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +0 -93
  104. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +0 -3160
  105. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +0 -93
  106. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +0 -3647
  107. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +0 -142
  108. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +0 -4050
  109. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +0 -162
  110. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +0 -4154
  111. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +0 -172
  112. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +0 -4541
  113. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +0 -187
  114. data/ext/zstdruby/libzstd/libzstd.mk +0 -185
  115. data/ext/zstdruby/libzstd/libzstd.pc.in +0 -16
  116. data/ext/zstdruby/libzstd/modulemap/module.modulemap +0 -4
  117. data/ext/zstdruby/zstdruby.h +0 -6
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -16,7 +16,7 @@
16
16
  #define ZSTD_LITFREQ_ADD 2 /* scaling factor for litFreq, so that frequencies adapt faster to new stats */
17
17
  #define ZSTD_MAX_PRICE (1<<30)
18
18
 
19
- #define ZSTD_PREDEF_THRESHOLD 1024 /* if srcSize < ZSTD_PREDEF_THRESHOLD, symbols' cost is assumed static, directly determined by pre-defined distributions */
19
+ #define ZSTD_PREDEF_THRESHOLD 8 /* if srcSize < ZSTD_PREDEF_THRESHOLD, symbols' cost is assumed static, directly determined by pre-defined distributions */
20
20
 
21
21
 
22
22
  /*-*************************************
@@ -26,27 +26,35 @@
26
26
  #if 0 /* approximation at bit level (for tests) */
27
27
  # define BITCOST_ACCURACY 0
28
28
  # define BITCOST_MULTIPLIER (1 << BITCOST_ACCURACY)
29
- # define WEIGHT(stat, opt) ((void)opt, ZSTD_bitWeight(stat))
29
+ # define WEIGHT(stat, opt) ((void)(opt), ZSTD_bitWeight(stat))
30
30
  #elif 0 /* fractional bit accuracy (for tests) */
31
31
  # define BITCOST_ACCURACY 8
32
32
  # define BITCOST_MULTIPLIER (1 << BITCOST_ACCURACY)
33
- # define WEIGHT(stat,opt) ((void)opt, ZSTD_fracWeight(stat))
33
+ # define WEIGHT(stat,opt) ((void)(opt), ZSTD_fracWeight(stat))
34
34
  #else /* opt==approx, ultra==accurate */
35
35
  # define BITCOST_ACCURACY 8
36
36
  # define BITCOST_MULTIPLIER (1 << BITCOST_ACCURACY)
37
- # define WEIGHT(stat,opt) (opt ? ZSTD_fracWeight(stat) : ZSTD_bitWeight(stat))
37
+ # define WEIGHT(stat,opt) ((opt) ? ZSTD_fracWeight(stat) : ZSTD_bitWeight(stat))
38
38
  #endif
39
39
 
40
+ /* ZSTD_bitWeight() :
41
+ * provide estimated "cost" of a stat in full bits only */
40
42
  MEM_STATIC U32 ZSTD_bitWeight(U32 stat)
41
43
  {
42
44
  return (ZSTD_highbit32(stat+1) * BITCOST_MULTIPLIER);
43
45
  }
44
46
 
47
+ /* ZSTD_fracWeight() :
48
+ * provide fractional-bit "cost" of a stat,
49
+ * using linear interpolation approximation */
45
50
  MEM_STATIC U32 ZSTD_fracWeight(U32 rawStat)
46
51
  {
47
52
  U32 const stat = rawStat + 1;
48
53
  U32 const hb = ZSTD_highbit32(stat);
49
54
  U32 const BWeight = hb * BITCOST_MULTIPLIER;
55
+ /* Fweight was meant for "Fractional weight"
56
+ * but it's effectively a value between 1 and 2
57
+ * using fixed point arithmetic */
50
58
  U32 const FWeight = (stat << BITCOST_ACCURACY) >> hb;
51
59
  U32 const weight = BWeight + FWeight;
52
60
  assert(hb + BITCOST_ACCURACY < 31);
@@ -57,7 +65,7 @@ MEM_STATIC U32 ZSTD_fracWeight(U32 rawStat)
57
65
  /* debugging function,
58
66
  * @return price in bytes as fractional value
59
67
  * for debug messages only */
60
- MEM_STATIC double ZSTD_fCost(U32 price)
68
+ MEM_STATIC double ZSTD_fCost(int price)
61
69
  {
62
70
  return (double)price / (BITCOST_MULTIPLIER*8);
63
71
  }
@@ -88,20 +96,26 @@ static U32 sum_u32(const unsigned table[], size_t nbElts)
88
96
  return total;
89
97
  }
90
98
 
91
- static U32 ZSTD_downscaleStats(unsigned* table, U32 lastEltIndex, U32 shift)
99
+ typedef enum { base_0possible=0, base_1guaranteed=1 } base_directive_e;
100
+
101
+ static U32
102
+ ZSTD_downscaleStats(unsigned* table, U32 lastEltIndex, U32 shift, base_directive_e base1)
92
103
  {
93
104
  U32 s, sum=0;
94
- DEBUGLOG(5, "ZSTD_downscaleStats (nbElts=%u, shift=%u)", (unsigned)lastEltIndex+1, (unsigned)shift);
105
+ DEBUGLOG(5, "ZSTD_downscaleStats (nbElts=%u, shift=%u)",
106
+ (unsigned)lastEltIndex+1, (unsigned)shift );
95
107
  assert(shift < 30);
96
108
  for (s=0; s<lastEltIndex+1; s++) {
97
- table[s] = 1 + (table[s] >> shift);
98
- sum += table[s];
109
+ unsigned const base = base1 ? 1 : (table[s]>0);
110
+ unsigned const newStat = base + (table[s] >> shift);
111
+ sum += newStat;
112
+ table[s] = newStat;
99
113
  }
100
114
  return sum;
101
115
  }
102
116
 
103
117
  /* ZSTD_scaleStats() :
104
- * reduce all elements in table is sum too large
118
+ * reduce all elt frequencies in table if sum too large
105
119
  * return the resulting sum of elements */
106
120
  static U32 ZSTD_scaleStats(unsigned* table, U32 lastEltIndex, U32 logTarget)
107
121
  {
@@ -110,7 +124,7 @@ static U32 ZSTD_scaleStats(unsigned* table, U32 lastEltIndex, U32 logTarget)
110
124
  DEBUGLOG(5, "ZSTD_scaleStats (nbElts=%u, target=%u)", (unsigned)lastEltIndex+1, (unsigned)logTarget);
111
125
  assert(logTarget < 30);
112
126
  if (factor <= 1) return prevsum;
113
- return ZSTD_downscaleStats(table, lastEltIndex, ZSTD_highbit32(factor));
127
+ return ZSTD_downscaleStats(table, lastEltIndex, ZSTD_highbit32(factor), base_1guaranteed);
114
128
  }
115
129
 
116
130
  /* ZSTD_rescaleFreqs() :
@@ -129,18 +143,22 @@ ZSTD_rescaleFreqs(optState_t* const optPtr,
129
143
  DEBUGLOG(5, "ZSTD_rescaleFreqs (srcSize=%u)", (unsigned)srcSize);
130
144
  optPtr->priceType = zop_dynamic;
131
145
 
132
- if (optPtr->litLengthSum == 0) { /* first block : init */
133
- if (srcSize <= ZSTD_PREDEF_THRESHOLD) { /* heuristic */
134
- DEBUGLOG(5, "(srcSize <= ZSTD_PREDEF_THRESHOLD) => zop_predef");
146
+ if (optPtr->litLengthSum == 0) { /* no literals stats collected -> first block assumed -> init */
147
+
148
+ /* heuristic: use pre-defined stats for too small inputs */
149
+ if (srcSize <= ZSTD_PREDEF_THRESHOLD) {
150
+ DEBUGLOG(5, "srcSize <= %i : use predefined stats", ZSTD_PREDEF_THRESHOLD);
135
151
  optPtr->priceType = zop_predef;
136
152
  }
137
153
 
138
154
  assert(optPtr->symbolCosts != NULL);
139
155
  if (optPtr->symbolCosts->huf.repeatMode == HUF_repeat_valid) {
140
- /* huffman table presumed generated by dictionary */
156
+
157
+ /* huffman stats covering the full value set : table presumed generated by dictionary */
141
158
  optPtr->priceType = zop_dynamic;
142
159
 
143
160
  if (compressedLiterals) {
161
+ /* generate literals statistics from huffman table */
144
162
  unsigned lit;
145
163
  assert(optPtr->litFreq != NULL);
146
164
  optPtr->litSum = 0;
@@ -188,13 +206,14 @@ ZSTD_rescaleFreqs(optState_t* const optPtr,
188
206
  optPtr->offCodeSum += optPtr->offCodeFreq[of];
189
207
  } }
190
208
 
191
- } else { /* not a dictionary */
209
+ } else { /* first block, no dictionary */
192
210
 
193
211
  assert(optPtr->litFreq != NULL);
194
212
  if (compressedLiterals) {
213
+ /* base initial cost of literals on direct frequency within src */
195
214
  unsigned lit = MaxLit;
196
215
  HIST_count_simple(optPtr->litFreq, &lit, src, srcSize); /* use raw first block to init statistics */
197
- optPtr->litSum = ZSTD_downscaleStats(optPtr->litFreq, MaxLit, 8);
216
+ optPtr->litSum = ZSTD_downscaleStats(optPtr->litFreq, MaxLit, 8, base_0possible);
198
217
  }
199
218
 
200
219
  { unsigned const baseLLfreqs[MaxLL+1] = {
@@ -204,7 +223,8 @@ ZSTD_rescaleFreqs(optState_t* const optPtr,
204
223
  1, 1, 1, 1, 1, 1, 1, 1,
205
224
  1, 1, 1, 1
206
225
  };
207
- ZSTD_memcpy(optPtr->litLengthFreq, baseLLfreqs, sizeof(baseLLfreqs)); optPtr->litLengthSum = sum_u32(baseLLfreqs, MaxLL+1);
226
+ ZSTD_memcpy(optPtr->litLengthFreq, baseLLfreqs, sizeof(baseLLfreqs));
227
+ optPtr->litLengthSum = sum_u32(baseLLfreqs, MaxLL+1);
208
228
  }
209
229
 
210
230
  { unsigned ml;
@@ -219,13 +239,13 @@ ZSTD_rescaleFreqs(optState_t* const optPtr,
219
239
  1, 1, 1, 1, 1, 1, 1, 1,
220
240
  1, 1, 1, 1, 1, 1, 1, 1
221
241
  };
222
- ZSTD_memcpy(optPtr->offCodeFreq, baseOFCfreqs, sizeof(baseOFCfreqs)); optPtr->offCodeSum = sum_u32(baseOFCfreqs, MaxOff+1);
242
+ ZSTD_memcpy(optPtr->offCodeFreq, baseOFCfreqs, sizeof(baseOFCfreqs));
243
+ optPtr->offCodeSum = sum_u32(baseOFCfreqs, MaxOff+1);
223
244
  }
224
245
 
225
-
226
246
  }
227
247
 
228
- } else { /* new block : re-use previous statistics, scaled down */
248
+ } else { /* new block : scale down accumulated statistics */
229
249
 
230
250
  if (compressedLiterals)
231
251
  optPtr->litSum = ZSTD_scaleStats(optPtr->litFreq, MaxLit, 12);
@@ -253,11 +273,14 @@ static U32 ZSTD_rawLiteralsCost(const BYTE* const literals, U32 const litLength,
253
273
  return (litLength*6) * BITCOST_MULTIPLIER; /* 6 bit per literal - no statistic used */
254
274
 
255
275
  /* dynamic statistics */
256
- { U32 price = litLength * optPtr->litSumBasePrice;
276
+ { U32 price = optPtr->litSumBasePrice * litLength;
277
+ U32 const litPriceMax = optPtr->litSumBasePrice - BITCOST_MULTIPLIER;
257
278
  U32 u;
279
+ assert(optPtr->litSumBasePrice >= BITCOST_MULTIPLIER);
258
280
  for (u=0; u < litLength; u++) {
259
- assert(WEIGHT(optPtr->litFreq[literals[u]], optLevel) <= optPtr->litSumBasePrice); /* literal cost should never be negative */
260
- price -= WEIGHT(optPtr->litFreq[literals[u]], optLevel);
281
+ U32 litPrice = WEIGHT(optPtr->litFreq[literals[u]], optLevel);
282
+ if (UNLIKELY(litPrice > litPriceMax)) litPrice = litPriceMax;
283
+ price -= litPrice;
261
284
  }
262
285
  return price;
263
286
  }
@@ -267,7 +290,17 @@ static U32 ZSTD_rawLiteralsCost(const BYTE* const literals, U32 const litLength,
267
290
  * cost of literalLength symbol */
268
291
  static U32 ZSTD_litLengthPrice(U32 const litLength, const optState_t* const optPtr, int optLevel)
269
292
  {
270
- if (optPtr->priceType == zop_predef) return WEIGHT(litLength, optLevel);
293
+ assert(litLength <= ZSTD_BLOCKSIZE_MAX);
294
+ if (optPtr->priceType == zop_predef)
295
+ return WEIGHT(litLength, optLevel);
296
+
297
+ /* ZSTD_LLcode() can't compute litLength price for sizes >= ZSTD_BLOCKSIZE_MAX
298
+ * because it isn't representable in the zstd format.
299
+ * So instead just pretend it would cost 1 bit more than ZSTD_BLOCKSIZE_MAX - 1.
300
+ * In such a case, the block would be all literals.
301
+ */
302
+ if (litLength == ZSTD_BLOCKSIZE_MAX)
303
+ return BITCOST_MULTIPLIER + ZSTD_litLengthPrice(ZSTD_BLOCKSIZE_MAX - 1, optPtr, optLevel);
271
304
 
272
305
  /* dynamic statistics */
273
306
  { U32 const llCode = ZSTD_LLcode(litLength);
@@ -278,22 +311,25 @@ static U32 ZSTD_litLengthPrice(U32 const litLength, const optState_t* const optP
278
311
  }
279
312
 
280
313
  /* ZSTD_getMatchPrice() :
281
- * Provides the cost of the match part (offset + matchLength) of a sequence
314
+ * Provides the cost of the match part (offset + matchLength) of a sequence.
282
315
  * Must be combined with ZSTD_fullLiteralsCost() to get the full cost of a sequence.
283
- * optLevel: when <2, favors small offset for decompression speed (improved cache efficiency) */
316
+ * @offBase : sumtype, representing an offset or a repcode, and using numeric representation of ZSTD_storeSeq()
317
+ * @optLevel: when <2, favors small offset for decompression speed (improved cache efficiency)
318
+ */
284
319
  FORCE_INLINE_TEMPLATE U32
285
- ZSTD_getMatchPrice(U32 const offset,
320
+ ZSTD_getMatchPrice(U32 const offBase,
286
321
  U32 const matchLength,
287
322
  const optState_t* const optPtr,
288
323
  int const optLevel)
289
324
  {
290
325
  U32 price;
291
- U32 const offCode = ZSTD_highbit32(offset+1);
326
+ U32 const offCode = ZSTD_highbit32(offBase);
292
327
  U32 const mlBase = matchLength - MINMATCH;
293
328
  assert(matchLength >= MINMATCH);
294
329
 
295
- if (optPtr->priceType == zop_predef) /* fixed scheme, do not use statistics */
296
- return WEIGHT(mlBase, optLevel) + ((16 + offCode) * BITCOST_MULTIPLIER);
330
+ if (optPtr->priceType == zop_predef) /* fixed scheme, does not use statistics */
331
+ return WEIGHT(mlBase, optLevel)
332
+ + ((16 + offCode) * BITCOST_MULTIPLIER); /* emulated offset cost */
297
333
 
298
334
  /* dynamic statistics */
299
335
  price = (offCode * BITCOST_MULTIPLIER) + (optPtr->offCodeSumBasePrice - WEIGHT(optPtr->offCodeFreq[offCode], optLevel));
@@ -312,10 +348,10 @@ ZSTD_getMatchPrice(U32 const offset,
312
348
  }
313
349
 
314
350
  /* ZSTD_updateStats() :
315
- * assumption : literals + litLengtn <= iend */
351
+ * assumption : literals + litLength <= iend */
316
352
  static void ZSTD_updateStats(optState_t* const optPtr,
317
353
  U32 litLength, const BYTE* literals,
318
- U32 offsetCode, U32 matchLength)
354
+ U32 offBase, U32 matchLength)
319
355
  {
320
356
  /* literals */
321
357
  if (ZSTD_compressedLiterals(optPtr)) {
@@ -331,8 +367,8 @@ static void ZSTD_updateStats(optState_t* const optPtr,
331
367
  optPtr->litLengthSum++;
332
368
  }
333
369
 
334
- /* match offset code (0-2=>repCode; 3+=>offset+2) */
335
- { U32 const offCode = ZSTD_highbit32(offsetCode+1);
370
+ /* offset code : follows storeSeq() numeric representation */
371
+ { U32 const offCode = ZSTD_highbit32(offBase);
336
372
  assert(offCode <= MaxOff);
337
373
  optPtr->offCodeFreq[offCode]++;
338
374
  optPtr->offCodeSum++;
@@ -539,16 +575,17 @@ void ZSTD_updateTree(ZSTD_matchState_t* ms, const BYTE* ip, const BYTE* iend) {
539
575
  ZSTD_updateTree_internal(ms, ip, iend, ms->cParams.minMatch, ZSTD_noDict);
540
576
  }
541
577
 
542
- FORCE_INLINE_TEMPLATE
543
- U32 ZSTD_insertBtAndGetAllMatches (
544
- ZSTD_match_t* matches, /* store result (found matches) in this table (presumed large enough) */
545
- ZSTD_matchState_t* ms,
546
- U32* nextToUpdate3,
547
- const BYTE* const ip, const BYTE* const iLimit, const ZSTD_dictMode_e dictMode,
548
- const U32 rep[ZSTD_REP_NUM],
549
- U32 const ll0, /* tells if associated literal length is 0 or not. This value must be 0 or 1 */
550
- const U32 lengthToBeat,
551
- U32 const mls /* template */)
578
+ FORCE_INLINE_TEMPLATE U32
579
+ ZSTD_insertBtAndGetAllMatches (
580
+ ZSTD_match_t* matches, /* store result (found matches) in this table (presumed large enough) */
581
+ ZSTD_matchState_t* ms,
582
+ U32* nextToUpdate3,
583
+ const BYTE* const ip, const BYTE* const iLimit,
584
+ const ZSTD_dictMode_e dictMode,
585
+ const U32 rep[ZSTD_REP_NUM],
586
+ const U32 ll0, /* tells if associated literal length is 0 or not. This value must be 0 or 1 */
587
+ const U32 lengthToBeat,
588
+ const U32 mls /* template */)
552
589
  {
553
590
  const ZSTD_compressionParameters* const cParams = &ms->cParams;
554
591
  U32 const sufficient_len = MIN(cParams->targetLength, ZSTD_OPT_NUM -1);
@@ -631,7 +668,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
631
668
  DEBUGLOG(8, "found repCode %u (ll0:%u, offset:%u) of length %u",
632
669
  repCode, ll0, repOffset, repLen);
633
670
  bestLength = repLen;
634
- matches[mnum].off = repCode - ll0;
671
+ matches[mnum].off = REPCODE_TO_OFFBASE(repCode - ll0 + 1); /* expect value between 1 and 3 */
635
672
  matches[mnum].len = (U32)repLen;
636
673
  mnum++;
637
674
  if ( (repLen > sufficient_len)
@@ -660,7 +697,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
660
697
  bestLength = mlen;
661
698
  assert(curr > matchIndex3);
662
699
  assert(mnum==0); /* no prior solution */
663
- matches[0].off = (curr - matchIndex3) + ZSTD_REP_MOVE;
700
+ matches[0].off = OFFSET_TO_OFFBASE(curr - matchIndex3);
664
701
  matches[0].len = (U32)mlen;
665
702
  mnum = 1;
666
703
  if ( (mlen > sufficient_len) |
@@ -693,13 +730,13 @@ U32 ZSTD_insertBtAndGetAllMatches (
693
730
  }
694
731
 
695
732
  if (matchLength > bestLength) {
696
- DEBUGLOG(8, "found match of length %u at distance %u (offCode=%u)",
697
- (U32)matchLength, curr - matchIndex, curr - matchIndex + ZSTD_REP_MOVE);
733
+ DEBUGLOG(8, "found match of length %u at distance %u (offBase=%u)",
734
+ (U32)matchLength, curr - matchIndex, OFFSET_TO_OFFBASE(curr - matchIndex));
698
735
  assert(matchEndIdx > matchIndex);
699
736
  if (matchLength > matchEndIdx - matchIndex)
700
737
  matchEndIdx = matchIndex + (U32)matchLength;
701
738
  bestLength = matchLength;
702
- matches[mnum].off = (curr - matchIndex) + ZSTD_REP_MOVE;
739
+ matches[mnum].off = OFFSET_TO_OFFBASE(curr - matchIndex);
703
740
  matches[mnum].len = (U32)matchLength;
704
741
  mnum++;
705
742
  if ( (matchLength > ZSTD_OPT_NUM)
@@ -741,12 +778,12 @@ U32 ZSTD_insertBtAndGetAllMatches (
741
778
 
742
779
  if (matchLength > bestLength) {
743
780
  matchIndex = dictMatchIndex + dmsIndexDelta;
744
- DEBUGLOG(8, "found dms match of length %u at distance %u (offCode=%u)",
745
- (U32)matchLength, curr - matchIndex, curr - matchIndex + ZSTD_REP_MOVE);
781
+ DEBUGLOG(8, "found dms match of length %u at distance %u (offBase=%u)",
782
+ (U32)matchLength, curr - matchIndex, OFFSET_TO_OFFBASE(curr - matchIndex));
746
783
  if (matchLength > matchEndIdx - matchIndex)
747
784
  matchEndIdx = matchIndex + (U32)matchLength;
748
785
  bestLength = matchLength;
749
- matches[mnum].off = (curr - matchIndex) + ZSTD_REP_MOVE;
786
+ matches[mnum].off = OFFSET_TO_OFFBASE(curr - matchIndex);
750
787
  matches[mnum].len = (U32)matchLength;
751
788
  mnum++;
752
789
  if ( (matchLength > ZSTD_OPT_NUM)
@@ -835,7 +872,8 @@ GEN_ZSTD_BT_GET_ALL_MATCHES(dictMatchState)
835
872
  ZSTD_BT_GET_ALL_MATCHES_FN(dictMode, 6) \
836
873
  }
837
874
 
838
- static ZSTD_getAllMatchesFn ZSTD_selectBtGetAllMatches(ZSTD_matchState_t const* ms, ZSTD_dictMode_e const dictMode)
875
+ static ZSTD_getAllMatchesFn
876
+ ZSTD_selectBtGetAllMatches(ZSTD_matchState_t const* ms, ZSTD_dictMode_e const dictMode)
839
877
  {
840
878
  ZSTD_getAllMatchesFn const getAllMatchesFns[3][4] = {
841
879
  ZSTD_BT_GET_ALL_MATCHES_ARRAY(noDict),
@@ -854,16 +892,18 @@ static ZSTD_getAllMatchesFn ZSTD_selectBtGetAllMatches(ZSTD_matchState_t const*
854
892
 
855
893
  /* Struct containing info needed to make decision about ldm inclusion */
856
894
  typedef struct {
857
- rawSeqStore_t seqStore; /* External match candidates store for this block */
858
- U32 startPosInBlock; /* Start position of the current match candidate */
859
- U32 endPosInBlock; /* End position of the current match candidate */
860
- U32 offset; /* Offset of the match candidate */
895
+ rawSeqStore_t seqStore; /* External match candidates store for this block */
896
+ U32 startPosInBlock; /* Start position of the current match candidate */
897
+ U32 endPosInBlock; /* End position of the current match candidate */
898
+ U32 offset; /* Offset of the match candidate */
861
899
  } ZSTD_optLdm_t;
862
900
 
863
901
  /* ZSTD_optLdm_skipRawSeqStoreBytes():
864
- * Moves forward in rawSeqStore by nbBytes, which will update the fields 'pos' and 'posInSequence'.
902
+ * Moves forward in @rawSeqStore by @nbBytes,
903
+ * which will update the fields 'pos' and 'posInSequence'.
865
904
  */
866
- static void ZSTD_optLdm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t nbBytes) {
905
+ static void ZSTD_optLdm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t nbBytes)
906
+ {
867
907
  U32 currPos = (U32)(rawSeqStore->posInSequence + nbBytes);
868
908
  while (currPos && rawSeqStore->pos < rawSeqStore->size) {
869
909
  rawSeq currSeq = rawSeqStore->seq[rawSeqStore->pos];
@@ -884,8 +924,10 @@ static void ZSTD_optLdm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, size_t
884
924
  * Calculates the beginning and end of the next match in the current block.
885
925
  * Updates 'pos' and 'posInSequence' of the ldmSeqStore.
886
926
  */
887
- static void ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, U32 currPosInBlock,
888
- U32 blockBytesRemaining) {
927
+ static void
928
+ ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, U32 currPosInBlock,
929
+ U32 blockBytesRemaining)
930
+ {
889
931
  rawSeq currSeq;
890
932
  U32 currBlockEndPos;
891
933
  U32 literalsBytesRemaining;
@@ -897,8 +939,8 @@ static void ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, U32 cu
897
939
  optLdm->endPosInBlock = UINT_MAX;
898
940
  return;
899
941
  }
900
- /* Calculate appropriate bytes left in matchLength and litLength after adjusting
901
- based on ldmSeqStore->posInSequence */
942
+ /* Calculate appropriate bytes left in matchLength and litLength
943
+ * after adjusting based on ldmSeqStore->posInSequence */
902
944
  currSeq = optLdm->seqStore.seq[optLdm->seqStore.pos];
903
945
  assert(optLdm->seqStore.posInSequence <= currSeq.litLength + currSeq.matchLength);
904
946
  currBlockEndPos = currPosInBlock + blockBytesRemaining;
@@ -934,15 +976,16 @@ static void ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, U32 cu
934
976
  }
935
977
 
936
978
  /* ZSTD_optLdm_maybeAddMatch():
937
- * Adds a match if it's long enough, based on it's 'matchStartPosInBlock'
938
- * and 'matchEndPosInBlock', into 'matches'. Maintains the correct ordering of 'matches'
979
+ * Adds a match if it's long enough,
980
+ * based on it's 'matchStartPosInBlock' and 'matchEndPosInBlock',
981
+ * into 'matches'. Maintains the correct ordering of 'matches'.
939
982
  */
940
983
  static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, U32* nbMatches,
941
- ZSTD_optLdm_t* optLdm, U32 currPosInBlock) {
942
- U32 posDiff = currPosInBlock - optLdm->startPosInBlock;
943
- /* Note: ZSTD_match_t actually contains offCode and matchLength (before subtracting MINMATCH) */
944
- U32 candidateMatchLength = optLdm->endPosInBlock - optLdm->startPosInBlock - posDiff;
945
- U32 candidateOffCode = optLdm->offset + ZSTD_REP_MOVE;
984
+ const ZSTD_optLdm_t* optLdm, U32 currPosInBlock)
985
+ {
986
+ U32 const posDiff = currPosInBlock - optLdm->startPosInBlock;
987
+ /* Note: ZSTD_match_t actually contains offBase and matchLength (before subtracting MINMATCH) */
988
+ U32 const candidateMatchLength = optLdm->endPosInBlock - optLdm->startPosInBlock - posDiff;
946
989
 
947
990
  /* Ensure that current block position is not outside of the match */
948
991
  if (currPosInBlock < optLdm->startPosInBlock
@@ -952,10 +995,11 @@ static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, U32* nbMatches,
952
995
  }
953
996
 
954
997
  if (*nbMatches == 0 || ((candidateMatchLength > matches[*nbMatches-1].len) && *nbMatches < ZSTD_OPT_NUM)) {
955
- DEBUGLOG(6, "ZSTD_optLdm_maybeAddMatch(): Adding ldm candidate match (offCode: %u matchLength %u) at block position=%u",
956
- candidateOffCode, candidateMatchLength, currPosInBlock);
998
+ U32 const candidateOffBase = OFFSET_TO_OFFBASE(optLdm->offset);
999
+ DEBUGLOG(6, "ZSTD_optLdm_maybeAddMatch(): Adding ldm candidate match (offBase: %u matchLength %u) at block position=%u",
1000
+ candidateOffBase, candidateMatchLength, currPosInBlock);
957
1001
  matches[*nbMatches].len = candidateMatchLength;
958
- matches[*nbMatches].off = candidateOffCode;
1002
+ matches[*nbMatches].off = candidateOffBase;
959
1003
  (*nbMatches)++;
960
1004
  }
961
1005
  }
@@ -963,8 +1007,11 @@ static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, U32* nbMatches,
963
1007
  /* ZSTD_optLdm_processMatchCandidate():
964
1008
  * Wrapper function to update ldm seq store and call ldm functions as necessary.
965
1009
  */
966
- static void ZSTD_optLdm_processMatchCandidate(ZSTD_optLdm_t* optLdm, ZSTD_match_t* matches, U32* nbMatches,
967
- U32 currPosInBlock, U32 remainingBytes) {
1010
+ static void
1011
+ ZSTD_optLdm_processMatchCandidate(ZSTD_optLdm_t* optLdm,
1012
+ ZSTD_match_t* matches, U32* nbMatches,
1013
+ U32 currPosInBlock, U32 remainingBytes)
1014
+ {
968
1015
  if (optLdm->seqStore.size == 0 || optLdm->seqStore.pos >= optLdm->seqStore.size) {
969
1016
  return;
970
1017
  }
@@ -975,7 +1022,7 @@ static void ZSTD_optLdm_processMatchCandidate(ZSTD_optLdm_t* optLdm, ZSTD_match_
975
1022
  * at the end of a match from the ldm seq store, and will often be some bytes
976
1023
  * over beyond matchEndPosInBlock. As such, we need to correct for these "overshoots"
977
1024
  */
978
- U32 posOvershoot = currPosInBlock - optLdm->endPosInBlock;
1025
+ U32 const posOvershoot = currPosInBlock - optLdm->endPosInBlock;
979
1026
  ZSTD_optLdm_skipRawSeqStoreBytes(&optLdm->seqStore, posOvershoot);
980
1027
  }
981
1028
  ZSTD_opt_getNextMatchAndUpdateSeqStore(optLdm, currPosInBlock, remainingBytes);
@@ -1039,6 +1086,8 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1039
1086
  ZSTD_optimal_t lastSequence;
1040
1087
  ZSTD_optLdm_t optLdm;
1041
1088
 
1089
+ ZSTD_memset(&lastSequence, 0, sizeof(ZSTD_optimal_t));
1090
+
1042
1091
  optLdm.seqStore = ms->ldmSeqStore ? *ms->ldmSeqStore : kNullRawSeqStore;
1043
1092
  optLdm.endPosInBlock = optLdm.startPosInBlock = optLdm.offset = 0;
1044
1093
  ZSTD_opt_getNextMatchAndUpdateSeqStore(&optLdm, (U32)(ip-istart), (U32)(iend-ip));
@@ -1075,14 +1124,14 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1075
1124
 
1076
1125
  /* large match -> immediate encoding */
1077
1126
  { U32 const maxML = matches[nbMatches-1].len;
1078
- U32 const maxOffset = matches[nbMatches-1].off;
1079
- DEBUGLOG(6, "found %u matches of maxLength=%u and maxOffCode=%u at cPos=%u => start new series",
1080
- nbMatches, maxML, maxOffset, (U32)(ip-prefixStart));
1127
+ U32 const maxOffBase = matches[nbMatches-1].off;
1128
+ DEBUGLOG(6, "found %u matches of maxLength=%u and maxOffBase=%u at cPos=%u => start new series",
1129
+ nbMatches, maxML, maxOffBase, (U32)(ip-prefixStart));
1081
1130
 
1082
1131
  if (maxML > sufficient_len) {
1083
1132
  lastSequence.litlen = litlen;
1084
1133
  lastSequence.mlen = maxML;
1085
- lastSequence.off = maxOffset;
1134
+ lastSequence.off = maxOffBase;
1086
1135
  DEBUGLOG(6, "large match (%u>%u), immediate encoding",
1087
1136
  maxML, sufficient_len);
1088
1137
  cur = 0;
@@ -1099,15 +1148,15 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1099
1148
  opt[pos].price = ZSTD_MAX_PRICE; /* mlen, litlen and price will be fixed during forward scanning */
1100
1149
  }
1101
1150
  for (matchNb = 0; matchNb < nbMatches; matchNb++) {
1102
- U32 const offset = matches[matchNb].off;
1151
+ U32 const offBase = matches[matchNb].off;
1103
1152
  U32 const end = matches[matchNb].len;
1104
1153
  for ( ; pos <= end ; pos++ ) {
1105
- U32 const matchPrice = ZSTD_getMatchPrice(offset, pos, optStatePtr, optLevel);
1154
+ U32 const matchPrice = ZSTD_getMatchPrice(offBase, pos, optStatePtr, optLevel);
1106
1155
  U32 const sequencePrice = literalsPrice + matchPrice;
1107
1156
  DEBUGLOG(7, "rPos:%u => set initial price : %.2f",
1108
- pos, ZSTD_fCost(sequencePrice));
1157
+ pos, ZSTD_fCost((int)sequencePrice));
1109
1158
  opt[pos].mlen = pos;
1110
- opt[pos].off = offset;
1159
+ opt[pos].off = offBase;
1111
1160
  opt[pos].litlen = litlen;
1112
1161
  opt[pos].price = (int)sequencePrice;
1113
1162
  } }
@@ -1152,7 +1201,7 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1152
1201
  assert(cur >= opt[cur].mlen);
1153
1202
  if (opt[cur].mlen != 0) {
1154
1203
  U32 const prev = cur - opt[cur].mlen;
1155
- repcodes_t newReps = ZSTD_updateRep(opt[prev].rep, opt[cur].off, opt[cur].litlen==0);
1204
+ repcodes_t const newReps = ZSTD_newRep(opt[prev].rep, opt[cur].off, opt[cur].litlen==0);
1156
1205
  ZSTD_memcpy(opt[cur].rep, &newReps, sizeof(repcodes_t));
1157
1206
  } else {
1158
1207
  ZSTD_memcpy(opt[cur].rep, opt[cur - 1].rep, sizeof(repcodes_t));
@@ -1207,7 +1256,7 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1207
1256
  U32 const startML = (matchNb>0) ? matches[matchNb-1].len+1 : minMatch;
1208
1257
  U32 mlen;
1209
1258
 
1210
- DEBUGLOG(7, "testing match %u => offCode=%4u, mlen=%2u, llen=%2u",
1259
+ DEBUGLOG(7, "testing match %u => offBase=%4u, mlen=%2u, llen=%2u",
1211
1260
  matchNb, matches[matchNb].off, lastML, litlen);
1212
1261
 
1213
1262
  for (mlen = lastML; mlen >= startML; mlen--) { /* scan downward */
@@ -1242,7 +1291,7 @@ _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */
1242
1291
  * update them while traversing the sequences.
1243
1292
  */
1244
1293
  if (lastSequence.mlen != 0) {
1245
- repcodes_t reps = ZSTD_updateRep(opt[cur].rep, lastSequence.off, lastSequence.litlen==0);
1294
+ repcodes_t const reps = ZSTD_newRep(opt[cur].rep, lastSequence.off, lastSequence.litlen==0);
1246
1295
  ZSTD_memcpy(rep, &reps, sizeof(reps));
1247
1296
  } else {
1248
1297
  ZSTD_memcpy(rep, opt[cur].rep, sizeof(repcodes_t));
@@ -1273,7 +1322,7 @@ _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */
1273
1322
  for (storePos=storeStart; storePos <= storeEnd; storePos++) {
1274
1323
  U32 const llen = opt[storePos].litlen;
1275
1324
  U32 const mlen = opt[storePos].mlen;
1276
- U32 const offCode = opt[storePos].off;
1325
+ U32 const offBase = opt[storePos].off;
1277
1326
  U32 const advance = llen + mlen;
1278
1327
  DEBUGLOG(6, "considering seq starting at %zi, llen=%u, mlen=%u",
1279
1328
  anchor - istart, (unsigned)llen, (unsigned)mlen);
@@ -1285,8 +1334,8 @@ _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */
1285
1334
  }
1286
1335
 
1287
1336
  assert(anchor + llen <= iend);
1288
- ZSTD_updateStats(optStatePtr, llen, anchor, offCode, mlen);
1289
- ZSTD_storeSeq(seqStore, llen, anchor, iend, offCode, mlen-MINMATCH);
1337
+ ZSTD_updateStats(optStatePtr, llen, anchor, offBase, mlen);
1338
+ ZSTD_storeSeq(seqStore, llen, anchor, iend, offBase, mlen);
1290
1339
  anchor += advance;
1291
1340
  ip = anchor;
1292
1341
  } }
@@ -1326,7 +1375,7 @@ size_t ZSTD_compressBlock_btopt(
1326
1375
  /* ZSTD_initStats_ultra():
1327
1376
  * make a first compression pass, just to seed stats with more accurate starting values.
1328
1377
  * only works on first block, with no dictionary and no ldm.
1329
- * this function cannot error, hence its contract must be respected.
1378
+ * this function cannot error out, its narrow contract must be respected.
1330
1379
  */
1331
1380
  static void
1332
1381
  ZSTD_initStats_ultra(ZSTD_matchState_t* ms,
@@ -1345,7 +1394,7 @@ ZSTD_initStats_ultra(ZSTD_matchState_t* ms,
1345
1394
 
1346
1395
  ZSTD_compressBlock_opt2(ms, seqStore, tmpRep, src, srcSize, ZSTD_noDict); /* generate stats into ms->opt*/
1347
1396
 
1348
- /* invalidate first scan from history */
1397
+ /* invalidate first scan from history, only keep entropy stats */
1349
1398
  ZSTD_resetSeqStore(seqStore);
1350
1399
  ms->window.base -= srcSize;
1351
1400
  ms->window.dictLimit += (U32)srcSize;
@@ -1369,20 +1418,20 @@ size_t ZSTD_compressBlock_btultra2(
1369
1418
  U32 const curr = (U32)((const BYTE*)src - ms->window.base);
1370
1419
  DEBUGLOG(5, "ZSTD_compressBlock_btultra2 (srcSize=%zu)", srcSize);
1371
1420
 
1372
- /* 2-pass strategy:
1421
+ /* 2-passes strategy:
1373
1422
  * this strategy makes a first pass over first block to collect statistics
1374
- * and seed next round's statistics with it.
1375
- * After 1st pass, function forgets everything, and starts a new block.
1423
+ * in order to seed next round's statistics with it.
1424
+ * After 1st pass, function forgets history, and starts a new block.
1376
1425
  * Consequently, this can only work if no data has been previously loaded in tables,
1377
1426
  * aka, no dictionary, no prefix, no ldm preprocessing.
1378
1427
  * The compression ratio gain is generally small (~0.5% on first block),
1379
- * the cost is 2x cpu time on first block. */
1428
+ ** the cost is 2x cpu time on first block. */
1380
1429
  assert(srcSize <= ZSTD_BLOCKSIZE_MAX);
1381
1430
  if ( (ms->opt.litLengthSum==0) /* first block */
1382
1431
  && (seqStore->sequences == seqStore->sequencesStart) /* no ldm */
1383
1432
  && (ms->window.dictLimit == ms->window.lowLimit) /* no dictionary */
1384
- && (curr == ms->window.dictLimit) /* start of frame, nothing already loaded nor skipped */
1385
- && (srcSize > ZSTD_PREDEF_THRESHOLD)
1433
+ && (curr == ms->window.dictLimit) /* start of frame, nothing already loaded nor skipped */
1434
+ && (srcSize > ZSTD_PREDEF_THRESHOLD) /* input large enough to not employ default stats */
1386
1435
  ) {
1387
1436
  ZSTD_initStats_ultra(ms, seqStore, rep, src, srcSize);
1388
1437
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) Yann Collet, Facebook, Inc.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the