zstdlib 0.9.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +13 -0
  3. data/ext/zstdlib_c/extconf.rb +3 -3
  4. data/ext/zstdlib_c/ruby/zlib-3.2/zstdlib.c +5090 -0
  5. data/ext/zstdlib_c/zlib-1.2.12/crc32.c +1116 -0
  6. data/ext/zstdlib_c/zlib-1.2.12/crc32.h +9446 -0
  7. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/deflate.c +78 -30
  8. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/deflate.h +12 -15
  9. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/gzguts.h +3 -2
  10. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/gzlib.c +5 -3
  11. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/gzread.c +5 -7
  12. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/gzwrite.c +25 -13
  13. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/infback.c +2 -1
  14. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/inffast.c +14 -14
  15. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/inflate.c +39 -8
  16. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/inflate.h +3 -2
  17. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/inftrees.c +3 -3
  18. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/trees.c +27 -48
  19. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/zlib.h +123 -100
  20. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/zutil.c +2 -2
  21. data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/zutil.h +12 -9
  22. data/ext/zstdlib_c/zstd-1.5.5/lib/common/allocations.h +55 -0
  23. data/ext/zstdlib_c/zstd-1.5.5/lib/common/bits.h +200 -0
  24. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/bitstream.h +19 -60
  25. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/compiler.h +26 -3
  26. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/cpu.h +1 -1
  27. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/debug.c +1 -1
  28. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/debug.h +1 -1
  29. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/entropy_common.c +12 -40
  30. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/error_private.c +9 -2
  31. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/error_private.h +1 -1
  32. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/fse.h +5 -83
  33. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/fse_decompress.c +7 -99
  34. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/huf.h +65 -156
  35. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/mem.h +39 -46
  36. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/pool.c +26 -10
  37. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/pool.h +7 -1
  38. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/portability_macros.h +22 -3
  39. data/ext/zstdlib_c/zstd-1.5.5/lib/common/threading.c +176 -0
  40. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/threading.h +5 -10
  41. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/xxhash.c +2 -2
  42. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/xxhash.h +8 -8
  43. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/zstd_common.c +1 -36
  44. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/zstd_deps.h +1 -1
  45. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/zstd_internal.h +17 -118
  46. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/common/zstd_trace.h +3 -3
  47. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/clevels.h +1 -1
  48. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/fse_compress.c +7 -124
  49. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/hist.c +1 -1
  50. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/hist.h +1 -1
  51. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/huf_compress.c +234 -169
  52. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_compress.c +1243 -538
  53. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_compress_internal.h +225 -151
  54. data/ext/zstdlib_c/zstd-1.5.5/lib/compress/zstd_compress_literals.c +235 -0
  55. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_compress_literals.h +16 -8
  56. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_compress_sequences.c +3 -3
  57. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_compress_sequences.h +1 -1
  58. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_compress_superblock.c +25 -21
  59. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_compress_superblock.h +1 -1
  60. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_cwksp.h +128 -62
  61. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_double_fast.c +95 -33
  62. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_double_fast.h +3 -2
  63. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_fast.c +433 -148
  64. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_fast.h +3 -2
  65. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_lazy.c +398 -345
  66. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_lazy.h +4 -2
  67. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_ldm.c +5 -5
  68. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_ldm.h +1 -1
  69. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_ldm_geartab.h +1 -1
  70. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_opt.c +106 -80
  71. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstd_opt.h +1 -1
  72. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstdmt_compress.c +17 -9
  73. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/compress/zstdmt_compress.h +1 -1
  74. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/decompress/huf_decompress.c +434 -441
  75. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/decompress/huf_decompress_amd64.S +30 -39
  76. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/decompress/zstd_ddict.c +4 -4
  77. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/decompress/zstd_ddict.h +1 -1
  78. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/decompress/zstd_decompress.c +205 -80
  79. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/decompress/zstd_decompress_block.c +201 -81
  80. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/decompress/zstd_decompress_block.h +6 -1
  81. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/decompress/zstd_decompress_internal.h +4 -2
  82. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/zdict.h +53 -31
  83. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/zstd.h +580 -135
  84. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/lib/zstd_errors.h +27 -8
  85. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/zlibWrapper/gzclose.c +1 -1
  86. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/zlibWrapper/gzcompatibility.h +8 -8
  87. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/zlibWrapper/gzguts.h +10 -10
  88. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/zlibWrapper/gzlib.c +3 -3
  89. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/zlibWrapper/gzread.c +10 -10
  90. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/zlibWrapper/gzwrite.c +5 -5
  91. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/zlibWrapper/zstd_zlibwrapper.c +46 -44
  92. data/ext/zstdlib_c/{zstd-1.5.2 → zstd-1.5.5}/zlibWrapper/zstd_zlibwrapper.h +4 -1
  93. metadata +103 -100
  94. data/ext/zstdlib_c/zlib-1.2.11/crc32.c +0 -442
  95. data/ext/zstdlib_c/zlib-1.2.11/crc32.h +0 -441
  96. data/ext/zstdlib_c/zstd-1.5.2/lib/common/threading.c +0 -122
  97. data/ext/zstdlib_c/zstd-1.5.2/lib/compress/zstd_compress_literals.c +0 -159
  98. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/adler32.c +0 -0
  99. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/compress.c +0 -0
  100. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/gzclose.c +0 -0
  101. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/inffast.h +0 -0
  102. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/inffixed.h +0 -0
  103. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/inftrees.h +0 -0
  104. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/trees.h +0 -0
  105. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/uncompr.c +0 -0
  106. /data/ext/zstdlib_c/{zlib-1.2.11 → zlib-1.2.12}/zconf.h +0 -0
@@ -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
@@ -25,6 +25,8 @@ extern "C" {
25
25
  */
26
26
  #define ZSTD_LAZY_DDSS_BUCKET_LOG 2
27
27
 
28
+ #define ZSTD_ROW_HASH_TAG_BITS 8 /* nb bits to use for the tag */
29
+
28
30
  U32 ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t* ms, const BYTE* ip);
29
31
  void ZSTD_row_update(ZSTD_matchState_t* const ms, const BYTE* ip);
30
32
 
@@ -116,7 +118,7 @@ size_t ZSTD_compressBlock_lazy2_extDict_row(
116
118
  size_t ZSTD_compressBlock_btlazy2_extDict(
117
119
  ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
118
120
  void const* src, size_t srcSize);
119
-
121
+
120
122
 
121
123
  #if defined (__cplusplus)
122
124
  }
@@ -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
@@ -242,11 +242,11 @@ static size_t ZSTD_ldm_fillFastTables(ZSTD_matchState_t* ms,
242
242
  switch(ms->cParams.strategy)
243
243
  {
244
244
  case ZSTD_fast:
245
- ZSTD_fillHashTable(ms, iend, ZSTD_dtlm_fast);
245
+ ZSTD_fillHashTable(ms, iend, ZSTD_dtlm_fast, ZSTD_tfp_forCCtx);
246
246
  break;
247
247
 
248
248
  case ZSTD_dfast:
249
- ZSTD_fillDoubleHashTable(ms, iend, ZSTD_dtlm_fast);
249
+ ZSTD_fillDoubleHashTable(ms, iend, ZSTD_dtlm_fast, ZSTD_tfp_forCCtx);
250
250
  break;
251
251
 
252
252
  case ZSTD_greedy:
@@ -549,7 +549,7 @@ size_t ZSTD_ldm_generateSequences(
549
549
  * the window through early invalidation.
550
550
  * TODO: * Test the chunk size.
551
551
  * * Try invalidation after the sequence generation and test the
552
- * the offset against maxDist directly.
552
+ * offset against maxDist directly.
553
553
  *
554
554
  * NOTE: Because of dictionaries + sequence splitting we MUST make sure
555
555
  * that any offset used is valid at the END of the sequence, since it may
@@ -711,7 +711,7 @@ size_t ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore,
711
711
  rep[0] = sequence.offset;
712
712
  /* Store the sequence */
713
713
  ZSTD_storeSeq(seqStore, newLitLength, ip - newLitLength, iend,
714
- STORE_OFFSET(sequence.offset),
714
+ OFFSET_TO_OFFBASE(sequence.offset),
715
715
  sequence.matchLength);
716
716
  ip += sequence.matchLength;
717
717
  }
@@ -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
@@ -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
@@ -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] = {
@@ -224,10 +243,9 @@ ZSTD_rescaleFreqs(optState_t* const optPtr,
224
243
  optPtr->offCodeSum = sum_u32(baseOFCfreqs, MaxOff+1);
225
244
  }
226
245
 
227
-
228
246
  }
229
247
 
230
- } else { /* new block : re-use previous statistics, scaled down */
248
+ } else { /* new block : scale down accumulated statistics */
231
249
 
232
250
  if (compressedLiterals)
233
251
  optPtr->litSum = ZSTD_scaleStats(optPtr->litFreq, MaxLit, 12);
@@ -255,11 +273,14 @@ static U32 ZSTD_rawLiteralsCost(const BYTE* const literals, U32 const litLength,
255
273
  return (litLength*6) * BITCOST_MULTIPLIER; /* 6 bit per literal - no statistic used */
256
274
 
257
275
  /* dynamic statistics */
258
- { U32 price = litLength * optPtr->litSumBasePrice;
276
+ { U32 price = optPtr->litSumBasePrice * litLength;
277
+ U32 const litPriceMax = optPtr->litSumBasePrice - BITCOST_MULTIPLIER;
259
278
  U32 u;
279
+ assert(optPtr->litSumBasePrice >= BITCOST_MULTIPLIER);
260
280
  for (u=0; u < litLength; u++) {
261
- assert(WEIGHT(optPtr->litFreq[literals[u]], optLevel) <= optPtr->litSumBasePrice); /* literal cost should never be negative */
262
- 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;
263
284
  }
264
285
  return price;
265
286
  }
@@ -272,10 +293,11 @@ static U32 ZSTD_litLengthPrice(U32 const litLength, const optState_t* const optP
272
293
  assert(litLength <= ZSTD_BLOCKSIZE_MAX);
273
294
  if (optPtr->priceType == zop_predef)
274
295
  return WEIGHT(litLength, optLevel);
275
- /* We can't compute the litLength price for sizes >= ZSTD_BLOCKSIZE_MAX
276
- * because it isn't representable in the zstd format. So instead just
277
- * call it 1 bit more than ZSTD_BLOCKSIZE_MAX - 1. In this case the block
278
- * would be all literals.
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.
279
301
  */
280
302
  if (litLength == ZSTD_BLOCKSIZE_MAX)
281
303
  return BITCOST_MULTIPLIER + ZSTD_litLengthPrice(ZSTD_BLOCKSIZE_MAX - 1, optPtr, optLevel);
@@ -289,24 +311,25 @@ static U32 ZSTD_litLengthPrice(U32 const litLength, const optState_t* const optP
289
311
  }
290
312
 
291
313
  /* ZSTD_getMatchPrice() :
292
- * 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.
293
315
  * Must be combined with ZSTD_fullLiteralsCost() to get the full cost of a sequence.
294
- * @offcode : expects a scale where 0,1,2 are repcodes 1-3, and 3+ are real_offsets+2
316
+ * @offBase : sumtype, representing an offset or a repcode, and using numeric representation of ZSTD_storeSeq()
295
317
  * @optLevel: when <2, favors small offset for decompression speed (improved cache efficiency)
296
318
  */
297
319
  FORCE_INLINE_TEMPLATE U32
298
- ZSTD_getMatchPrice(U32 const offcode,
320
+ ZSTD_getMatchPrice(U32 const offBase,
299
321
  U32 const matchLength,
300
322
  const optState_t* const optPtr,
301
323
  int const optLevel)
302
324
  {
303
325
  U32 price;
304
- U32 const offCode = ZSTD_highbit32(STORED_TO_OFFBASE(offcode));
326
+ U32 const offCode = ZSTD_highbit32(offBase);
305
327
  U32 const mlBase = matchLength - MINMATCH;
306
328
  assert(matchLength >= MINMATCH);
307
329
 
308
- if (optPtr->priceType == zop_predef) /* fixed scheme, do not use statistics */
309
- 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 */
310
333
 
311
334
  /* dynamic statistics */
312
335
  price = (offCode * BITCOST_MULTIPLIER) + (optPtr->offCodeSumBasePrice - WEIGHT(optPtr->offCodeFreq[offCode], optLevel));
@@ -325,10 +348,10 @@ ZSTD_getMatchPrice(U32 const offcode,
325
348
  }
326
349
 
327
350
  /* ZSTD_updateStats() :
328
- * assumption : literals + litLengtn <= iend */
351
+ * assumption : literals + litLength <= iend */
329
352
  static void ZSTD_updateStats(optState_t* const optPtr,
330
353
  U32 litLength, const BYTE* literals,
331
- U32 offsetCode, U32 matchLength)
354
+ U32 offBase, U32 matchLength)
332
355
  {
333
356
  /* literals */
334
357
  if (ZSTD_compressedLiterals(optPtr)) {
@@ -344,8 +367,8 @@ static void ZSTD_updateStats(optState_t* const optPtr,
344
367
  optPtr->litLengthSum++;
345
368
  }
346
369
 
347
- /* offset code : expected to follow storeSeq() numeric representation */
348
- { U32 const offCode = ZSTD_highbit32(STORED_TO_OFFBASE(offsetCode));
370
+ /* offset code : follows storeSeq() numeric representation */
371
+ { U32 const offCode = ZSTD_highbit32(offBase);
349
372
  assert(offCode <= MaxOff);
350
373
  optPtr->offCodeFreq[offCode]++;
351
374
  optPtr->offCodeSum++;
@@ -552,16 +575,17 @@ void ZSTD_updateTree(ZSTD_matchState_t* ms, const BYTE* ip, const BYTE* iend) {
552
575
  ZSTD_updateTree_internal(ms, ip, iend, ms->cParams.minMatch, ZSTD_noDict);
553
576
  }
554
577
 
555
- FORCE_INLINE_TEMPLATE
556
- U32 ZSTD_insertBtAndGetAllMatches (
557
- ZSTD_match_t* matches, /* store result (found matches) in this table (presumed large enough) */
558
- ZSTD_matchState_t* ms,
559
- U32* nextToUpdate3,
560
- const BYTE* const ip, const BYTE* const iLimit, const ZSTD_dictMode_e dictMode,
561
- const U32 rep[ZSTD_REP_NUM],
562
- U32 const ll0, /* tells if associated literal length is 0 or not. This value must be 0 or 1 */
563
- const U32 lengthToBeat,
564
- 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 */)
565
589
  {
566
590
  const ZSTD_compressionParameters* const cParams = &ms->cParams;
567
591
  U32 const sufficient_len = MIN(cParams->targetLength, ZSTD_OPT_NUM -1);
@@ -644,7 +668,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
644
668
  DEBUGLOG(8, "found repCode %u (ll0:%u, offset:%u) of length %u",
645
669
  repCode, ll0, repOffset, repLen);
646
670
  bestLength = repLen;
647
- matches[mnum].off = STORE_REPCODE(repCode - ll0 + 1); /* expect value between 1 and 3 */
671
+ matches[mnum].off = REPCODE_TO_OFFBASE(repCode - ll0 + 1); /* expect value between 1 and 3 */
648
672
  matches[mnum].len = (U32)repLen;
649
673
  mnum++;
650
674
  if ( (repLen > sufficient_len)
@@ -673,7 +697,7 @@ U32 ZSTD_insertBtAndGetAllMatches (
673
697
  bestLength = mlen;
674
698
  assert(curr > matchIndex3);
675
699
  assert(mnum==0); /* no prior solution */
676
- matches[0].off = STORE_OFFSET(curr - matchIndex3);
700
+ matches[0].off = OFFSET_TO_OFFBASE(curr - matchIndex3);
677
701
  matches[0].len = (U32)mlen;
678
702
  mnum = 1;
679
703
  if ( (mlen > sufficient_len) |
@@ -706,13 +730,13 @@ U32 ZSTD_insertBtAndGetAllMatches (
706
730
  }
707
731
 
708
732
  if (matchLength > bestLength) {
709
- DEBUGLOG(8, "found match of length %u at distance %u (offCode=%u)",
710
- (U32)matchLength, curr - matchIndex, STORE_OFFSET(curr - matchIndex));
733
+ DEBUGLOG(8, "found match of length %u at distance %u (offBase=%u)",
734
+ (U32)matchLength, curr - matchIndex, OFFSET_TO_OFFBASE(curr - matchIndex));
711
735
  assert(matchEndIdx > matchIndex);
712
736
  if (matchLength > matchEndIdx - matchIndex)
713
737
  matchEndIdx = matchIndex + (U32)matchLength;
714
738
  bestLength = matchLength;
715
- matches[mnum].off = STORE_OFFSET(curr - matchIndex);
739
+ matches[mnum].off = OFFSET_TO_OFFBASE(curr - matchIndex);
716
740
  matches[mnum].len = (U32)matchLength;
717
741
  mnum++;
718
742
  if ( (matchLength > ZSTD_OPT_NUM)
@@ -754,12 +778,12 @@ U32 ZSTD_insertBtAndGetAllMatches (
754
778
 
755
779
  if (matchLength > bestLength) {
756
780
  matchIndex = dictMatchIndex + dmsIndexDelta;
757
- DEBUGLOG(8, "found dms match of length %u at distance %u (offCode=%u)",
758
- (U32)matchLength, curr - matchIndex, STORE_OFFSET(curr - matchIndex));
781
+ DEBUGLOG(8, "found dms match of length %u at distance %u (offBase=%u)",
782
+ (U32)matchLength, curr - matchIndex, OFFSET_TO_OFFBASE(curr - matchIndex));
759
783
  if (matchLength > matchEndIdx - matchIndex)
760
784
  matchEndIdx = matchIndex + (U32)matchLength;
761
785
  bestLength = matchLength;
762
- matches[mnum].off = STORE_OFFSET(curr - matchIndex);
786
+ matches[mnum].off = OFFSET_TO_OFFBASE(curr - matchIndex);
763
787
  matches[mnum].len = (U32)matchLength;
764
788
  mnum++;
765
789
  if ( (matchLength > ZSTD_OPT_NUM)
@@ -960,7 +984,7 @@ static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, U32* nbMatches,
960
984
  const ZSTD_optLdm_t* optLdm, U32 currPosInBlock)
961
985
  {
962
986
  U32 const posDiff = currPosInBlock - optLdm->startPosInBlock;
963
- /* Note: ZSTD_match_t actually contains offCode and matchLength (before subtracting MINMATCH) */
987
+ /* Note: ZSTD_match_t actually contains offBase and matchLength (before subtracting MINMATCH) */
964
988
  U32 const candidateMatchLength = optLdm->endPosInBlock - optLdm->startPosInBlock - posDiff;
965
989
 
966
990
  /* Ensure that current block position is not outside of the match */
@@ -971,11 +995,11 @@ static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, U32* nbMatches,
971
995
  }
972
996
 
973
997
  if (*nbMatches == 0 || ((candidateMatchLength > matches[*nbMatches-1].len) && *nbMatches < ZSTD_OPT_NUM)) {
974
- U32 const candidateOffCode = STORE_OFFSET(optLdm->offset);
975
- DEBUGLOG(6, "ZSTD_optLdm_maybeAddMatch(): Adding ldm candidate match (offCode: %u matchLength %u) at block position=%u",
976
- 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);
977
1001
  matches[*nbMatches].len = candidateMatchLength;
978
- matches[*nbMatches].off = candidateOffCode;
1002
+ matches[*nbMatches].off = candidateOffBase;
979
1003
  (*nbMatches)++;
980
1004
  }
981
1005
  }
@@ -1062,6 +1086,8 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1062
1086
  ZSTD_optimal_t lastSequence;
1063
1087
  ZSTD_optLdm_t optLdm;
1064
1088
 
1089
+ ZSTD_memset(&lastSequence, 0, sizeof(ZSTD_optimal_t));
1090
+
1065
1091
  optLdm.seqStore = ms->ldmSeqStore ? *ms->ldmSeqStore : kNullRawSeqStore;
1066
1092
  optLdm.endPosInBlock = optLdm.startPosInBlock = optLdm.offset = 0;
1067
1093
  ZSTD_opt_getNextMatchAndUpdateSeqStore(&optLdm, (U32)(ip-istart), (U32)(iend-ip));
@@ -1098,14 +1124,14 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1098
1124
 
1099
1125
  /* large match -> immediate encoding */
1100
1126
  { U32 const maxML = matches[nbMatches-1].len;
1101
- U32 const maxOffcode = matches[nbMatches-1].off;
1102
- DEBUGLOG(6, "found %u matches of maxLength=%u and maxOffCode=%u at cPos=%u => start new series",
1103
- nbMatches, maxML, maxOffcode, (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));
1104
1130
 
1105
1131
  if (maxML > sufficient_len) {
1106
1132
  lastSequence.litlen = litlen;
1107
1133
  lastSequence.mlen = maxML;
1108
- lastSequence.off = maxOffcode;
1134
+ lastSequence.off = maxOffBase;
1109
1135
  DEBUGLOG(6, "large match (%u>%u), immediate encoding",
1110
1136
  maxML, sufficient_len);
1111
1137
  cur = 0;
@@ -1122,15 +1148,15 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1122
1148
  opt[pos].price = ZSTD_MAX_PRICE; /* mlen, litlen and price will be fixed during forward scanning */
1123
1149
  }
1124
1150
  for (matchNb = 0; matchNb < nbMatches; matchNb++) {
1125
- U32 const offcode = matches[matchNb].off;
1151
+ U32 const offBase = matches[matchNb].off;
1126
1152
  U32 const end = matches[matchNb].len;
1127
1153
  for ( ; pos <= end ; pos++ ) {
1128
- U32 const matchPrice = ZSTD_getMatchPrice(offcode, pos, optStatePtr, optLevel);
1154
+ U32 const matchPrice = ZSTD_getMatchPrice(offBase, pos, optStatePtr, optLevel);
1129
1155
  U32 const sequencePrice = literalsPrice + matchPrice;
1130
1156
  DEBUGLOG(7, "rPos:%u => set initial price : %.2f",
1131
- pos, ZSTD_fCost(sequencePrice));
1157
+ pos, ZSTD_fCost((int)sequencePrice));
1132
1158
  opt[pos].mlen = pos;
1133
- opt[pos].off = offcode;
1159
+ opt[pos].off = offBase;
1134
1160
  opt[pos].litlen = litlen;
1135
1161
  opt[pos].price = (int)sequencePrice;
1136
1162
  } }
@@ -1230,7 +1256,7 @@ ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms,
1230
1256
  U32 const startML = (matchNb>0) ? matches[matchNb-1].len+1 : minMatch;
1231
1257
  U32 mlen;
1232
1258
 
1233
- DEBUGLOG(7, "testing match %u => offCode=%4u, mlen=%2u, llen=%2u",
1259
+ DEBUGLOG(7, "testing match %u => offBase=%4u, mlen=%2u, llen=%2u",
1234
1260
  matchNb, matches[matchNb].off, lastML, litlen);
1235
1261
 
1236
1262
  for (mlen = lastML; mlen >= startML; mlen--) { /* scan downward */
@@ -1296,7 +1322,7 @@ _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */
1296
1322
  for (storePos=storeStart; storePos <= storeEnd; storePos++) {
1297
1323
  U32 const llen = opt[storePos].litlen;
1298
1324
  U32 const mlen = opt[storePos].mlen;
1299
- U32 const offCode = opt[storePos].off;
1325
+ U32 const offBase = opt[storePos].off;
1300
1326
  U32 const advance = llen + mlen;
1301
1327
  DEBUGLOG(6, "considering seq starting at %zi, llen=%u, mlen=%u",
1302
1328
  anchor - istart, (unsigned)llen, (unsigned)mlen);
@@ -1308,8 +1334,8 @@ _shortestPath: /* cur, last_pos, best_mlen, best_off have to be set */
1308
1334
  }
1309
1335
 
1310
1336
  assert(anchor + llen <= iend);
1311
- ZSTD_updateStats(optStatePtr, llen, anchor, offCode, mlen);
1312
- ZSTD_storeSeq(seqStore, llen, anchor, iend, offCode, mlen);
1337
+ ZSTD_updateStats(optStatePtr, llen, anchor, offBase, mlen);
1338
+ ZSTD_storeSeq(seqStore, llen, anchor, iend, offBase, mlen);
1313
1339
  anchor += advance;
1314
1340
  ip = anchor;
1315
1341
  } }
@@ -1349,7 +1375,7 @@ size_t ZSTD_compressBlock_btopt(
1349
1375
  /* ZSTD_initStats_ultra():
1350
1376
  * make a first compression pass, just to seed stats with more accurate starting values.
1351
1377
  * only works on first block, with no dictionary and no ldm.
1352
- * this function cannot error, hence its contract must be respected.
1378
+ * this function cannot error out, its narrow contract must be respected.
1353
1379
  */
1354
1380
  static void
1355
1381
  ZSTD_initStats_ultra(ZSTD_matchState_t* ms,
@@ -1368,7 +1394,7 @@ ZSTD_initStats_ultra(ZSTD_matchState_t* ms,
1368
1394
 
1369
1395
  ZSTD_compressBlock_opt2(ms, seqStore, tmpRep, src, srcSize, ZSTD_noDict); /* generate stats into ms->opt*/
1370
1396
 
1371
- /* invalidate first scan from history */
1397
+ /* invalidate first scan from history, only keep entropy stats */
1372
1398
  ZSTD_resetSeqStore(seqStore);
1373
1399
  ms->window.base -= srcSize;
1374
1400
  ms->window.dictLimit += (U32)srcSize;
@@ -1392,20 +1418,20 @@ size_t ZSTD_compressBlock_btultra2(
1392
1418
  U32 const curr = (U32)((const BYTE*)src - ms->window.base);
1393
1419
  DEBUGLOG(5, "ZSTD_compressBlock_btultra2 (srcSize=%zu)", srcSize);
1394
1420
 
1395
- /* 2-pass strategy:
1421
+ /* 2-passes strategy:
1396
1422
  * this strategy makes a first pass over first block to collect statistics
1397
- * and seed next round's statistics with it.
1398
- * 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.
1399
1425
  * Consequently, this can only work if no data has been previously loaded in tables,
1400
1426
  * aka, no dictionary, no prefix, no ldm preprocessing.
1401
1427
  * The compression ratio gain is generally small (~0.5% on first block),
1402
- * the cost is 2x cpu time on first block. */
1428
+ ** the cost is 2x cpu time on first block. */
1403
1429
  assert(srcSize <= ZSTD_BLOCKSIZE_MAX);
1404
1430
  if ( (ms->opt.litLengthSum==0) /* first block */
1405
1431
  && (seqStore->sequences == seqStore->sequencesStart) /* no ldm */
1406
1432
  && (ms->window.dictLimit == ms->window.lowLimit) /* no dictionary */
1407
- && (curr == ms->window.dictLimit) /* start of frame, nothing already loaded nor skipped */
1408
- && (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 */
1409
1435
  ) {
1410
1436
  ZSTD_initStats_ultra(ms, seqStore, rep, src, srcSize);
1411
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
@@ -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
@@ -20,6 +20,7 @@
20
20
 
21
21
 
22
22
  /* ====== Dependencies ====== */
23
+ #include "../common/allocations.h" /* ZSTD_customMalloc, ZSTD_customCalloc, ZSTD_customFree */
23
24
  #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset, INT_MAX, UINT_MAX */
24
25
  #include "../common/mem.h" /* MEM_STATIC */
25
26
  #include "../common/pool.h" /* threadpool */
@@ -266,11 +267,11 @@ static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* bufPool, buffer_t buf)
266
267
  * 1 buffer for input loading
267
268
  * 1 buffer for "next input" when submitting current one
268
269
  * 1 buffer stuck in queue */
269
- #define BUF_POOL_MAX_NB_BUFFERS(nbWorkers) 2*nbWorkers + 3
270
+ #define BUF_POOL_MAX_NB_BUFFERS(nbWorkers) (2*(nbWorkers) + 3)
270
271
 
271
272
  /* After a worker releases its rawSeqStore, it is immediately ready for reuse.
272
273
  * So we only need one seq buffer per worker. */
273
- #define SEQ_POOL_MAX_NB_BUFFERS(nbWorkers) nbWorkers
274
+ #define SEQ_POOL_MAX_NB_BUFFERS(nbWorkers) (nbWorkers)
274
275
 
275
276
  /* ===== Seq Pool Wrapper ====== */
276
277
 
@@ -719,7 +720,7 @@ static void ZSTDMT_compressionJob(void* jobDescription)
719
720
  ZSTDMT_serialState_update(job->serial, cctx, rawSeqStore, job->src, job->jobID);
720
721
 
721
722
  if (!job->firstJob) { /* flush and overwrite frame header when it's not first job */
722
- size_t const hSize = ZSTD_compressContinue(cctx, dstBuff.start, dstBuff.capacity, job->src.start, 0);
723
+ size_t const hSize = ZSTD_compressContinue_public(cctx, dstBuff.start, dstBuff.capacity, job->src.start, 0);
723
724
  if (ZSTD_isError(hSize)) JOB_ERROR(hSize);
724
725
  DEBUGLOG(5, "ZSTDMT_compressionJob: flush and overwrite %u bytes of frame header (not first job)", (U32)hSize);
725
726
  ZSTD_invalidateRepCodes(cctx);
@@ -737,7 +738,7 @@ static void ZSTDMT_compressionJob(void* jobDescription)
737
738
  DEBUGLOG(5, "ZSTDMT_compressionJob: compress %u bytes in %i blocks", (U32)job->src.size, nbChunks);
738
739
  assert(job->cSize == 0);
739
740
  for (chunkNb = 1; chunkNb < nbChunks; chunkNb++) {
740
- size_t const cSize = ZSTD_compressContinue(cctx, op, oend-op, ip, chunkSize);
741
+ size_t const cSize = ZSTD_compressContinue_public(cctx, op, oend-op, ip, chunkSize);
741
742
  if (ZSTD_isError(cSize)) JOB_ERROR(cSize);
742
743
  ip += chunkSize;
743
744
  op += cSize; assert(op < oend);
@@ -757,8 +758,8 @@ static void ZSTDMT_compressionJob(void* jobDescription)
757
758
  size_t const lastBlockSize1 = job->src.size & (chunkSize-1);
758
759
  size_t const lastBlockSize = ((lastBlockSize1==0) & (job->src.size>=chunkSize)) ? chunkSize : lastBlockSize1;
759
760
  size_t const cSize = (job->lastJob) ?
760
- ZSTD_compressEnd (cctx, op, oend-op, ip, lastBlockSize) :
761
- ZSTD_compressContinue(cctx, op, oend-op, ip, lastBlockSize);
761
+ ZSTD_compressEnd_public(cctx, op, oend-op, ip, lastBlockSize) :
762
+ ZSTD_compressContinue_public(cctx, op, oend-op, ip, lastBlockSize);
762
763
  if (ZSTD_isError(cSize)) JOB_ERROR(cSize);
763
764
  lastCBlockSize = cSize;
764
765
  } }
@@ -1734,7 +1735,7 @@ findSynchronizationPoint(ZSTDMT_CCtx const* mtctx, ZSTD_inBuffer const input)
1734
1735
  }
1735
1736
  } else {
1736
1737
  /* We have enough bytes buffered to initialize the hash,
1737
- * and are have processed enough bytes to find a sync point.
1738
+ * and have processed enough bytes to find a sync point.
1738
1739
  * Start scanning at the beginning of the input.
1739
1740
  */
1740
1741
  assert(mtctx->inBuff.filled >= RSYNC_MIN_BLOCK_SIZE);
@@ -1761,17 +1762,24 @@ findSynchronizationPoint(ZSTDMT_CCtx const* mtctx, ZSTD_inBuffer const input)
1761
1762
  * then a block will be emitted anyways, but this is okay, since if we
1762
1763
  * are already synchronized we will remain synchronized.
1763
1764
  */
1765
+ assert(pos < RSYNC_LENGTH || ZSTD_rollingHash_compute(istart + pos - RSYNC_LENGTH, RSYNC_LENGTH) == hash);
1764
1766
  for (; pos < syncPoint.toLoad; ++pos) {
1765
1767
  BYTE const toRemove = pos < RSYNC_LENGTH ? prev[pos] : istart[pos - RSYNC_LENGTH];
1766
- assert(pos < RSYNC_LENGTH || ZSTD_rollingHash_compute(istart + pos - RSYNC_LENGTH, RSYNC_LENGTH) == hash);
1768
+ /* This assert is very expensive, and Debian compiles with asserts enabled.
1769
+ * So disable it for now. We can get similar coverage by checking it at the
1770
+ * beginning & end of the loop.
1771
+ * assert(pos < RSYNC_LENGTH || ZSTD_rollingHash_compute(istart + pos - RSYNC_LENGTH, RSYNC_LENGTH) == hash);
1772
+ */
1767
1773
  hash = ZSTD_rollingHash_rotate(hash, toRemove, istart[pos], primePower);
1768
1774
  assert(mtctx->inBuff.filled + pos >= RSYNC_MIN_BLOCK_SIZE);
1769
1775
  if ((hash & hitMask) == hitMask) {
1770
1776
  syncPoint.toLoad = pos + 1;
1771
1777
  syncPoint.flush = 1;
1778
+ ++pos; /* for assert */
1772
1779
  break;
1773
1780
  }
1774
1781
  }
1782
+ assert(pos < RSYNC_LENGTH || ZSTD_rollingHash_compute(istart + pos - RSYNC_LENGTH, RSYNC_LENGTH) == hash);
1775
1783
  return syncPoint;
1776
1784
  }
1777
1785