multi_compress 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -0
  3. data/GET_STARTED.md +4 -2
  4. data/README.md +1 -1
  5. data/THIRD_PARTY_NOTICES.md +2 -2
  6. data/ext/multi_compress/exports.darwin +1 -0
  7. data/ext/multi_compress/exports.map +4 -0
  8. data/ext/multi_compress/extconf.rb +20 -0
  9. data/ext/multi_compress/multi_compress.c +123 -73
  10. data/ext/multi_compress/vendor/.vendored +1 -1
  11. data/ext/multi_compress/vendor/zstd/lib/BUCK +10 -5
  12. data/ext/multi_compress/vendor/zstd/lib/README.md +25 -5
  13. data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +6 -2
  14. data/ext/multi_compress/vendor/zstd/lib/common/bits.h +1 -1
  15. data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +3 -3
  16. data/ext/multi_compress/vendor/zstd/lib/common/bmi2.h +40 -0
  17. data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +37 -11
  18. data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +7 -6
  19. data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +4 -3
  20. data/ext/multi_compress/vendor/zstd/lib/common/pool.c +16 -19
  21. data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +31 -0
  22. data/ext/multi_compress/vendor/zstd/lib/common/threading.c +16 -2
  23. data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +9 -0
  24. data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +4 -2
  25. data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +300 -3
  26. data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +4 -0
  27. data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +4 -1
  28. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +775 -214
  29. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +35 -6
  30. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +9 -5
  31. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +14 -5
  32. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +2 -2
  33. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +2 -2
  34. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +49 -5
  35. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +11 -11
  36. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +11 -0
  37. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +16 -18
  38. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.c +3 -3
  39. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +64 -39
  40. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +30 -2
  41. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +78 -64
  42. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +182 -18
  43. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +59 -27
  44. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +11 -0
  45. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +194 -93
  46. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +225 -85
  47. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +19 -2
  48. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +136 -91
  49. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.c +77 -77
  50. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +48 -42
  51. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +18 -14
  52. data/ext/multi_compress/vendor/zstd/lib/dll/example/build_package.bat +66 -20
  53. data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.vcxproj +10 -6
  54. data/ext/multi_compress/vendor/zstd/lib/install_oses.mk +17 -0
  55. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +10 -8
  56. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +5 -364
  57. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +4 -5
  58. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +7 -3
  59. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +8 -5
  60. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +3 -3
  61. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +1 -0
  62. data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +8 -7
  63. data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +1 -0
  64. data/ext/multi_compress/vendor/zstd/lib/module.modulemap +0 -21
  65. data/ext/multi_compress/vendor/zstd/lib/zstd.h +209 -112
  66. data/lib/multi_compress/reader/read_operations.rb +2 -0
  67. data/lib/multi_compress/version.rb +1 -1
  68. data/multi_compress.gemspec +2 -0
  69. data/mysql_udf/Makefile +12 -5
  70. data/mysql_udf/multi_compress_mysql.exports +6 -0
  71. data/postgres_extension/Makefile +2 -1
  72. data/postgres_extension/test/run_e2e.sh +28 -10
  73. metadata +6 -5
  74. data/ext/multi_compress/vendor/lz4/lib/Makefile +0 -244
  75. data/ext/multi_compress/vendor/lz4/lib/dll/example/Makefile +0 -63
  76. data/ext/multi_compress/vendor/zstd/lib/Makefile +0 -389
  77. data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +0 -48
@@ -155,12 +155,15 @@ struct ZSTD_DCtx_s
155
155
  size_t staticSize;
156
156
  int isFrameDecompression;
157
157
  #if DYNAMIC_BMI2
158
- int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise. CPU support is determined dynamically once per context lifetime. */
158
+ int bmi2; /* == 1 if the CPU supports BMI2 and 0 otherwise when CPU state
159
+ * is cached per context. Never read this directly: use ZSTD_DCtx_get_bmi2(),
160
+ * which also handles builds
161
+ * where cached CPU state is not used. */
159
162
  #endif
160
163
 
161
164
  /* dictionary */
162
165
  ZSTD_DDict* ddictLocal;
163
- const ZSTD_DDict* ddict; /* set by ZSTD_initDStream_usingDDict(), or ZSTD_DCtx_refDDict() */
166
+ const ZSTD_DDict* ddict; /* single-DDict state; NULL while ddictSet exists */
164
167
  U32 dictID;
165
168
  int ddictIsCold; /* if == 1 : dictionary is "new" for working context, and presumed "cold" (not in cpu cache) */
166
169
  ZSTD_dictUses_e dictUses;
@@ -207,9 +210,21 @@ struct ZSTD_DCtx_s
207
210
  /* Tracing */
208
211
  #if ZSTD_TRACE
209
212
  ZSTD_TraceCtx traceCtx;
213
+ size_t traceDictSize; /* size of the dictionary installed for this frame */
210
214
  #endif
211
215
  }; /* typedef'd to ZSTD_DCtx within "zstd.h" */
212
216
 
217
+ /**
218
+ * @returns the cached per-context BMI2 dispatch hint, or 0 when the build does
219
+ * not use cached CPU state.
220
+ *
221
+ * Beware: this is *not* a "does the CPU support BMI2" test. When BMI2 is enabled
222
+ * at compile time, DYNAMIC_BMI2 is 0: the whole library is already compiled with
223
+ * BMI2, no separate variant exists, and this returns 0 even though the CPU does
224
+ * support BMI2. Only use the result as a dispatch hint passed to a lower-level
225
+ * BMI2/default selector. Integrations may ignore the cached hint and consult
226
+ * their own CPU-feature policy at that selector.
227
+ */
213
228
  MEM_STATIC int ZSTD_DCtx_get_bmi2(const struct ZSTD_DCtx_s *dctx) {
214
229
  #if DYNAMIC_BMI2
215
230
  return dctx->bmi2;
@@ -233,6 +248,8 @@ size_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
233
248
  * check if next `dst` follows previous position, where decompression ended.
234
249
  * If yes, do nothing (continue on current segment).
235
250
  * If not, classify previous segment as "external dictionary", and start a new segment.
251
+ * A zero dstSize leaves history unchanged. Callers must only advance
252
+ * previousDstEnd when bytes are actually produced or inserted.
236
253
  * This function cannot fail. */
237
254
  void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst, size_t dstSize);
238
255
 
@@ -21,14 +21,18 @@
21
21
  /*-*************************************
22
22
  * Dependencies
23
23
  ***************************************/
24
- /* qsort_r is an extension. */
25
- #if defined(__linux) || defined(__linux__) || defined(linux) || defined(__gnu_linux__) || \
26
- defined(__CYGWIN__) || defined(__MSYS__)
27
- #if !defined(_GNU_SOURCE) && !defined(__ANDROID__) /* NDK doesn't ship qsort_r(). */
28
- #define _GNU_SOURCE
29
- #endif
24
+ /* qsort_r is an extension.
25
+ *
26
+ * Android NDK does not ship qsort_r().
27
+ */
28
+ #if (defined(__linux__) && !defined(__ANDROID__)) || defined(__CYGWIN__) || defined(__MSYS__)
29
+ # ifndef _GNU_SOURCE
30
+ # define _GNU_SOURCE
31
+ # endif
30
32
  #endif
31
33
 
34
+ #define __STDC_WANT_LIB_EXT1__ 1 /* request C11 Annex K, which includes qsort_s() */
35
+
32
36
  #include <stdio.h> /* fprintf */
33
37
  #include <stdlib.h> /* malloc, free, qsort_r */
34
38
 
@@ -39,6 +43,7 @@
39
43
  # define ZDICT_STATIC_LINKING_ONLY
40
44
  #endif
41
45
 
46
+ #include "../common/debug.h" /* DEBUG_STATIC_ASSERT */
42
47
  #include "../common/mem.h" /* read */
43
48
  #include "../common/pool.h" /* POOL_ctx */
44
49
  #include "../common/threading.h" /* ZSTD_pthread_mutex_t */
@@ -60,40 +65,58 @@
60
65
  #define COVER_MAX_SAMPLES_SIZE (sizeof(size_t) == 8 ? ((unsigned)-1) : ((unsigned)1 GB))
61
66
  #define COVER_DEFAULT_SPLITPOINT 1.0
62
67
 
68
+ /**
69
+ * Select the qsort() variant used by cover
70
+ */
71
+ #define ZDICT_QSORT_MIN 0
72
+ #define ZDICT_QSORT_C90 ZDICT_QSORT_MIN
73
+ #define ZDICT_QSORT_GNU 1
74
+ #define ZDICT_QSORT_APPLE 2
75
+ #define ZDICT_QSORT_MSVC 3
76
+ #define ZDICT_QSORT_C11 ZDICT_QSORT_MAX
77
+ #define ZDICT_QSORT_MAX 4
78
+
79
+ #ifndef ZDICT_QSORT
80
+ # if defined(__APPLE__)
81
+ # define ZDICT_QSORT ZDICT_QSORT_APPLE /* uses qsort_r() with a different order for parameters */
82
+ # elif (defined(__linux__) && !defined(__ANDROID__)) || defined(__CYGWIN__) || defined(__MSYS__)
83
+ # define ZDICT_QSORT ZDICT_QSORT_GNU /* uses qsort_r() */
84
+ # elif defined(_WIN32) && defined(_MSC_VER)
85
+ # define ZDICT_QSORT ZDICT_QSORT_MSVC /* uses qsort_s() with a different order for parameters */
86
+ # elif defined(STDC_LIB_EXT1) && (STDC_LIB_EXT1 > 0) /* C11 Annex K */
87
+ # define ZDICT_QSORT ZDICT_QSORT_C11 /* uses qsort_s() */
88
+ # else
89
+ # define ZDICT_QSORT ZDICT_QSORT_C90 /* uses standard qsort() which is not re-entrant (requires global variable) */
90
+ # endif
91
+ #endif
92
+
93
+
63
94
  /*-*************************************
64
95
  * Console display
96
+ *
97
+ * Captures the `displayLevel` variable in the local scope.
65
98
  ***************************************/
66
- #ifndef LOCALDISPLAYLEVEL
67
- static int g_displayLevel = 0;
68
- #endif
69
99
  #undef DISPLAY
70
100
  #define DISPLAY(...) \
71
101
  { \
72
102
  fprintf(stderr, __VA_ARGS__); \
73
103
  fflush(stderr); \
74
104
  }
75
- #undef LOCALDISPLAYLEVEL
76
- #define LOCALDISPLAYLEVEL(displayLevel, l, ...) \
105
+ #undef DISPLAYLEVEL
106
+ #define DISPLAYLEVEL(l, ...) \
77
107
  if (displayLevel >= l) { \
78
108
  DISPLAY(__VA_ARGS__); \
79
109
  } /* 0 : no display; 1: errors; 2: default; 3: details; 4: debug */
80
- #undef DISPLAYLEVEL
81
- #define DISPLAYLEVEL(l, ...) LOCALDISPLAYLEVEL(g_displayLevel, l, __VA_ARGS__)
82
110
 
83
- #ifndef LOCALDISPLAYUPDATE
84
- static const clock_t g_refreshRate = CLOCKS_PER_SEC * 15 / 100;
85
- static clock_t g_time = 0;
86
- #endif
87
- #undef LOCALDISPLAYUPDATE
88
- #define LOCALDISPLAYUPDATE(displayLevel, l, ...) \
111
+ #undef DISPLAYUPDATE
112
+ #define DISPLAYUPDATE(lastUpdateTime, l, ...) \
89
113
  if (displayLevel >= l) { \
90
- if ((clock() - g_time > g_refreshRate) || (displayLevel >= 4)) { \
91
- g_time = clock(); \
114
+ const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100; \
115
+ if ((clock() - lastUpdateTime > refreshRate) || (displayLevel >= 4)) { \
116
+ lastUpdateTime = clock(); \
92
117
  DISPLAY(__VA_ARGS__); \
93
118
  } \
94
119
  }
95
- #undef DISPLAYUPDATE
96
- #define DISPLAYUPDATE(l, ...) LOCALDISPLAYUPDATE(g_displayLevel, l, __VA_ARGS__)
97
120
 
98
121
  /*-*************************************
99
122
  * Hash table
@@ -188,7 +211,7 @@ static U32 *COVER_map_at(COVER_map_t *map, U32 key) {
188
211
  */
189
212
  static void COVER_map_remove(COVER_map_t *map, U32 key) {
190
213
  U32 i = COVER_map_index(map, key);
191
- COVER_map_pair_t *del = &map->data[i];
214
+ COVER_map_pair_t* del = &map->data[i];
192
215
  U32 shift = 1;
193
216
  if (del->value == MAP_EMPTY_VALUE) {
194
217
  return;
@@ -239,10 +262,11 @@ typedef struct {
239
262
  U32 *freqs;
240
263
  U32 *dmerAt;
241
264
  unsigned d;
265
+ int displayLevel;
242
266
  } COVER_ctx_t;
243
267
 
244
- #if !defined(_GNU_SOURCE) && !defined(__APPLE__) && !defined(_MSC_VER)
245
- /* C90 only offers qsort() that needs a global context. */
268
+ #if ZDICT_QSORT == ZDICT_QSORT_C90
269
+ /* Use global context for non-reentrant sort functions */
246
270
  static COVER_ctx_t *g_coverCtx = NULL;
247
271
  #endif
248
272
 
@@ -286,34 +310,38 @@ static int COVER_cmp8(COVER_ctx_t *ctx, const void *lp, const void *rp) {
286
310
  }
287
311
 
288
312
  /**
289
- * Same as COVER_cmp() except ties are broken by pointer value
313
+ * Same as COVER_cmp() except ties are broken by the position each element holds
290
314
  */
291
- #if (defined(_WIN32) && defined(_MSC_VER)) || defined(__APPLE__)
315
+ #if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
292
316
  static int WIN_CDECL COVER_strict_cmp(void* g_coverCtx, const void* lp, const void* rp) {
293
- #elif defined(_GNU_SOURCE)
317
+ #elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
294
318
  static int COVER_strict_cmp(const void *lp, const void *rp, void *g_coverCtx) {
295
319
  #else /* C90 fallback.*/
296
320
  static int COVER_strict_cmp(const void *lp, const void *rp) {
297
321
  #endif
298
322
  int result = COVER_cmp((COVER_ctx_t*)g_coverCtx, lp, rp);
299
323
  if (result == 0) {
300
- result = lp < rp ? -1 : 1;
324
+ U32 const lpos = *(const U32 *)lp;
325
+ U32 const rpos = *(const U32 *)rp;
326
+ result = (lpos < rpos) ? -1 : (lpos > rpos) ? 1 : 0;
301
327
  }
302
328
  return result;
303
329
  }
304
330
  /**
305
331
  * Faster version for d <= 8.
306
332
  */
307
- #if (defined(_WIN32) && defined(_MSC_VER)) || defined(__APPLE__)
333
+ #if (ZDICT_QSORT == ZDICT_QSORT_MSVC) || (ZDICT_QSORT == ZDICT_QSORT_APPLE)
308
334
  static int WIN_CDECL COVER_strict_cmp8(void* g_coverCtx, const void* lp, const void* rp) {
309
- #elif defined(_GNU_SOURCE)
335
+ #elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
310
336
  static int COVER_strict_cmp8(const void *lp, const void *rp, void *g_coverCtx) {
311
337
  #else /* C90 fallback.*/
312
338
  static int COVER_strict_cmp8(const void *lp, const void *rp) {
313
339
  #endif
314
340
  int result = COVER_cmp8((COVER_ctx_t*)g_coverCtx, lp, rp);
315
341
  if (result == 0) {
316
- result = lp < rp ? -1 : 1;
342
+ U32 const lpos = *(const U32 *)lp;
343
+ U32 const rpos = *(const U32 *)rp;
344
+ result = (lpos < rpos) ? -1 : (lpos > rpos) ? 1 : 0;
317
345
  }
318
346
  return result;
319
347
  }
@@ -323,26 +351,28 @@ static int COVER_strict_cmp8(const void *lp, const void *rp) {
323
351
  * Hopefully when C11 become the norm, we will be able
324
352
  * to clean it up.
325
353
  */
326
- static void stableSort(COVER_ctx_t *ctx) {
327
- #if defined(__APPLE__)
354
+ static void stableSort(COVER_ctx_t *ctx)
355
+ {
356
+ DEBUG_STATIC_ASSERT(ZDICT_QSORT_MIN <= ZDICT_QSORT && ZDICT_QSORT <= ZDICT_QSORT_MAX);
357
+ #if (ZDICT_QSORT == ZDICT_QSORT_APPLE)
328
358
  qsort_r(ctx->suffix, ctx->suffixSize, sizeof(U32),
329
359
  ctx,
330
360
  (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
331
- #elif defined(_GNU_SOURCE)
361
+ #elif (ZDICT_QSORT == ZDICT_QSORT_GNU)
332
362
  qsort_r(ctx->suffix, ctx->suffixSize, sizeof(U32),
333
363
  (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
334
364
  ctx);
335
- #elif defined(_WIN32) && defined(_MSC_VER)
365
+ #elif (ZDICT_QSORT == ZDICT_QSORT_MSVC)
366
+ qsort_s(ctx->suffix, ctx->suffixSize, sizeof(U32),
367
+ (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
368
+ ctx);
369
+ #elif (ZDICT_QSORT == ZDICT_QSORT_C11)
336
370
  qsort_s(ctx->suffix, ctx->suffixSize, sizeof(U32),
337
371
  (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp),
338
372
  ctx);
339
- #elif defined(__OpenBSD__)
340
- g_coverCtx = ctx;
341
- mergesort(ctx->suffix, ctx->suffixSize, sizeof(U32),
342
- (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
343
373
  #else /* C90 fallback.*/
344
374
  g_coverCtx = ctx;
345
- /* TODO(cavalcanti): implement a reentrant qsort() when is not available. */
375
+ /* TODO(cavalcanti): implement a reentrant qsort() when _r is not available. */
346
376
  qsort(ctx->suffix, ctx->suffixSize, sizeof(U32),
347
377
  (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
348
378
  #endif
@@ -601,7 +631,7 @@ static void COVER_ctx_destroy(COVER_ctx_t *ctx) {
601
631
  */
602
632
  static size_t COVER_ctx_init(COVER_ctx_t *ctx, const void *samplesBuffer,
603
633
  const size_t *samplesSizes, unsigned nbSamples,
604
- unsigned d, double splitPoint)
634
+ unsigned d, double splitPoint, int displayLevel)
605
635
  {
606
636
  const BYTE *const samples = (const BYTE *)samplesBuffer;
607
637
  const size_t totalSamplesSize = COVER_sum(samplesSizes, nbSamples);
@@ -610,13 +640,25 @@ static size_t COVER_ctx_init(COVER_ctx_t *ctx, const void *samplesBuffer,
610
640
  const unsigned nbTestSamples = splitPoint < 1.0 ? nbSamples - nbTrainSamples : nbSamples;
611
641
  const size_t trainingSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes, nbTrainSamples) : totalSamplesSize;
612
642
  const size_t testSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes + nbTrainSamples, nbTestSamples) : totalSamplesSize;
643
+ const size_t readLength = MAX(d, sizeof(U64));
644
+ ctx->displayLevel = displayLevel;
613
645
  /* Checks */
614
- if (totalSamplesSize < MAX(d, sizeof(U64)) ||
646
+ if (totalSamplesSize < readLength ||
615
647
  totalSamplesSize >= (size_t)COVER_MAX_SAMPLES_SIZE) {
616
648
  DISPLAYLEVEL(1, "Total samples size is too large (%u MB), maximum size is %u MB\n",
617
649
  (unsigned)(totalSamplesSize>>20), (COVER_MAX_SAMPLES_SIZE >> 20));
618
650
  return ERROR(srcSize_wrong);
619
651
  }
652
+ /* The training set must be large enough for the partial suffix array, whose
653
+ * size is `trainingSamplesSize - MAX(d, sizeof(U64)) + 1`. When splitPoint
654
+ * < 1.0 the training set is a subset of the corpus, so it can be smaller
655
+ * than the total even after the check above. Guard against the size_t
656
+ * subtraction underflowing into a huge allocation. */
657
+ if (trainingSamplesSize < MAX(d, sizeof(U64))) {
658
+ DISPLAYLEVEL(1, "Training samples size is too small (%u), minimum size is %u\n",
659
+ (unsigned)trainingSamplesSize, (unsigned)MAX(d, sizeof(U64)));
660
+ return ERROR(srcSize_wrong);
661
+ }
620
662
  /* Check if there are at least 5 training samples */
621
663
  if (nbTrainSamples < 5) {
622
664
  DISPLAYLEVEL(1, "Total number of training samples is %u and is invalid.", nbTrainSamples);
@@ -639,7 +681,7 @@ static size_t COVER_ctx_init(COVER_ctx_t *ctx, const void *samplesBuffer,
639
681
  ctx->nbTrainSamples = nbTrainSamples;
640
682
  ctx->nbTestSamples = nbTestSamples;
641
683
  /* Partial suffix array */
642
- ctx->suffixSize = trainingSamplesSize - MAX(d, sizeof(U64)) + 1;
684
+ ctx->suffixSize = trainingSamplesSize - readLength + 1;
643
685
  ctx->suffix = (U32 *)malloc(ctx->suffixSize * sizeof(U32));
644
686
  /* Maps index to the dmerID */
645
687
  ctx->dmerAt = (U32 *)malloc(ctx->suffixSize * sizeof(U32));
@@ -665,7 +707,7 @@ static size_t COVER_ctx_init(COVER_ctx_t *ctx, const void *samplesBuffer,
665
707
  {
666
708
  /* suffix is a partial suffix array.
667
709
  * It only sorts suffixes by their first parameters.d bytes.
668
- * The sort is stable, so each dmer group is sorted by position in input.
710
+ * Ties are broken by position, so each group is sorted by input position.
669
711
  */
670
712
  U32 i;
671
713
  for (i = 0; i < ctx->suffixSize; ++i) {
@@ -694,14 +736,14 @@ void COVER_warnOnSmallCorpus(size_t maxDictSize, size_t nbDmers, int displayLeve
694
736
  if (ratio >= 10) {
695
737
  return;
696
738
  }
697
- LOCALDISPLAYLEVEL(displayLevel, 1,
698
- "WARNING: The maximum dictionary size %u is too large "
699
- "compared to the source size %u! "
700
- "size(source)/size(dictionary) = %f, but it should be >= "
701
- "10! This may lead to a subpar dictionary! We recommend "
702
- "training on sources at least 10x, and preferably 100x "
703
- "the size of the dictionary! \n", (U32)maxDictSize,
704
- (U32)nbDmers, ratio);
739
+ DISPLAYLEVEL(1,
740
+ "WARNING: The maximum dictionary size %u is too large "
741
+ "compared to the source size %u! "
742
+ "size(source)/size(dictionary) = %f, but it should be >= "
743
+ "10! This may lead to a subpar dictionary! We recommend "
744
+ "training on sources at least 10x, and preferably 100x "
745
+ "the size of the dictionary! \n", (U32)maxDictSize,
746
+ (U32)nbDmers, ratio);
705
747
  }
706
748
 
707
749
  COVER_epoch_info_t COVER_computeEpochs(U32 maxDictSize,
@@ -736,6 +778,8 @@ static size_t COVER_buildDictionary(const COVER_ctx_t *ctx, U32 *freqs,
736
778
  const size_t maxZeroScoreRun = MAX(10, MIN(100, epochs.num >> 3));
737
779
  size_t zeroScoreRun = 0;
738
780
  size_t epoch;
781
+ clock_t lastUpdateTime = 0;
782
+ const int displayLevel = ctx->displayLevel;
739
783
  DISPLAYLEVEL(2, "Breaking content into %u epochs of size %u\n",
740
784
  (U32)epochs.num, (U32)epochs.size);
741
785
  /* Loop through the epochs until there are no more segments or the dictionary
@@ -769,6 +813,7 @@ static size_t COVER_buildDictionary(const COVER_ctx_t *ctx, U32 *freqs,
769
813
  tail -= segmentSize;
770
814
  memcpy(dict + tail, ctx->samples + segment.begin, segmentSize);
771
815
  DISPLAYUPDATE(
816
+ lastUpdateTime,
772
817
  2, "\r%u%% ",
773
818
  (unsigned)(((dictBufferCapacity - tail) * 100) / dictBufferCapacity));
774
819
  }
@@ -784,9 +829,8 @@ ZDICTLIB_STATIC_API size_t ZDICT_trainFromBuffer_cover(
784
829
  BYTE* const dict = (BYTE*)dictBuffer;
785
830
  COVER_ctx_t ctx;
786
831
  COVER_map_t activeDmers;
832
+ const int displayLevel = (int)parameters.zParams.notificationLevel;
787
833
  parameters.splitPoint = 1.0;
788
- /* Initialize global data */
789
- g_displayLevel = (int)parameters.zParams.notificationLevel;
790
834
  /* Checks */
791
835
  if (!COVER_checkParameters(parameters, dictBufferCapacity)) {
792
836
  DISPLAYLEVEL(1, "Cover parameters incorrect\n");
@@ -804,12 +848,12 @@ ZDICTLIB_STATIC_API size_t ZDICT_trainFromBuffer_cover(
804
848
  /* Initialize context and activeDmers */
805
849
  {
806
850
  size_t const initVal = COVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples,
807
- parameters.d, parameters.splitPoint);
851
+ parameters.d, parameters.splitPoint, displayLevel);
808
852
  if (ZSTD_isError(initVal)) {
809
853
  return initVal;
810
854
  }
811
855
  }
812
- COVER_warnOnSmallCorpus(dictBufferCapacity, ctx.suffixSize, g_displayLevel);
856
+ COVER_warnOnSmallCorpus(dictBufferCapacity, ctx.suffixSize, displayLevel);
813
857
  if (!COVER_map_init(&activeDmers, parameters.k - parameters.d + 1)) {
814
858
  DISPLAYLEVEL(1, "Failed to allocate dmer map: out of memory\n");
815
859
  COVER_ctx_destroy(&ctx);
@@ -1023,6 +1067,8 @@ COVER_dictSelection_t COVER_selectDict(BYTE* customDictContent, size_t dictBuffe
1023
1067
 
1024
1068
  size_t largestDict = 0;
1025
1069
  size_t largestCompressed = 0;
1070
+ size_t candidateContentSize = 0;
1071
+ size_t candidateDict = 0;
1026
1072
  BYTE* customDictContentEnd = customDictContent + dictContentSize;
1027
1073
 
1028
1074
  BYTE* largestDictbuffer = (BYTE*)malloc(dictBufferCapacity);
@@ -1037,20 +1083,20 @@ COVER_dictSelection_t COVER_selectDict(BYTE* customDictContent, size_t dictBuffe
1037
1083
 
1038
1084
  /* Initial dictionary size and compressed size */
1039
1085
  memcpy(largestDictbuffer, customDictContent, dictContentSize);
1040
- dictContentSize = ZDICT_finalizeDictionary(
1086
+ largestDict = ZDICT_finalizeDictionary(
1041
1087
  largestDictbuffer, dictBufferCapacity, customDictContent, dictContentSize,
1042
1088
  samplesBuffer, samplesSizes, nbFinalizeSamples, params.zParams);
1043
1089
 
1044
- if (ZDICT_isError(dictContentSize)) {
1090
+ if (ZDICT_isError(largestDict)) {
1045
1091
  free(largestDictbuffer);
1046
1092
  free(candidateDictBuffer);
1047
- return COVER_dictSelectionError(dictContentSize);
1093
+ return COVER_dictSelectionError(largestDict);
1048
1094
  }
1049
1095
 
1050
1096
  totalCompressedSize = COVER_checkTotalCompressedSize(params, samplesSizes,
1051
1097
  samplesBuffer, offsets,
1052
1098
  nbCheckSamples, nbSamples,
1053
- largestDictbuffer, dictContentSize);
1099
+ largestDictbuffer, largestDict);
1054
1100
 
1055
1101
  if (ZSTD_isError(totalCompressedSize)) {
1056
1102
  free(largestDictbuffer);
@@ -1060,31 +1106,29 @@ COVER_dictSelection_t COVER_selectDict(BYTE* customDictContent, size_t dictBuffe
1060
1106
 
1061
1107
  if (params.shrinkDict == 0) {
1062
1108
  free(candidateDictBuffer);
1063
- return setDictSelection(largestDictbuffer, dictContentSize, totalCompressedSize);
1109
+ return setDictSelection(largestDictbuffer, largestDict, totalCompressedSize);
1064
1110
  }
1065
1111
 
1066
- largestDict = dictContentSize;
1067
1112
  largestCompressed = totalCompressedSize;
1068
- dictContentSize = ZDICT_DICTSIZE_MIN;
1113
+ candidateContentSize = ZDICT_DICTSIZE_MIN;
1069
1114
 
1070
- /* Largest dict is initially at least ZDICT_DICTSIZE_MIN */
1071
- while (dictContentSize < largestDict) {
1115
+ while (candidateContentSize < dictContentSize) {
1072
1116
  memcpy(candidateDictBuffer, largestDictbuffer, largestDict);
1073
- dictContentSize = ZDICT_finalizeDictionary(
1074
- candidateDictBuffer, dictBufferCapacity, customDictContentEnd - dictContentSize, dictContentSize,
1117
+ candidateDict = ZDICT_finalizeDictionary(
1118
+ candidateDictBuffer, dictBufferCapacity, customDictContentEnd - candidateContentSize, candidateContentSize,
1075
1119
  samplesBuffer, samplesSizes, nbFinalizeSamples, params.zParams);
1076
1120
 
1077
- if (ZDICT_isError(dictContentSize)) {
1121
+ if (ZDICT_isError(candidateDict)) {
1078
1122
  free(largestDictbuffer);
1079
1123
  free(candidateDictBuffer);
1080
- return COVER_dictSelectionError(dictContentSize);
1124
+ return COVER_dictSelectionError(candidateDict);
1081
1125
 
1082
1126
  }
1083
1127
 
1084
1128
  totalCompressedSize = COVER_checkTotalCompressedSize(params, samplesSizes,
1085
1129
  samplesBuffer, offsets,
1086
1130
  nbCheckSamples, nbSamples,
1087
- candidateDictBuffer, dictContentSize);
1131
+ candidateDictBuffer, candidateDict);
1088
1132
 
1089
1133
  if (ZSTD_isError(totalCompressedSize)) {
1090
1134
  free(largestDictbuffer);
@@ -1094,14 +1138,13 @@ COVER_dictSelection_t COVER_selectDict(BYTE* customDictContent, size_t dictBuffe
1094
1138
 
1095
1139
  if ((double)totalCompressedSize <= (double)largestCompressed * regressionTolerance) {
1096
1140
  free(largestDictbuffer);
1097
- return setDictSelection( candidateDictBuffer, dictContentSize, totalCompressedSize );
1141
+ return setDictSelection( candidateDictBuffer, candidateDict, totalCompressedSize );
1098
1142
  }
1099
- dictContentSize *= 2;
1143
+ candidateContentSize *= 2;
1100
1144
  }
1101
- dictContentSize = largestDict;
1102
1145
  totalCompressedSize = largestCompressed;
1103
1146
  free(candidateDictBuffer);
1104
- return setDictSelection( largestDictbuffer, dictContentSize, totalCompressedSize );
1147
+ return setDictSelection( largestDictbuffer, largestDict, totalCompressedSize );
1105
1148
  }
1106
1149
 
1107
1150
  /**
@@ -1132,6 +1175,7 @@ static void COVER_tryParameters(void *opaque)
1132
1175
  BYTE* const dict = (BYTE*)malloc(dictBufferCapacity);
1133
1176
  COVER_dictSelection_t selection = COVER_dictSelectionError(ERROR(GENERIC));
1134
1177
  U32* const freqs = (U32*)malloc(ctx->suffixSize * sizeof(U32));
1178
+ const int displayLevel = ctx->displayLevel;
1135
1179
  if (!COVER_map_init(&activeDmers, parameters.k - parameters.d + 1)) {
1136
1180
  DISPLAYLEVEL(1, "Failed to allocate dmer map: out of memory\n");
1137
1181
  goto _cleanup;
@@ -1183,21 +1227,22 @@ ZDICTLIB_STATIC_API size_t ZDICT_optimizeTrainFromBuffer_cover(
1183
1227
  (1 + (kMaxD - kMinD) / 2) * (1 + (kMaxK - kMinK) / kStepSize);
1184
1228
  const unsigned shrinkDict = 0;
1185
1229
  /* Local variables */
1186
- const int displayLevel = parameters->zParams.notificationLevel;
1230
+ int displayLevel = (int)parameters->zParams.notificationLevel;
1187
1231
  unsigned iteration = 1;
1188
1232
  unsigned d;
1189
1233
  unsigned k;
1190
1234
  COVER_best_t best;
1191
1235
  POOL_ctx *pool = NULL;
1192
1236
  int warned = 0;
1237
+ clock_t lastUpdateTime = 0;
1193
1238
 
1194
1239
  /* Checks */
1195
1240
  if (splitPoint <= 0 || splitPoint > 1) {
1196
- LOCALDISPLAYLEVEL(displayLevel, 1, "Incorrect parameters\n");
1241
+ DISPLAYLEVEL(1, "Incorrect parameters\n");
1197
1242
  return ERROR(parameter_outOfBound);
1198
1243
  }
1199
1244
  if (kMinK < kMaxD || kMaxK < kMinK) {
1200
- LOCALDISPLAYLEVEL(displayLevel, 1, "Incorrect parameters\n");
1245
+ DISPLAYLEVEL(1, "Incorrect parameters\n");
1201
1246
  return ERROR(parameter_outOfBound);
1202
1247
  }
1203
1248
  if (nbSamples == 0) {
@@ -1217,19 +1262,19 @@ ZDICTLIB_STATIC_API size_t ZDICT_optimizeTrainFromBuffer_cover(
1217
1262
  }
1218
1263
  /* Initialization */
1219
1264
  COVER_best_init(&best);
1220
- /* Turn down global display level to clean up display at level 2 and below */
1221
- g_displayLevel = displayLevel == 0 ? 0 : displayLevel - 1;
1222
1265
  /* Loop through d first because each new value needs a new context */
1223
- LOCALDISPLAYLEVEL(displayLevel, 2, "Trying %u different sets of parameters\n",
1266
+ DISPLAYLEVEL(2, "Trying %u different sets of parameters\n",
1224
1267
  kIterations);
1225
1268
  for (d = kMinD; d <= kMaxD; d += 2) {
1226
1269
  /* Initialize the context for this value of d */
1227
1270
  COVER_ctx_t ctx;
1228
- LOCALDISPLAYLEVEL(displayLevel, 3, "d=%u\n", d);
1271
+ DISPLAYLEVEL(3, "d=%u\n", d);
1229
1272
  {
1230
- const size_t initVal = COVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, d, splitPoint);
1273
+ /* Turn down global display level to clean up display at level 2 and below */
1274
+ const int childDisplayLevel = (displayLevel == 0) ? 0 : displayLevel - 1;
1275
+ const size_t initVal = COVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, d, splitPoint, childDisplayLevel);
1231
1276
  if (ZSTD_isError(initVal)) {
1232
- LOCALDISPLAYLEVEL(displayLevel, 1, "Failed to initialize context\n");
1277
+ DISPLAYLEVEL(1, "Failed to initialize context\n");
1233
1278
  COVER_best_destroy(&best);
1234
1279
  POOL_free(pool);
1235
1280
  return initVal;
@@ -1244,9 +1289,9 @@ ZDICTLIB_STATIC_API size_t ZDICT_optimizeTrainFromBuffer_cover(
1244
1289
  /* Prepare the arguments */
1245
1290
  COVER_tryParameters_data_t *data = (COVER_tryParameters_data_t *)malloc(
1246
1291
  sizeof(COVER_tryParameters_data_t));
1247
- LOCALDISPLAYLEVEL(displayLevel, 3, "k=%u\n", k);
1292
+ DISPLAYLEVEL(3, "k=%u\n", k);
1248
1293
  if (!data) {
1249
- LOCALDISPLAYLEVEL(displayLevel, 1, "Failed to allocate parameters\n");
1294
+ DISPLAYLEVEL(1, "Failed to allocate parameters\n");
1250
1295
  COVER_best_destroy(&best);
1251
1296
  COVER_ctx_destroy(&ctx);
1252
1297
  POOL_free(pool);
@@ -1261,7 +1306,7 @@ ZDICTLIB_STATIC_API size_t ZDICT_optimizeTrainFromBuffer_cover(
1261
1306
  data->parameters.splitPoint = splitPoint;
1262
1307
  data->parameters.steps = kSteps;
1263
1308
  data->parameters.shrinkDict = shrinkDict;
1264
- data->parameters.zParams.notificationLevel = g_displayLevel;
1309
+ data->parameters.zParams.notificationLevel = (unsigned)ctx.displayLevel;
1265
1310
  /* Check the parameters */
1266
1311
  if (!COVER_checkParameters(data->parameters, dictBufferCapacity)) {
1267
1312
  DISPLAYLEVEL(1, "Cover parameters incorrect\n");
@@ -1276,14 +1321,14 @@ ZDICTLIB_STATIC_API size_t ZDICT_optimizeTrainFromBuffer_cover(
1276
1321
  COVER_tryParameters(data);
1277
1322
  }
1278
1323
  /* Print status */
1279
- LOCALDISPLAYUPDATE(displayLevel, 2, "\r%u%% ",
1280
- (unsigned)((iteration * 100) / kIterations));
1324
+ DISPLAYUPDATE(lastUpdateTime, 2, "\r%u%% ",
1325
+ (unsigned)((iteration * 100) / kIterations));
1281
1326
  ++iteration;
1282
1327
  }
1283
1328
  COVER_best_wait(&best);
1284
1329
  COVER_ctx_destroy(&ctx);
1285
1330
  }
1286
- LOCALDISPLAYLEVEL(displayLevel, 2, "\r%79s\r", "");
1331
+ DISPLAYLEVEL(2, "\r%79s\r", "");
1287
1332
  /* Fill the output buffer and parameters with output of the best parameters */
1288
1333
  {
1289
1334
  const size_t dictSize = best.dictSize;