2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 /* *****************************************************************************
12 * Constructs a dictionary using a heuristic based on the following paper:
14 * Liao, Petri, Moffat, Wirth
15 * Effective Construction of Relative Lempel-Ziv Dictionaries
16 * Published in WWW 2016.
18 * Adapted from code originally written by @ot (Giuseppe Ottaviano).
19 ******************************************************************************/
21 /*-*************************************
23 ***************************************/
24 #include <stdio.h> /* fprintf */
25 #include <stdlib.h> /* malloc, free, qsort */
26 #include <string.h> /* memset */
27 #include <time.h> /* clock */
29 #include "mem.h" /* read */
31 #include "threading.h"
33 #include "zstd_internal.h" /* includes zstd.h */
34 #ifndef ZDICT_STATIC_LINKING_ONLY
35 #define ZDICT_STATIC_LINKING_ONLY
39 /*-*************************************
41 ***************************************/
42 #define COVER_MAX_SAMPLES_SIZE (sizeof(size_t) == 8 ? ((unsigned)-1) : ((unsigned)1 GB))
43 #define DEFAULT_SPLITPOINT 1.0
45 /*-*************************************
47 ***************************************/
48 static int g_displayLevel = 2;
49 #define DISPLAY(...) \
51 fprintf(stderr, __VA_ARGS__); \
54 #define LOCALDISPLAYLEVEL(displayLevel, l, ...) \
55 if (displayLevel >= l) { \
56 DISPLAY(__VA_ARGS__); \
57 } /* 0 : no display; 1: errors; 2: default; 3: details; 4: debug */
58 #define DISPLAYLEVEL(l, ...) LOCALDISPLAYLEVEL(g_displayLevel, l, __VA_ARGS__)
60 #define LOCALDISPLAYUPDATE(displayLevel, l, ...) \
61 if (displayLevel >= l) { \
62 if ((clock() - g_time > refreshRate) || (displayLevel >= 4)) { \
64 DISPLAY(__VA_ARGS__); \
67 #define DISPLAYUPDATE(l, ...) LOCALDISPLAYUPDATE(g_displayLevel, l, __VA_ARGS__)
68 static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
69 static clock_t g_time = 0;
71 /*-*************************************
73 ***************************************
74 * A small specialized hash map for storing activeDmers.
75 * The map does not resize, so if it becomes full it will loop forever.
76 * Thus, the map must be large enough to store every value.
77 * The map implements linear probing and keeps its load less than 0.5.
80 #define MAP_EMPTY_VALUE ((U32)-1)
81 typedef struct COVER_map_pair_t_s {
86 typedef struct COVER_map_s {
87 COVER_map_pair_t *data;
96 static void COVER_map_clear(COVER_map_t *map) {
97 memset(map->data, MAP_EMPTY_VALUE, map->size * sizeof(COVER_map_pair_t));
101 * Initializes a map of the given size.
102 * Returns 1 on success and 0 on failure.
103 * The map must be destroyed with COVER_map_destroy().
104 * The map is only guaranteed to be large enough to hold size elements.
106 static int COVER_map_init(COVER_map_t *map, U32 size) {
107 map->sizeLog = ZSTD_highbit32(size) + 2;
108 map->size = (U32)1 << map->sizeLog;
109 map->sizeMask = map->size - 1;
110 map->data = (COVER_map_pair_t *)malloc(map->size * sizeof(COVER_map_pair_t));
116 COVER_map_clear(map);
121 * Internal hash function
123 static const U32 prime4bytes = 2654435761U;
124 static U32 COVER_map_hash(COVER_map_t *map, U32 key) {
125 return (key * prime4bytes) >> (32 - map->sizeLog);
129 * Helper function that returns the index that a key should be placed into.
131 static U32 COVER_map_index(COVER_map_t *map, U32 key) {
132 const U32 hash = COVER_map_hash(map, key);
134 for (i = hash;; i = (i + 1) & map->sizeMask) {
135 COVER_map_pair_t *pos = &map->data[i];
136 if (pos->value == MAP_EMPTY_VALUE) {
139 if (pos->key == key) {
146 * Returns the pointer to the value for key.
147 * If key is not in the map, it is inserted and the value is set to 0.
148 * The map must not be full.
150 static U32 *COVER_map_at(COVER_map_t *map, U32 key) {
151 COVER_map_pair_t *pos = &map->data[COVER_map_index(map, key)];
152 if (pos->value == MAP_EMPTY_VALUE) {
160 * Deletes key from the map if present.
162 static void COVER_map_remove(COVER_map_t *map, U32 key) {
163 U32 i = COVER_map_index(map, key);
164 COVER_map_pair_t *del = &map->data[i];
166 if (del->value == MAP_EMPTY_VALUE) {
169 for (i = (i + 1) & map->sizeMask;; i = (i + 1) & map->sizeMask) {
170 COVER_map_pair_t *const pos = &map->data[i];
171 /* If the position is empty we are done */
172 if (pos->value == MAP_EMPTY_VALUE) {
173 del->value = MAP_EMPTY_VALUE;
176 /* If pos can be moved to del do so */
177 if (((i - COVER_map_hash(map, pos->key)) & map->sizeMask) >= shift) {
179 del->value = pos->value;
189 * Destroys a map that is inited with COVER_map_init().
191 static void COVER_map_destroy(COVER_map_t *map) {
199 /*-*************************************
201 ***************************************/
206 const size_t *samplesSizes;
208 size_t nbTrainSamples;
209 size_t nbTestSamples;
217 /* We need a global context for qsort... */
218 static COVER_ctx_t *g_ctx = NULL;
220 /*-*************************************
222 ***************************************/
225 * Returns the sum of the sample sizes.
227 size_t COVER_sum(const size_t *samplesSizes, unsigned nbSamples) {
230 for (i = 0; i < nbSamples; ++i) {
231 sum += samplesSizes[i];
237 * Returns -1 if the dmer at lp is less than the dmer at rp.
238 * Return 0 if the dmers at lp and rp are equal.
239 * Returns 1 if the dmer at lp is greater than the dmer at rp.
241 static int COVER_cmp(COVER_ctx_t *ctx, const void *lp, const void *rp) {
242 U32 const lhs = *(U32 const *)lp;
243 U32 const rhs = *(U32 const *)rp;
244 return memcmp(ctx->samples + lhs, ctx->samples + rhs, ctx->d);
247 * Faster version for d <= 8.
249 static int COVER_cmp8(COVER_ctx_t *ctx, const void *lp, const void *rp) {
250 U64 const mask = (ctx->d == 8) ? (U64)-1 : (((U64)1 << (8 * ctx->d)) - 1);
251 U64 const lhs = MEM_readLE64(ctx->samples + *(U32 const *)lp) & mask;
252 U64 const rhs = MEM_readLE64(ctx->samples + *(U32 const *)rp) & mask;
260 * Same as COVER_cmp() except ties are broken by pointer value
261 * NOTE: g_ctx must be set to call this function. A global is required because
262 * qsort doesn't take an opaque pointer.
264 static int COVER_strict_cmp(const void *lp, const void *rp) {
265 int result = COVER_cmp(g_ctx, lp, rp);
267 result = lp < rp ? -1 : 1;
272 * Faster version for d <= 8.
274 static int COVER_strict_cmp8(const void *lp, const void *rp) {
275 int result = COVER_cmp8(g_ctx, lp, rp);
277 result = lp < rp ? -1 : 1;
283 * Returns the first pointer in [first, last) whose element does not compare
284 * less than value. If no such element exists it returns last.
286 static const size_t *COVER_lower_bound(const size_t *first, const size_t *last,
288 size_t count = last - first;
290 size_t step = count / 2;
291 const size_t *ptr = first;
304 * Generic groupBy function.
305 * Groups an array sorted by cmp into groups with equivalent values.
306 * Calls grp for each group.
309 COVER_groupBy(const void *data, size_t count, size_t size, COVER_ctx_t *ctx,
310 int (*cmp)(COVER_ctx_t *, const void *, const void *),
311 void (*grp)(COVER_ctx_t *, const void *, const void *)) {
312 const BYTE *ptr = (const BYTE *)data;
314 while (num < count) {
315 const BYTE *grpEnd = ptr + size;
317 while (num < count && cmp(ctx, ptr, grpEnd) == 0) {
321 grp(ctx, ptr, grpEnd);
326 /*-*************************************
328 ***************************************/
331 * Called on each group of positions with the same dmer.
332 * Counts the frequency of each dmer and saves it in the suffix array.
333 * Fills `ctx->dmerAt`.
335 static void COVER_group(COVER_ctx_t *ctx, const void *group,
336 const void *groupEnd) {
337 /* The group consists of all the positions with the same first d bytes. */
338 const U32 *grpPtr = (const U32 *)group;
339 const U32 *grpEnd = (const U32 *)groupEnd;
340 /* The dmerId is how we will reference this dmer.
341 * This allows us to map the whole dmer space to a much smaller space, the
342 * size of the suffix array.
344 const U32 dmerId = (U32)(grpPtr - ctx->suffix);
345 /* Count the number of samples this dmer shows up in */
348 const size_t *curOffsetPtr = ctx->offsets;
349 const size_t *offsetsEnd = ctx->offsets + ctx->nbSamples;
350 /* Once *grpPtr >= curSampleEnd this occurrence of the dmer is in a
351 * different sample than the last.
353 size_t curSampleEnd = ctx->offsets[0];
354 for (; grpPtr != grpEnd; ++grpPtr) {
355 /* Save the dmerId for this position so we can get back to it. */
356 ctx->dmerAt[*grpPtr] = dmerId;
357 /* Dictionaries only help for the first reference to the dmer.
358 * After that zstd can reference the match from the previous reference.
359 * So only count each dmer once for each sample it is in.
361 if (*grpPtr < curSampleEnd) {
365 /* Binary search to find the end of the sample *grpPtr is in.
366 * In the common case that grpPtr + 1 == grpEnd we can skip the binary
367 * search because the loop is over.
369 if (grpPtr + 1 != grpEnd) {
370 const size_t *sampleEndPtr =
371 COVER_lower_bound(curOffsetPtr, offsetsEnd, *grpPtr);
372 curSampleEnd = *sampleEndPtr;
373 curOffsetPtr = sampleEndPtr + 1;
376 /* At this point we are never going to look at this segment of the suffix
377 * array again. We take advantage of this fact to save memory.
378 * We store the frequency of the dmer in the first position of the group,
381 ctx->suffix[dmerId] = freq;
386 * Selects the best segment in an epoch.
387 * Segments of are scored according to the function:
389 * Let F(d) be the frequency of dmer d.
390 * Let S_i be the dmer at position i of segment S which has length k.
392 * Score(S) = F(S_1) + F(S_2) + ... + F(S_{k-d+1})
394 * Once the dmer d is in the dictionay we set F(d) = 0.
396 static COVER_segment_t COVER_selectSegment(const COVER_ctx_t *ctx, U32 *freqs,
397 COVER_map_t *activeDmers, U32 begin,
399 ZDICT_cover_params_t parameters) {
401 const U32 k = parameters.k;
402 const U32 d = parameters.d;
403 const U32 dmersInK = k - d + 1;
404 /* Try each segment (activeSegment) and save the best (bestSegment) */
405 COVER_segment_t bestSegment = {0, 0, 0};
406 COVER_segment_t activeSegment;
407 /* Reset the activeDmers in the segment */
408 COVER_map_clear(activeDmers);
409 /* The activeSegment starts at the beginning of the epoch. */
410 activeSegment.begin = begin;
411 activeSegment.end = begin;
412 activeSegment.score = 0;
413 /* Slide the activeSegment through the whole epoch.
414 * Save the best segment in bestSegment.
416 while (activeSegment.end < end) {
417 /* The dmerId for the dmer at the next position */
418 U32 newDmer = ctx->dmerAt[activeSegment.end];
419 /* The entry in activeDmers for this dmerId */
420 U32 *newDmerOcc = COVER_map_at(activeDmers, newDmer);
421 /* If the dmer isn't already present in the segment add its score. */
422 if (*newDmerOcc == 0) {
423 /* The paper suggest using the L-0.5 norm, but experiments show that it
426 activeSegment.score += freqs[newDmer];
428 /* Add the dmer to the segment */
429 activeSegment.end += 1;
432 /* If the window is now too large, drop the first position */
433 if (activeSegment.end - activeSegment.begin == dmersInK + 1) {
434 U32 delDmer = ctx->dmerAt[activeSegment.begin];
435 U32 *delDmerOcc = COVER_map_at(activeDmers, delDmer);
436 activeSegment.begin += 1;
438 /* If this is the last occurence of the dmer, subtract its score */
439 if (*delDmerOcc == 0) {
440 COVER_map_remove(activeDmers, delDmer);
441 activeSegment.score -= freqs[delDmer];
445 /* If this segment is the best so far save it */
446 if (activeSegment.score > bestSegment.score) {
447 bestSegment = activeSegment;
451 /* Trim off the zero frequency head and tail from the segment. */
452 U32 newBegin = bestSegment.end;
453 U32 newEnd = bestSegment.begin;
455 for (pos = bestSegment.begin; pos != bestSegment.end; ++pos) {
456 U32 freq = freqs[ctx->dmerAt[pos]];
458 newBegin = MIN(newBegin, pos);
462 bestSegment.begin = newBegin;
463 bestSegment.end = newEnd;
466 /* Zero out the frequency of each dmer covered by the chosen segment. */
468 for (pos = bestSegment.begin; pos != bestSegment.end; ++pos) {
469 freqs[ctx->dmerAt[pos]] = 0;
476 * Check the validity of the parameters.
477 * Returns non-zero if the parameters are valid and 0 otherwise.
479 static int COVER_checkParameters(ZDICT_cover_params_t parameters,
480 size_t maxDictSize) {
481 /* k and d are required parameters */
482 if (parameters.d == 0 || parameters.k == 0) {
485 /* k <= maxDictSize */
486 if (parameters.k > maxDictSize) {
490 if (parameters.d > parameters.k) {
493 /* 0 < splitPoint <= 1 */
494 if (parameters.splitPoint <= 0 || parameters.splitPoint > 1){
501 * Clean up a context initialized with `COVER_ctx_init()`.
503 static void COVER_ctx_destroy(COVER_ctx_t *ctx) {
526 * Prepare a context for dictionary building.
527 * The context is only dependent on the parameter `d` and can used multiple
529 * Returns 1 on success or zero on error.
530 * The context must be destroyed with `COVER_ctx_destroy()`.
532 static int COVER_ctx_init(COVER_ctx_t *ctx, const void *samplesBuffer,
533 const size_t *samplesSizes, unsigned nbSamples,
534 unsigned d, double splitPoint) {
535 const BYTE *const samples = (const BYTE *)samplesBuffer;
536 const size_t totalSamplesSize = COVER_sum(samplesSizes, nbSamples);
537 /* Split samples into testing and training sets */
538 const unsigned nbTrainSamples = splitPoint < 1.0 ? (unsigned)((double)nbSamples * splitPoint) : nbSamples;
539 const unsigned nbTestSamples = splitPoint < 1.0 ? nbSamples - nbTrainSamples : nbSamples;
540 const size_t trainingSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes, nbTrainSamples) : totalSamplesSize;
541 const size_t testSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes + nbTrainSamples, nbTestSamples) : totalSamplesSize;
543 if (totalSamplesSize < MAX(d, sizeof(U64)) ||
544 totalSamplesSize >= (size_t)COVER_MAX_SAMPLES_SIZE) {
545 DISPLAYLEVEL(1, "Total samples size is too large (%u MB), maximum size is %u MB\n",
546 (unsigned)(totalSamplesSize>>20), (COVER_MAX_SAMPLES_SIZE >> 20));
549 /* Check if there are at least 5 training samples */
550 if (nbTrainSamples < 5) {
551 DISPLAYLEVEL(1, "Total number of training samples is %u and is invalid.", nbTrainSamples);
554 /* Check if there's testing sample */
555 if (nbTestSamples < 1) {
556 DISPLAYLEVEL(1, "Total number of testing samples is %u and is invalid.", nbTestSamples);
559 /* Zero the context */
560 memset(ctx, 0, sizeof(*ctx));
561 DISPLAYLEVEL(2, "Training on %u samples of total size %u\n", nbTrainSamples,
562 (unsigned)trainingSamplesSize);
563 DISPLAYLEVEL(2, "Testing on %u samples of total size %u\n", nbTestSamples,
564 (unsigned)testSamplesSize);
565 ctx->samples = samples;
566 ctx->samplesSizes = samplesSizes;
567 ctx->nbSamples = nbSamples;
568 ctx->nbTrainSamples = nbTrainSamples;
569 ctx->nbTestSamples = nbTestSamples;
570 /* Partial suffix array */
571 ctx->suffixSize = trainingSamplesSize - MAX(d, sizeof(U64)) + 1;
572 ctx->suffix = (U32 *)malloc(ctx->suffixSize * sizeof(U32));
573 /* Maps index to the dmerID */
574 ctx->dmerAt = (U32 *)malloc(ctx->suffixSize * sizeof(U32));
575 /* The offsets of each file */
576 ctx->offsets = (size_t *)malloc((nbSamples + 1) * sizeof(size_t));
577 if (!ctx->suffix || !ctx->dmerAt || !ctx->offsets) {
578 DISPLAYLEVEL(1, "Failed to allocate scratch buffers\n");
579 COVER_ctx_destroy(ctx);
585 /* Fill offsets from the samplesSizes */
589 for (i = 1; i <= nbSamples; ++i) {
590 ctx->offsets[i] = ctx->offsets[i - 1] + samplesSizes[i - 1];
593 DISPLAYLEVEL(2, "Constructing partial suffix array\n");
595 /* suffix is a partial suffix array.
596 * It only sorts suffixes by their first parameters.d bytes.
597 * The sort is stable, so each dmer group is sorted by position in input.
600 for (i = 0; i < ctx->suffixSize; ++i) {
603 /* qsort doesn't take an opaque pointer, so pass as a global.
604 * On OpenBSD qsort() is not guaranteed to be stable, their mergesort() is.
607 #if defined(__OpenBSD__)
608 mergesort(ctx->suffix, ctx->suffixSize, sizeof(U32),
609 (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
611 qsort(ctx->suffix, ctx->suffixSize, sizeof(U32),
612 (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
615 DISPLAYLEVEL(2, "Computing frequencies\n");
616 /* For each dmer group (group of positions with the same first d bytes):
617 * 1. For each position we set dmerAt[position] = dmerID. The dmerID is
618 * (groupBeginPtr - suffix). This allows us to go from position to
619 * dmerID so we can look up values in freq.
620 * 2. We calculate how many samples the dmer occurs in and save it in
623 COVER_groupBy(ctx->suffix, ctx->suffixSize, sizeof(U32), ctx,
624 (ctx->d <= 8 ? &COVER_cmp8 : &COVER_cmp), &COVER_group);
625 ctx->freqs = ctx->suffix;
631 * Given the prepared context build the dictionary.
633 static size_t COVER_buildDictionary(const COVER_ctx_t *ctx, U32 *freqs,
634 COVER_map_t *activeDmers, void *dictBuffer,
635 size_t dictBufferCapacity,
636 ZDICT_cover_params_t parameters) {
637 BYTE *const dict = (BYTE *)dictBuffer;
638 size_t tail = dictBufferCapacity;
639 /* Divide the data up into epochs of equal size.
640 * We will select at least one segment from each epoch.
642 const unsigned epochs = MAX(1, (U32)(dictBufferCapacity / parameters.k / 4));
643 const unsigned epochSize = (U32)(ctx->suffixSize / epochs);
645 DISPLAYLEVEL(2, "Breaking content into %u epochs of size %u\n",
647 /* Loop through the epochs until there are no more segments or the dictionary
650 for (epoch = 0; tail > 0; epoch = (epoch + 1) % epochs) {
651 const U32 epochBegin = (U32)(epoch * epochSize);
652 const U32 epochEnd = epochBegin + epochSize;
654 /* Select a segment */
655 COVER_segment_t segment = COVER_selectSegment(
656 ctx, freqs, activeDmers, epochBegin, epochEnd, parameters);
657 /* If the segment covers no dmers, then we are out of content */
658 if (segment.score == 0) {
661 /* Trim the segment if necessary and if it is too small then we are done */
662 segmentSize = MIN(segment.end - segment.begin + parameters.d - 1, tail);
663 if (segmentSize < parameters.d) {
666 /* We fill the dictionary from the back to allow the best segments to be
667 * referenced with the smallest offsets.
670 memcpy(dict + tail, ctx->samples + segment.begin, segmentSize);
673 (unsigned)(((dictBufferCapacity - tail) * 100) / dictBufferCapacity));
675 DISPLAYLEVEL(2, "\r%79s\r", "");
679 ZDICTLIB_API size_t ZDICT_trainFromBuffer_cover(
680 void *dictBuffer, size_t dictBufferCapacity,
681 const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples,
682 ZDICT_cover_params_t parameters)
684 BYTE* const dict = (BYTE*)dictBuffer;
686 COVER_map_t activeDmers;
687 parameters.splitPoint = 1.0;
688 /* Initialize global data */
689 g_displayLevel = parameters.zParams.notificationLevel;
691 if (!COVER_checkParameters(parameters, dictBufferCapacity)) {
692 DISPLAYLEVEL(1, "Cover parameters incorrect\n");
693 return ERROR(GENERIC);
695 if (nbSamples == 0) {
696 DISPLAYLEVEL(1, "Cover must have at least one input file\n");
697 return ERROR(GENERIC);
699 if (dictBufferCapacity < ZDICT_DICTSIZE_MIN) {
700 DISPLAYLEVEL(1, "dictBufferCapacity must be at least %u\n",
702 return ERROR(dstSize_tooSmall);
704 /* Initialize context and activeDmers */
705 if (!COVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples,
706 parameters.d, parameters.splitPoint)) {
707 return ERROR(GENERIC);
709 if (!COVER_map_init(&activeDmers, parameters.k - parameters.d + 1)) {
710 DISPLAYLEVEL(1, "Failed to allocate dmer map: out of memory\n");
711 COVER_ctx_destroy(&ctx);
712 return ERROR(GENERIC);
715 DISPLAYLEVEL(2, "Building dictionary\n");
718 COVER_buildDictionary(&ctx, ctx.freqs, &activeDmers, dictBuffer,
719 dictBufferCapacity, parameters);
720 const size_t dictionarySize = ZDICT_finalizeDictionary(
721 dict, dictBufferCapacity, dict + tail, dictBufferCapacity - tail,
722 samplesBuffer, samplesSizes, nbSamples, parameters.zParams);
723 if (!ZSTD_isError(dictionarySize)) {
724 DISPLAYLEVEL(2, "Constructed dictionary of size %u\n",
725 (unsigned)dictionarySize);
727 COVER_ctx_destroy(&ctx);
728 COVER_map_destroy(&activeDmers);
729 return dictionarySize;
735 size_t COVER_checkTotalCompressedSize(const ZDICT_cover_params_t parameters,
736 const size_t *samplesSizes, const BYTE *samples,
738 size_t nbTrainSamples, size_t nbSamples,
739 BYTE *const dict, size_t dictBufferCapacity) {
740 size_t totalCompressedSize = ERROR(GENERIC);
745 /* Local variables */
748 /* Allocate dst with enough space to compress the maximum sized sample */
750 size_t maxSampleSize = 0;
751 i = parameters.splitPoint < 1.0 ? nbTrainSamples : 0;
752 for (; i < nbSamples; ++i) {
753 maxSampleSize = MAX(samplesSizes[i], maxSampleSize);
755 dstCapacity = ZSTD_compressBound(maxSampleSize);
756 dst = malloc(dstCapacity);
758 /* Create the cctx and cdict */
759 cctx = ZSTD_createCCtx();
760 cdict = ZSTD_createCDict(dict, dictBufferCapacity,
761 parameters.zParams.compressionLevel);
762 if (!dst || !cctx || !cdict) {
763 goto _compressCleanup;
765 /* Compress each sample and sum their sizes (or error) */
766 totalCompressedSize = dictBufferCapacity;
767 i = parameters.splitPoint < 1.0 ? nbTrainSamples : 0;
768 for (; i < nbSamples; ++i) {
769 const size_t size = ZSTD_compress_usingCDict(
770 cctx, dst, dstCapacity, samples + offsets[i],
771 samplesSizes[i], cdict);
772 if (ZSTD_isError(size)) {
773 totalCompressedSize = ERROR(GENERIC);
774 goto _compressCleanup;
776 totalCompressedSize += size;
780 ZSTD_freeCDict(cdict);
784 return totalCompressedSize;
789 * Initialize the `COVER_best_t`.
791 void COVER_best_init(COVER_best_t *best) {
792 if (best==NULL) return; /* compatible with init on NULL */
793 (void)ZSTD_pthread_mutex_init(&best->mutex, NULL);
794 (void)ZSTD_pthread_cond_init(&best->cond, NULL);
798 best->compressedSize = (size_t)-1;
799 memset(&best->parameters, 0, sizeof(best->parameters));
803 * Wait until liveJobs == 0.
805 void COVER_best_wait(COVER_best_t *best) {
809 ZSTD_pthread_mutex_lock(&best->mutex);
810 while (best->liveJobs != 0) {
811 ZSTD_pthread_cond_wait(&best->cond, &best->mutex);
813 ZSTD_pthread_mutex_unlock(&best->mutex);
817 * Call COVER_best_wait() and then destroy the COVER_best_t.
819 void COVER_best_destroy(COVER_best_t *best) {
823 COVER_best_wait(best);
827 ZSTD_pthread_mutex_destroy(&best->mutex);
828 ZSTD_pthread_cond_destroy(&best->cond);
832 * Called when a thread is about to be launched.
833 * Increments liveJobs.
835 void COVER_best_start(COVER_best_t *best) {
839 ZSTD_pthread_mutex_lock(&best->mutex);
841 ZSTD_pthread_mutex_unlock(&best->mutex);
845 * Called when a thread finishes executing, both on error or success.
846 * Decrements liveJobs and signals any waiting threads if liveJobs == 0.
847 * If this dictionary is the best so far save it and its parameters.
849 void COVER_best_finish(COVER_best_t *best, size_t compressedSize,
850 ZDICT_cover_params_t parameters, void *dict,
857 ZSTD_pthread_mutex_lock(&best->mutex);
859 liveJobs = best->liveJobs;
860 /* If the new dictionary is better */
861 if (compressedSize < best->compressedSize) {
862 /* Allocate space if necessary */
863 if (!best->dict || best->dictSize < dictSize) {
867 best->dict = malloc(dictSize);
869 best->compressedSize = ERROR(GENERIC);
871 ZSTD_pthread_cond_signal(&best->cond);
872 ZSTD_pthread_mutex_unlock(&best->mutex);
876 /* Save the dictionary, parameters, and size */
877 memcpy(best->dict, dict, dictSize);
878 best->dictSize = dictSize;
879 best->parameters = parameters;
880 best->compressedSize = compressedSize;
883 ZSTD_pthread_cond_broadcast(&best->cond);
885 ZSTD_pthread_mutex_unlock(&best->mutex);
890 * Parameters for COVER_tryParameters().
892 typedef struct COVER_tryParameters_data_s {
893 const COVER_ctx_t *ctx;
895 size_t dictBufferCapacity;
896 ZDICT_cover_params_t parameters;
897 } COVER_tryParameters_data_t;
900 * Tries a set of parameters and updates the COVER_best_t with the results.
901 * This function is thread safe if zstd is compiled with multithreaded support.
902 * It takes its parameters as an *OWNING* opaque pointer to support threading.
904 static void COVER_tryParameters(void *opaque) {
905 /* Save parameters as local variables */
906 COVER_tryParameters_data_t *const data = (COVER_tryParameters_data_t *)opaque;
907 const COVER_ctx_t *const ctx = data->ctx;
908 const ZDICT_cover_params_t parameters = data->parameters;
909 size_t dictBufferCapacity = data->dictBufferCapacity;
910 size_t totalCompressedSize = ERROR(GENERIC);
911 /* Allocate space for hash table, dict, and freqs */
912 COVER_map_t activeDmers;
913 BYTE *const dict = (BYTE * const)malloc(dictBufferCapacity);
914 U32 *freqs = (U32 *)malloc(ctx->suffixSize * sizeof(U32));
915 if (!COVER_map_init(&activeDmers, parameters.k - parameters.d + 1)) {
916 DISPLAYLEVEL(1, "Failed to allocate dmer map: out of memory\n");
919 if (!dict || !freqs) {
920 DISPLAYLEVEL(1, "Failed to allocate buffers: out of memory\n");
923 /* Copy the frequencies because we need to modify them */
924 memcpy(freqs, ctx->freqs, ctx->suffixSize * sizeof(U32));
925 /* Build the dictionary */
927 const size_t tail = COVER_buildDictionary(ctx, freqs, &activeDmers, dict,
928 dictBufferCapacity, parameters);
929 dictBufferCapacity = ZDICT_finalizeDictionary(
930 dict, dictBufferCapacity, dict + tail, dictBufferCapacity - tail,
931 ctx->samples, ctx->samplesSizes, (unsigned)ctx->nbTrainSamples,
933 if (ZDICT_isError(dictBufferCapacity)) {
934 DISPLAYLEVEL(1, "Failed to finalize dictionary\n");
938 /* Check total compressed size */
939 totalCompressedSize = COVER_checkTotalCompressedSize(parameters, ctx->samplesSizes,
940 ctx->samples, ctx->offsets,
941 ctx->nbTrainSamples, ctx->nbSamples,
942 dict, dictBufferCapacity);
945 COVER_best_finish(data->best, totalCompressedSize, parameters, dict,
948 COVER_map_destroy(&activeDmers);
957 ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover(
958 void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer,
959 const size_t *samplesSizes, unsigned nbSamples,
960 ZDICT_cover_params_t *parameters) {
962 const unsigned nbThreads = parameters->nbThreads;
963 const double splitPoint =
964 parameters->splitPoint <= 0.0 ? DEFAULT_SPLITPOINT : parameters->splitPoint;
965 const unsigned kMinD = parameters->d == 0 ? 6 : parameters->d;
966 const unsigned kMaxD = parameters->d == 0 ? 8 : parameters->d;
967 const unsigned kMinK = parameters->k == 0 ? 50 : parameters->k;
968 const unsigned kMaxK = parameters->k == 0 ? 2000 : parameters->k;
969 const unsigned kSteps = parameters->steps == 0 ? 40 : parameters->steps;
970 const unsigned kStepSize = MAX((kMaxK - kMinK) / kSteps, 1);
971 const unsigned kIterations =
972 (1 + (kMaxD - kMinD) / 2) * (1 + (kMaxK - kMinK) / kStepSize);
973 /* Local variables */
974 const int displayLevel = parameters->zParams.notificationLevel;
975 unsigned iteration = 1;
979 POOL_ctx *pool = NULL;
982 if (splitPoint <= 0 || splitPoint > 1) {
983 LOCALDISPLAYLEVEL(displayLevel, 1, "Incorrect parameters\n");
984 return ERROR(GENERIC);
986 if (kMinK < kMaxD || kMaxK < kMinK) {
987 LOCALDISPLAYLEVEL(displayLevel, 1, "Incorrect parameters\n");
988 return ERROR(GENERIC);
990 if (nbSamples == 0) {
991 DISPLAYLEVEL(1, "Cover must have at least one input file\n");
992 return ERROR(GENERIC);
994 if (dictBufferCapacity < ZDICT_DICTSIZE_MIN) {
995 DISPLAYLEVEL(1, "dictBufferCapacity must be at least %u\n",
997 return ERROR(dstSize_tooSmall);
1000 pool = POOL_create(nbThreads, 1);
1002 return ERROR(memory_allocation);
1005 /* Initialization */
1006 COVER_best_init(&best);
1007 /* Turn down global display level to clean up display at level 2 and below */
1008 g_displayLevel = displayLevel == 0 ? 0 : displayLevel - 1;
1009 /* Loop through d first because each new value needs a new context */
1010 LOCALDISPLAYLEVEL(displayLevel, 2, "Trying %u different sets of parameters\n",
1012 for (d = kMinD; d <= kMaxD; d += 2) {
1013 /* Initialize the context for this value of d */
1015 LOCALDISPLAYLEVEL(displayLevel, 3, "d=%u\n", d);
1016 if (!COVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, d, splitPoint)) {
1017 LOCALDISPLAYLEVEL(displayLevel, 1, "Failed to initialize context\n");
1018 COVER_best_destroy(&best);
1020 return ERROR(GENERIC);
1022 /* Loop through k reusing the same context */
1023 for (k = kMinK; k <= kMaxK; k += kStepSize) {
1024 /* Prepare the arguments */
1025 COVER_tryParameters_data_t *data = (COVER_tryParameters_data_t *)malloc(
1026 sizeof(COVER_tryParameters_data_t));
1027 LOCALDISPLAYLEVEL(displayLevel, 3, "k=%u\n", k);
1029 LOCALDISPLAYLEVEL(displayLevel, 1, "Failed to allocate parameters\n");
1030 COVER_best_destroy(&best);
1031 COVER_ctx_destroy(&ctx);
1033 return ERROR(GENERIC);
1037 data->dictBufferCapacity = dictBufferCapacity;
1038 data->parameters = *parameters;
1039 data->parameters.k = k;
1040 data->parameters.d = d;
1041 data->parameters.splitPoint = splitPoint;
1042 data->parameters.steps = kSteps;
1043 data->parameters.zParams.notificationLevel = g_displayLevel;
1044 /* Check the parameters */
1045 if (!COVER_checkParameters(data->parameters, dictBufferCapacity)) {
1046 DISPLAYLEVEL(1, "Cover parameters incorrect\n");
1050 /* Call the function and pass ownership of data to it */
1051 COVER_best_start(&best);
1053 POOL_add(pool, &COVER_tryParameters, data);
1055 COVER_tryParameters(data);
1058 LOCALDISPLAYUPDATE(displayLevel, 2, "\r%u%% ",
1059 (unsigned)((iteration * 100) / kIterations));
1062 COVER_best_wait(&best);
1063 COVER_ctx_destroy(&ctx);
1065 LOCALDISPLAYLEVEL(displayLevel, 2, "\r%79s\r", "");
1066 /* Fill the output buffer and parameters with output of the best parameters */
1068 const size_t dictSize = best.dictSize;
1069 if (ZSTD_isError(best.compressedSize)) {
1070 const size_t compressedSize = best.compressedSize;
1071 COVER_best_destroy(&best);
1073 return compressedSize;
1075 *parameters = best.parameters;
1076 memcpy(dictBuffer, best.dict, dictSize);
1077 COVER_best_destroy(&best);