1*5ff13fbcSAllan Jude /* 2*5ff13fbcSAllan Jude * Copyright (c) Yann Collet, Facebook, Inc. 3*5ff13fbcSAllan Jude * All rights reserved. 4*5ff13fbcSAllan Jude * 5*5ff13fbcSAllan Jude * This source code is licensed under both the BSD-style license (found in the 6*5ff13fbcSAllan Jude * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7*5ff13fbcSAllan Jude * in the COPYING file in the root directory of this source tree). 8*5ff13fbcSAllan Jude * You may select, at your option, one of the above-listed licenses. 9*5ff13fbcSAllan Jude */ 10*5ff13fbcSAllan Jude 11*5ff13fbcSAllan Jude #ifndef DICTBUILDER_H_001 12*5ff13fbcSAllan Jude #define DICTBUILDER_H_001 13*5ff13fbcSAllan Jude 14*5ff13fbcSAllan Jude #if defined (__cplusplus) 15*5ff13fbcSAllan Jude extern "C" { 16*5ff13fbcSAllan Jude #endif 17*5ff13fbcSAllan Jude 18*5ff13fbcSAllan Jude 19*5ff13fbcSAllan Jude /*====== Dependencies ======*/ 20*5ff13fbcSAllan Jude #include <stddef.h> /* size_t */ 21*5ff13fbcSAllan Jude 22*5ff13fbcSAllan Jude 23*5ff13fbcSAllan Jude /* ===== ZDICTLIB_API : control library symbols visibility ===== */ 24*5ff13fbcSAllan Jude #ifndef ZDICTLIB_VISIBILITY 25*5ff13fbcSAllan Jude # if defined(__GNUC__) && (__GNUC__ >= 4) 26*5ff13fbcSAllan Jude # define ZDICTLIB_VISIBILITY __attribute__ ((visibility ("default"))) 27*5ff13fbcSAllan Jude # else 28*5ff13fbcSAllan Jude # define ZDICTLIB_VISIBILITY 29*5ff13fbcSAllan Jude # endif 30*5ff13fbcSAllan Jude #endif 31*5ff13fbcSAllan Jude #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) 32*5ff13fbcSAllan Jude # define ZDICTLIB_API __declspec(dllexport) ZDICTLIB_VISIBILITY 33*5ff13fbcSAllan Jude #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) 34*5ff13fbcSAllan Jude # define ZDICTLIB_API __declspec(dllimport) ZDICTLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ 35*5ff13fbcSAllan Jude #else 36*5ff13fbcSAllan Jude # define ZDICTLIB_API ZDICTLIB_VISIBILITY 37*5ff13fbcSAllan Jude #endif 38*5ff13fbcSAllan Jude 39*5ff13fbcSAllan Jude /******************************************************************************* 40*5ff13fbcSAllan Jude * Zstd dictionary builder 41*5ff13fbcSAllan Jude * 42*5ff13fbcSAllan Jude * FAQ 43*5ff13fbcSAllan Jude * === 44*5ff13fbcSAllan Jude * Why should I use a dictionary? 45*5ff13fbcSAllan Jude * ------------------------------ 46*5ff13fbcSAllan Jude * 47*5ff13fbcSAllan Jude * Zstd can use dictionaries to improve compression ratio of small data. 48*5ff13fbcSAllan Jude * Traditionally small files don't compress well because there is very little 49*5ff13fbcSAllan Jude * repetition in a single sample, since it is small. But, if you are compressing 50*5ff13fbcSAllan Jude * many similar files, like a bunch of JSON records that share the same 51*5ff13fbcSAllan Jude * structure, you can train a dictionary on ahead of time on some samples of 52*5ff13fbcSAllan Jude * these files. Then, zstd can use the dictionary to find repetitions that are 53*5ff13fbcSAllan Jude * present across samples. This can vastly improve compression ratio. 54*5ff13fbcSAllan Jude * 55*5ff13fbcSAllan Jude * When is a dictionary useful? 56*5ff13fbcSAllan Jude * ---------------------------- 57*5ff13fbcSAllan Jude * 58*5ff13fbcSAllan Jude * Dictionaries are useful when compressing many small files that are similar. 59*5ff13fbcSAllan Jude * The larger a file is, the less benefit a dictionary will have. Generally, 60*5ff13fbcSAllan Jude * we don't expect dictionary compression to be effective past 100KB. And the 61*5ff13fbcSAllan Jude * smaller a file is, the more we would expect the dictionary to help. 62*5ff13fbcSAllan Jude * 63*5ff13fbcSAllan Jude * How do I use a dictionary? 64*5ff13fbcSAllan Jude * -------------------------- 65*5ff13fbcSAllan Jude * 66*5ff13fbcSAllan Jude * Simply pass the dictionary to the zstd compressor with 67*5ff13fbcSAllan Jude * `ZSTD_CCtx_loadDictionary()`. The same dictionary must then be passed to 68*5ff13fbcSAllan Jude * the decompressor, using `ZSTD_DCtx_loadDictionary()`. There are other 69*5ff13fbcSAllan Jude * more advanced functions that allow selecting some options, see zstd.h for 70*5ff13fbcSAllan Jude * complete documentation. 71*5ff13fbcSAllan Jude * 72*5ff13fbcSAllan Jude * What is a zstd dictionary? 73*5ff13fbcSAllan Jude * -------------------------- 74*5ff13fbcSAllan Jude * 75*5ff13fbcSAllan Jude * A zstd dictionary has two pieces: Its header, and its content. The header 76*5ff13fbcSAllan Jude * contains a magic number, the dictionary ID, and entropy tables. These 77*5ff13fbcSAllan Jude * entropy tables allow zstd to save on header costs in the compressed file, 78*5ff13fbcSAllan Jude * which really matters for small data. The content is just bytes, which are 79*5ff13fbcSAllan Jude * repeated content that is common across many samples. 80*5ff13fbcSAllan Jude * 81*5ff13fbcSAllan Jude * What is a raw content dictionary? 82*5ff13fbcSAllan Jude * --------------------------------- 83*5ff13fbcSAllan Jude * 84*5ff13fbcSAllan Jude * A raw content dictionary is just bytes. It doesn't have a zstd dictionary 85*5ff13fbcSAllan Jude * header, a dictionary ID, or entropy tables. Any buffer is a valid raw 86*5ff13fbcSAllan Jude * content dictionary. 87*5ff13fbcSAllan Jude * 88*5ff13fbcSAllan Jude * How do I train a dictionary? 89*5ff13fbcSAllan Jude * ---------------------------- 90*5ff13fbcSAllan Jude * 91*5ff13fbcSAllan Jude * Gather samples from your use case. These samples should be similar to each 92*5ff13fbcSAllan Jude * other. If you have several use cases, you could try to train one dictionary 93*5ff13fbcSAllan Jude * per use case. 94*5ff13fbcSAllan Jude * 95*5ff13fbcSAllan Jude * Pass those samples to `ZDICT_trainFromBuffer()` and that will train your 96*5ff13fbcSAllan Jude * dictionary. There are a few advanced versions of this function, but this 97*5ff13fbcSAllan Jude * is a great starting point. If you want to further tune your dictionary 98*5ff13fbcSAllan Jude * you could try `ZDICT_optimizeTrainFromBuffer_cover()`. If that is too slow 99*5ff13fbcSAllan Jude * you can try `ZDICT_optimizeTrainFromBuffer_fastCover()`. 100*5ff13fbcSAllan Jude * 101*5ff13fbcSAllan Jude * If the dictionary training function fails, that is likely because you 102*5ff13fbcSAllan Jude * either passed too few samples, or a dictionary would not be effective 103*5ff13fbcSAllan Jude * for your data. Look at the messages that the dictionary trainer printed, 104*5ff13fbcSAllan Jude * if it doesn't say too few samples, then a dictionary would not be effective. 105*5ff13fbcSAllan Jude * 106*5ff13fbcSAllan Jude * How large should my dictionary be? 107*5ff13fbcSAllan Jude * ---------------------------------- 108*5ff13fbcSAllan Jude * 109*5ff13fbcSAllan Jude * A reasonable dictionary size, the `dictBufferCapacity`, is about 100KB. 110*5ff13fbcSAllan Jude * The zstd CLI defaults to a 110KB dictionary. You likely don't need a 111*5ff13fbcSAllan Jude * dictionary larger than that. But, most use cases can get away with a 112*5ff13fbcSAllan Jude * smaller dictionary. The advanced dictionary builders can automatically 113*5ff13fbcSAllan Jude * shrink the dictionary for you, and select a the smallest size that 114*5ff13fbcSAllan Jude * doesn't hurt compression ratio too much. See the `shrinkDict` parameter. 115*5ff13fbcSAllan Jude * A smaller dictionary can save memory, and potentially speed up 116*5ff13fbcSAllan Jude * compression. 117*5ff13fbcSAllan Jude * 118*5ff13fbcSAllan Jude * How many samples should I provide to the dictionary builder? 119*5ff13fbcSAllan Jude * ------------------------------------------------------------ 120*5ff13fbcSAllan Jude * 121*5ff13fbcSAllan Jude * We generally recommend passing ~100x the size of the dictionary 122*5ff13fbcSAllan Jude * in samples. A few thousand should suffice. Having too few samples 123*5ff13fbcSAllan Jude * can hurt the dictionaries effectiveness. Having more samples will 124*5ff13fbcSAllan Jude * only improve the dictionaries effectiveness. But having too many 125*5ff13fbcSAllan Jude * samples can slow down the dictionary builder. 126*5ff13fbcSAllan Jude * 127*5ff13fbcSAllan Jude * How do I determine if a dictionary will be effective? 128*5ff13fbcSAllan Jude * ----------------------------------------------------- 129*5ff13fbcSAllan Jude * 130*5ff13fbcSAllan Jude * Simply train a dictionary and try it out. You can use zstd's built in 131*5ff13fbcSAllan Jude * benchmarking tool to test the dictionary effectiveness. 132*5ff13fbcSAllan Jude * 133*5ff13fbcSAllan Jude * # Benchmark levels 1-3 without a dictionary 134*5ff13fbcSAllan Jude * zstd -b1e3 -r /path/to/my/files 135*5ff13fbcSAllan Jude * # Benchmark levels 1-3 with a dictionary 136*5ff13fbcSAllan Jude * zstd -b1e3 -r /path/to/my/files -D /path/to/my/dictionary 137*5ff13fbcSAllan Jude * 138*5ff13fbcSAllan Jude * When should I retrain a dictionary? 139*5ff13fbcSAllan Jude * ----------------------------------- 140*5ff13fbcSAllan Jude * 141*5ff13fbcSAllan Jude * You should retrain a dictionary when its effectiveness drops. Dictionary 142*5ff13fbcSAllan Jude * effectiveness drops as the data you are compressing changes. Generally, we do 143*5ff13fbcSAllan Jude * expect dictionaries to "decay" over time, as your data changes, but the rate 144*5ff13fbcSAllan Jude * at which they decay depends on your use case. Internally, we regularly 145*5ff13fbcSAllan Jude * retrain dictionaries, and if the new dictionary performs significantly 146*5ff13fbcSAllan Jude * better than the old dictionary, we will ship the new dictionary. 147*5ff13fbcSAllan Jude * 148*5ff13fbcSAllan Jude * I have a raw content dictionary, how do I turn it into a zstd dictionary? 149*5ff13fbcSAllan Jude * ------------------------------------------------------------------------- 150*5ff13fbcSAllan Jude * 151*5ff13fbcSAllan Jude * If you have a raw content dictionary, e.g. by manually constructing it, or 152*5ff13fbcSAllan Jude * using a third-party dictionary builder, you can turn it into a zstd 153*5ff13fbcSAllan Jude * dictionary by using `ZDICT_finalizeDictionary()`. You'll also have to 154*5ff13fbcSAllan Jude * provide some samples of the data. It will add the zstd header to the 155*5ff13fbcSAllan Jude * raw content, which contains a dictionary ID and entropy tables, which 156*5ff13fbcSAllan Jude * will improve compression ratio, and allow zstd to write the dictionary ID 157*5ff13fbcSAllan Jude * into the frame, if you so choose. 158*5ff13fbcSAllan Jude * 159*5ff13fbcSAllan Jude * Do I have to use zstd's dictionary builder? 160*5ff13fbcSAllan Jude * ------------------------------------------- 161*5ff13fbcSAllan Jude * 162*5ff13fbcSAllan Jude * No! You can construct dictionary content however you please, it is just 163*5ff13fbcSAllan Jude * bytes. It will always be valid as a raw content dictionary. If you want 164*5ff13fbcSAllan Jude * a zstd dictionary, which can improve compression ratio, use 165*5ff13fbcSAllan Jude * `ZDICT_finalizeDictionary()`. 166*5ff13fbcSAllan Jude * 167*5ff13fbcSAllan Jude * What is the attack surface of a zstd dictionary? 168*5ff13fbcSAllan Jude * ------------------------------------------------ 169*5ff13fbcSAllan Jude * 170*5ff13fbcSAllan Jude * Zstd is heavily fuzz tested, including loading fuzzed dictionaries, so 171*5ff13fbcSAllan Jude * zstd should never crash, or access out-of-bounds memory no matter what 172*5ff13fbcSAllan Jude * the dictionary is. However, if an attacker can control the dictionary 173*5ff13fbcSAllan Jude * during decompression, they can cause zstd to generate arbitrary bytes, 174*5ff13fbcSAllan Jude * just like if they controlled the compressed data. 175*5ff13fbcSAllan Jude * 176*5ff13fbcSAllan Jude ******************************************************************************/ 177*5ff13fbcSAllan Jude 178*5ff13fbcSAllan Jude 179*5ff13fbcSAllan Jude /*! ZDICT_trainFromBuffer(): 180*5ff13fbcSAllan Jude * Train a dictionary from an array of samples. 181*5ff13fbcSAllan Jude * Redirect towards ZDICT_optimizeTrainFromBuffer_fastCover() single-threaded, with d=8, steps=4, 182*5ff13fbcSAllan Jude * f=20, and accel=1. 183*5ff13fbcSAllan Jude * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, 184*5ff13fbcSAllan Jude * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. 185*5ff13fbcSAllan Jude * The resulting dictionary will be saved into `dictBuffer`. 186*5ff13fbcSAllan Jude * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) 187*5ff13fbcSAllan Jude * or an error code, which can be tested with ZDICT_isError(). 188*5ff13fbcSAllan Jude * Note: Dictionary training will fail if there are not enough samples to construct a 189*5ff13fbcSAllan Jude * dictionary, or if most of the samples are too small (< 8 bytes being the lower limit). 190*5ff13fbcSAllan Jude * If dictionary training fails, you should use zstd without a dictionary, as the dictionary 191*5ff13fbcSAllan Jude * would've been ineffective anyways. If you believe your samples would benefit from a dictionary 192*5ff13fbcSAllan Jude * please open an issue with details, and we can look into it. 193*5ff13fbcSAllan Jude * Note: ZDICT_trainFromBuffer()'s memory usage is about 6 MB. 194*5ff13fbcSAllan Jude * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. 195*5ff13fbcSAllan Jude * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. 196*5ff13fbcSAllan Jude * In general, it's recommended to provide a few thousands samples, though this can vary a lot. 197*5ff13fbcSAllan Jude * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. 198*5ff13fbcSAllan Jude */ 199*5ff13fbcSAllan Jude ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity, 200*5ff13fbcSAllan Jude const void* samplesBuffer, 201*5ff13fbcSAllan Jude const size_t* samplesSizes, unsigned nbSamples); 202*5ff13fbcSAllan Jude 203*5ff13fbcSAllan Jude typedef struct { 204*5ff13fbcSAllan Jude int compressionLevel; /*< optimize for a specific zstd compression level; 0 means default */ 205*5ff13fbcSAllan Jude unsigned notificationLevel; /*< Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */ 206*5ff13fbcSAllan Jude unsigned dictID; /*< force dictID value; 0 means auto mode (32-bits random value) 207*5ff13fbcSAllan Jude * NOTE: The zstd format reserves some dictionary IDs for future use. 208*5ff13fbcSAllan Jude * You may use them in private settings, but be warned that they 209*5ff13fbcSAllan Jude * may be used by zstd in a public dictionary registry in the future. 210*5ff13fbcSAllan Jude * These dictionary IDs are: 211*5ff13fbcSAllan Jude * - low range : <= 32767 212*5ff13fbcSAllan Jude * - high range : >= (2^31) 213*5ff13fbcSAllan Jude */ 214*5ff13fbcSAllan Jude } ZDICT_params_t; 215*5ff13fbcSAllan Jude 216*5ff13fbcSAllan Jude /*! ZDICT_finalizeDictionary(): 217*5ff13fbcSAllan Jude * Given a custom content as a basis for dictionary, and a set of samples, 218*5ff13fbcSAllan Jude * finalize dictionary by adding headers and statistics according to the zstd 219*5ff13fbcSAllan Jude * dictionary format. 220*5ff13fbcSAllan Jude * 221*5ff13fbcSAllan Jude * Samples must be stored concatenated in a flat buffer `samplesBuffer`, 222*5ff13fbcSAllan Jude * supplied with an array of sizes `samplesSizes`, providing the size of each 223*5ff13fbcSAllan Jude * sample in order. The samples are used to construct the statistics, so they 224*5ff13fbcSAllan Jude * should be representative of what you will compress with this dictionary. 225*5ff13fbcSAllan Jude * 226*5ff13fbcSAllan Jude * The compression level can be set in `parameters`. You should pass the 227*5ff13fbcSAllan Jude * compression level you expect to use in production. The statistics for each 228*5ff13fbcSAllan Jude * compression level differ, so tuning the dictionary for the compression level 229*5ff13fbcSAllan Jude * can help quite a bit. 230*5ff13fbcSAllan Jude * 231*5ff13fbcSAllan Jude * You can set an explicit dictionary ID in `parameters`, or allow us to pick 232*5ff13fbcSAllan Jude * a random dictionary ID for you, but we can't guarantee no collisions. 233*5ff13fbcSAllan Jude * 234*5ff13fbcSAllan Jude * The dstDictBuffer and the dictContent may overlap, and the content will be 235*5ff13fbcSAllan Jude * appended to the end of the header. If the header + the content doesn't fit in 236*5ff13fbcSAllan Jude * maxDictSize the beginning of the content is truncated to make room, since it 237*5ff13fbcSAllan Jude * is presumed that the most profitable content is at the end of the dictionary, 238*5ff13fbcSAllan Jude * since that is the cheapest to reference. 239*5ff13fbcSAllan Jude * 240*5ff13fbcSAllan Jude * `maxDictSize` must be >= max(dictContentSize, ZSTD_DICTSIZE_MIN). 241*5ff13fbcSAllan Jude * 242*5ff13fbcSAllan Jude * @return: size of dictionary stored into `dstDictBuffer` (<= `maxDictSize`), 243*5ff13fbcSAllan Jude * or an error code, which can be tested by ZDICT_isError(). 244*5ff13fbcSAllan Jude * Note: ZDICT_finalizeDictionary() will push notifications into stderr if 245*5ff13fbcSAllan Jude * instructed to, using notificationLevel>0. 246*5ff13fbcSAllan Jude * NOTE: This function currently may fail in several edge cases including: 247*5ff13fbcSAllan Jude * * Not enough samples 248*5ff13fbcSAllan Jude * * Samples are uncompressible 249*5ff13fbcSAllan Jude * * Samples are all exactly the same 250*5ff13fbcSAllan Jude */ 251*5ff13fbcSAllan Jude ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dstDictBuffer, size_t maxDictSize, 252*5ff13fbcSAllan Jude const void* dictContent, size_t dictContentSize, 253*5ff13fbcSAllan Jude const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, 254*5ff13fbcSAllan Jude ZDICT_params_t parameters); 255*5ff13fbcSAllan Jude 256*5ff13fbcSAllan Jude 257*5ff13fbcSAllan Jude /*====== Helper functions ======*/ 258*5ff13fbcSAllan Jude ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize); /**< extracts dictID; @return zero if error (not a valid dictionary) */ 259*5ff13fbcSAllan Jude ZDICTLIB_API size_t ZDICT_getDictHeaderSize(const void* dictBuffer, size_t dictSize); /* returns dict header size; returns a ZSTD error code on failure */ 260*5ff13fbcSAllan Jude ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode); 261*5ff13fbcSAllan Jude ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode); 262*5ff13fbcSAllan Jude 263*5ff13fbcSAllan Jude 264*5ff13fbcSAllan Jude 265*5ff13fbcSAllan Jude #ifdef ZDICT_STATIC_LINKING_ONLY 266*5ff13fbcSAllan Jude 267*5ff13fbcSAllan Jude /* ==================================================================================== 268*5ff13fbcSAllan Jude * The definitions in this section are considered experimental. 269*5ff13fbcSAllan Jude * They should never be used with a dynamic library, as they may change in the future. 270*5ff13fbcSAllan Jude * They are provided for advanced usages. 271*5ff13fbcSAllan Jude * Use them only in association with static linking. 272*5ff13fbcSAllan Jude * ==================================================================================== */ 273*5ff13fbcSAllan Jude 274*5ff13fbcSAllan Jude #define ZDICT_DICTSIZE_MIN 256 275*5ff13fbcSAllan Jude /* Deprecated: Remove in v1.6.0 */ 276*5ff13fbcSAllan Jude #define ZDICT_CONTENTSIZE_MIN 128 277*5ff13fbcSAllan Jude 278*5ff13fbcSAllan Jude /*! ZDICT_cover_params_t: 279*5ff13fbcSAllan Jude * k and d are the only required parameters. 280*5ff13fbcSAllan Jude * For others, value 0 means default. 281*5ff13fbcSAllan Jude */ 282*5ff13fbcSAllan Jude typedef struct { 283*5ff13fbcSAllan Jude unsigned k; /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */ 284*5ff13fbcSAllan Jude unsigned d; /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */ 285*5ff13fbcSAllan Jude unsigned steps; /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */ 286*5ff13fbcSAllan Jude unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */ 287*5ff13fbcSAllan Jude double splitPoint; /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (1.0), 1.0 when all samples are used for both training and testing */ 288*5ff13fbcSAllan Jude unsigned shrinkDict; /* Train dictionaries to shrink in size starting from the minimum size and selects the smallest dictionary that is shrinkDictMaxRegression% worse than the largest dictionary. 0 means no shrinking and 1 means shrinking */ 289*5ff13fbcSAllan Jude unsigned shrinkDictMaxRegression; /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */ 290*5ff13fbcSAllan Jude ZDICT_params_t zParams; 291*5ff13fbcSAllan Jude } ZDICT_cover_params_t; 292*5ff13fbcSAllan Jude 293*5ff13fbcSAllan Jude typedef struct { 294*5ff13fbcSAllan Jude unsigned k; /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */ 295*5ff13fbcSAllan Jude unsigned d; /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */ 296*5ff13fbcSAllan Jude unsigned f; /* log of size of frequency array : constraint: 0 < f <= 31 : 1 means default(20)*/ 297*5ff13fbcSAllan Jude unsigned steps; /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */ 298*5ff13fbcSAllan Jude unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */ 299*5ff13fbcSAllan Jude double splitPoint; /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (0.75), 1.0 when all samples are used for both training and testing */ 300*5ff13fbcSAllan Jude unsigned accel; /* Acceleration level: constraint: 0 < accel <= 10, higher means faster and less accurate, 0 means default(1) */ 301*5ff13fbcSAllan Jude unsigned shrinkDict; /* Train dictionaries to shrink in size starting from the minimum size and selects the smallest dictionary that is shrinkDictMaxRegression% worse than the largest dictionary. 0 means no shrinking and 1 means shrinking */ 302*5ff13fbcSAllan Jude unsigned shrinkDictMaxRegression; /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */ 303*5ff13fbcSAllan Jude 304*5ff13fbcSAllan Jude ZDICT_params_t zParams; 305*5ff13fbcSAllan Jude } ZDICT_fastCover_params_t; 306*5ff13fbcSAllan Jude 307*5ff13fbcSAllan Jude /*! ZDICT_trainFromBuffer_cover(): 308*5ff13fbcSAllan Jude * Train a dictionary from an array of samples using the COVER algorithm. 309*5ff13fbcSAllan Jude * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, 310*5ff13fbcSAllan Jude * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. 311*5ff13fbcSAllan Jude * The resulting dictionary will be saved into `dictBuffer`. 312*5ff13fbcSAllan Jude * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) 313*5ff13fbcSAllan Jude * or an error code, which can be tested with ZDICT_isError(). 314*5ff13fbcSAllan Jude * See ZDICT_trainFromBuffer() for details on failure modes. 315*5ff13fbcSAllan Jude * Note: ZDICT_trainFromBuffer_cover() requires about 9 bytes of memory for each input byte. 316*5ff13fbcSAllan Jude * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. 317*5ff13fbcSAllan Jude * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. 318*5ff13fbcSAllan Jude * In general, it's recommended to provide a few thousands samples, though this can vary a lot. 319*5ff13fbcSAllan Jude * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. 320*5ff13fbcSAllan Jude */ 321*5ff13fbcSAllan Jude ZDICTLIB_API size_t ZDICT_trainFromBuffer_cover( 322*5ff13fbcSAllan Jude void *dictBuffer, size_t dictBufferCapacity, 323*5ff13fbcSAllan Jude const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples, 324*5ff13fbcSAllan Jude ZDICT_cover_params_t parameters); 325*5ff13fbcSAllan Jude 326*5ff13fbcSAllan Jude /*! ZDICT_optimizeTrainFromBuffer_cover(): 327*5ff13fbcSAllan Jude * The same requirements as above hold for all the parameters except `parameters`. 328*5ff13fbcSAllan Jude * This function tries many parameter combinations and picks the best parameters. 329*5ff13fbcSAllan Jude * `*parameters` is filled with the best parameters found, 330*5ff13fbcSAllan Jude * dictionary constructed with those parameters is stored in `dictBuffer`. 331*5ff13fbcSAllan Jude * 332*5ff13fbcSAllan Jude * All of the parameters d, k, steps are optional. 333*5ff13fbcSAllan Jude * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}. 334*5ff13fbcSAllan Jude * if steps is zero it defaults to its default value. 335*5ff13fbcSAllan Jude * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000]. 336*5ff13fbcSAllan Jude * 337*5ff13fbcSAllan Jude * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) 338*5ff13fbcSAllan Jude * or an error code, which can be tested with ZDICT_isError(). 339*5ff13fbcSAllan Jude * On success `*parameters` contains the parameters selected. 340*5ff13fbcSAllan Jude * See ZDICT_trainFromBuffer() for details on failure modes. 341*5ff13fbcSAllan Jude * Note: ZDICT_optimizeTrainFromBuffer_cover() requires about 8 bytes of memory for each input byte and additionally another 5 bytes of memory for each byte of memory for each thread. 342*5ff13fbcSAllan Jude */ 343*5ff13fbcSAllan Jude ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover( 344*5ff13fbcSAllan Jude void* dictBuffer, size_t dictBufferCapacity, 345*5ff13fbcSAllan Jude const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, 346*5ff13fbcSAllan Jude ZDICT_cover_params_t* parameters); 347*5ff13fbcSAllan Jude 348*5ff13fbcSAllan Jude /*! ZDICT_trainFromBuffer_fastCover(): 349*5ff13fbcSAllan Jude * Train a dictionary from an array of samples using a modified version of COVER algorithm. 350*5ff13fbcSAllan Jude * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, 351*5ff13fbcSAllan Jude * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. 352*5ff13fbcSAllan Jude * d and k are required. 353*5ff13fbcSAllan Jude * All other parameters are optional, will use default values if not provided 354*5ff13fbcSAllan Jude * The resulting dictionary will be saved into `dictBuffer`. 355*5ff13fbcSAllan Jude * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) 356*5ff13fbcSAllan Jude * or an error code, which can be tested with ZDICT_isError(). 357*5ff13fbcSAllan Jude * See ZDICT_trainFromBuffer() for details on failure modes. 358*5ff13fbcSAllan Jude * Note: ZDICT_trainFromBuffer_fastCover() requires 6 * 2^f bytes of memory. 359*5ff13fbcSAllan Jude * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. 360*5ff13fbcSAllan Jude * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. 361*5ff13fbcSAllan Jude * In general, it's recommended to provide a few thousands samples, though this can vary a lot. 362*5ff13fbcSAllan Jude * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. 363*5ff13fbcSAllan Jude */ 364*5ff13fbcSAllan Jude ZDICTLIB_API size_t ZDICT_trainFromBuffer_fastCover(void *dictBuffer, 365*5ff13fbcSAllan Jude size_t dictBufferCapacity, const void *samplesBuffer, 366*5ff13fbcSAllan Jude const size_t *samplesSizes, unsigned nbSamples, 367*5ff13fbcSAllan Jude ZDICT_fastCover_params_t parameters); 368*5ff13fbcSAllan Jude 369*5ff13fbcSAllan Jude /*! ZDICT_optimizeTrainFromBuffer_fastCover(): 370*5ff13fbcSAllan Jude * The same requirements as above hold for all the parameters except `parameters`. 371*5ff13fbcSAllan Jude * This function tries many parameter combinations (specifically, k and d combinations) 372*5ff13fbcSAllan Jude * and picks the best parameters. `*parameters` is filled with the best parameters found, 373*5ff13fbcSAllan Jude * dictionary constructed with those parameters is stored in `dictBuffer`. 374*5ff13fbcSAllan Jude * All of the parameters d, k, steps, f, and accel are optional. 375*5ff13fbcSAllan Jude * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}. 376*5ff13fbcSAllan Jude * if steps is zero it defaults to its default value. 377*5ff13fbcSAllan Jude * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000]. 378*5ff13fbcSAllan Jude * If f is zero, default value of 20 is used. 379*5ff13fbcSAllan Jude * If accel is zero, default value of 1 is used. 380*5ff13fbcSAllan Jude * 381*5ff13fbcSAllan Jude * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) 382*5ff13fbcSAllan Jude * or an error code, which can be tested with ZDICT_isError(). 383*5ff13fbcSAllan Jude * On success `*parameters` contains the parameters selected. 384*5ff13fbcSAllan Jude * See ZDICT_trainFromBuffer() for details on failure modes. 385*5ff13fbcSAllan Jude * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 6 * 2^f bytes of memory for each thread. 386*5ff13fbcSAllan Jude */ 387*5ff13fbcSAllan Jude ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_fastCover(void* dictBuffer, 388*5ff13fbcSAllan Jude size_t dictBufferCapacity, const void* samplesBuffer, 389*5ff13fbcSAllan Jude const size_t* samplesSizes, unsigned nbSamples, 390*5ff13fbcSAllan Jude ZDICT_fastCover_params_t* parameters); 391*5ff13fbcSAllan Jude 392*5ff13fbcSAllan Jude typedef struct { 393*5ff13fbcSAllan Jude unsigned selectivityLevel; /* 0 means default; larger => select more => larger dictionary */ 394*5ff13fbcSAllan Jude ZDICT_params_t zParams; 395*5ff13fbcSAllan Jude } ZDICT_legacy_params_t; 396*5ff13fbcSAllan Jude 397*5ff13fbcSAllan Jude /*! ZDICT_trainFromBuffer_legacy(): 398*5ff13fbcSAllan Jude * Train a dictionary from an array of samples. 399*5ff13fbcSAllan Jude * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, 400*5ff13fbcSAllan Jude * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. 401*5ff13fbcSAllan Jude * The resulting dictionary will be saved into `dictBuffer`. 402*5ff13fbcSAllan Jude * `parameters` is optional and can be provided with values set to 0 to mean "default". 403*5ff13fbcSAllan Jude * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) 404*5ff13fbcSAllan Jude * or an error code, which can be tested with ZDICT_isError(). 405*5ff13fbcSAllan Jude * See ZDICT_trainFromBuffer() for details on failure modes. 406*5ff13fbcSAllan Jude * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. 407*5ff13fbcSAllan Jude * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. 408*5ff13fbcSAllan Jude * In general, it's recommended to provide a few thousands samples, though this can vary a lot. 409*5ff13fbcSAllan Jude * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. 410*5ff13fbcSAllan Jude * Note: ZDICT_trainFromBuffer_legacy() will send notifications into stderr if instructed to, using notificationLevel>0. 411*5ff13fbcSAllan Jude */ 412*5ff13fbcSAllan Jude ZDICTLIB_API size_t ZDICT_trainFromBuffer_legacy( 413*5ff13fbcSAllan Jude void* dictBuffer, size_t dictBufferCapacity, 414*5ff13fbcSAllan Jude const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, 415*5ff13fbcSAllan Jude ZDICT_legacy_params_t parameters); 416*5ff13fbcSAllan Jude 417*5ff13fbcSAllan Jude 418*5ff13fbcSAllan Jude /* Deprecation warnings */ 419*5ff13fbcSAllan Jude /* It is generally possible to disable deprecation warnings from compiler, 420*5ff13fbcSAllan Jude for example with -Wno-deprecated-declarations for gcc 421*5ff13fbcSAllan Jude or _CRT_SECURE_NO_WARNINGS in Visual. 422*5ff13fbcSAllan Jude Otherwise, it's also possible to manually define ZDICT_DISABLE_DEPRECATE_WARNINGS */ 423*5ff13fbcSAllan Jude #ifdef ZDICT_DISABLE_DEPRECATE_WARNINGS 424*5ff13fbcSAllan Jude # define ZDICT_DEPRECATED(message) ZDICTLIB_API /* disable deprecation warnings */ 425*5ff13fbcSAllan Jude #else 426*5ff13fbcSAllan Jude # define ZDICT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) 427*5ff13fbcSAllan Jude # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ 428*5ff13fbcSAllan Jude # define ZDICT_DEPRECATED(message) [[deprecated(message)]] ZDICTLIB_API 429*5ff13fbcSAllan Jude # elif defined(__clang__) || (ZDICT_GCC_VERSION >= 405) 430*5ff13fbcSAllan Jude # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated(message))) 431*5ff13fbcSAllan Jude # elif (ZDICT_GCC_VERSION >= 301) 432*5ff13fbcSAllan Jude # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated)) 433*5ff13fbcSAllan Jude # elif defined(_MSC_VER) 434*5ff13fbcSAllan Jude # define ZDICT_DEPRECATED(message) ZDICTLIB_API __declspec(deprecated(message)) 435*5ff13fbcSAllan Jude # else 436*5ff13fbcSAllan Jude # pragma message("WARNING: You need to implement ZDICT_DEPRECATED for this compiler") 437*5ff13fbcSAllan Jude # define ZDICT_DEPRECATED(message) ZDICTLIB_API 438*5ff13fbcSAllan Jude # endif 439*5ff13fbcSAllan Jude #endif /* ZDICT_DISABLE_DEPRECATE_WARNINGS */ 440*5ff13fbcSAllan Jude 441*5ff13fbcSAllan Jude ZDICT_DEPRECATED("use ZDICT_finalizeDictionary() instead") 442*5ff13fbcSAllan Jude size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity, 443*5ff13fbcSAllan Jude const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples); 444*5ff13fbcSAllan Jude 445*5ff13fbcSAllan Jude 446*5ff13fbcSAllan Jude #endif /* ZDICT_STATIC_LINKING_ONLY */ 447*5ff13fbcSAllan Jude 448*5ff13fbcSAllan Jude #if defined (__cplusplus) 449*5ff13fbcSAllan Jude } 450*5ff13fbcSAllan Jude #endif 451*5ff13fbcSAllan Jude 452*5ff13fbcSAllan Jude #endif /* DICTBUILDER_H_001 */ 453