]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | /* |
f67539c2 | 2 | * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. |
7c673cae FG |
3 | * All rights reserved. |
4 | * | |
11fdf7f2 TL |
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. | |
7c673cae FG |
9 | */ |
10 | ||
11 | #ifndef DICTBUILDER_H_001 | |
12 | #define DICTBUILDER_H_001 | |
13 | ||
14 | #if defined (__cplusplus) | |
15 | extern "C" { | |
16 | #endif | |
17 | ||
18 | ||
19 | /*====== Dependencies ======*/ | |
20 | #include <stddef.h> /* size_t */ | |
21 | ||
22 | ||
11fdf7f2 TL |
23 | /* ===== ZDICTLIB_API : control library symbols visibility ===== */ |
24 | #ifndef ZDICTLIB_VISIBILITY | |
25 | # if defined(__GNUC__) && (__GNUC__ >= 4) | |
26 | # define ZDICTLIB_VISIBILITY __attribute__ ((visibility ("default"))) | |
27 | # else | |
28 | # define ZDICTLIB_VISIBILITY | |
29 | # endif | |
30 | #endif | |
31 | #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) | |
32 | # define ZDICTLIB_API __declspec(dllexport) ZDICTLIB_VISIBILITY | |
33 | #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) | |
34 | # 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.*/ | |
7c673cae | 35 | #else |
11fdf7f2 | 36 | # define ZDICTLIB_API ZDICTLIB_VISIBILITY |
7c673cae FG |
37 | #endif |
38 | ||
39 | ||
11fdf7f2 | 40 | /*! ZDICT_trainFromBuffer(): |
9f95a23c TL |
41 | * Train a dictionary from an array of samples. |
42 | * Redirect towards ZDICT_optimizeTrainFromBuffer_fastCover() single-threaded, with d=8, steps=4, | |
43 | * f=20, and accel=1. | |
44 | * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, | |
45 | * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. | |
46 | * The resulting dictionary will be saved into `dictBuffer`. | |
11fdf7f2 | 47 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
9f95a23c TL |
48 | * or an error code, which can be tested with ZDICT_isError(). |
49 | * Note: Dictionary training will fail if there are not enough samples to construct a | |
50 | * dictionary, or if most of the samples are too small (< 8 bytes being the lower limit). | |
51 | * If dictionary training fails, you should use zstd without a dictionary, as the dictionary | |
52 | * would've been ineffective anyways. If you believe your samples would benefit from a dictionary | |
53 | * please open an issue with details, and we can look into it. | |
54 | * Note: ZDICT_trainFromBuffer()'s memory usage is about 6 MB. | |
55 | * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. | |
56 | * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. | |
57 | * In general, it's recommended to provide a few thousands samples, though this can vary a lot. | |
11fdf7f2 TL |
58 | * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. |
59 | */ | |
7c673cae | 60 | ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity, |
9f95a23c TL |
61 | const void* samplesBuffer, |
62 | const size_t* samplesSizes, unsigned nbSamples); | |
7c673cae | 63 | |
f67539c2 TL |
64 | typedef struct { |
65 | int compressionLevel; /*< optimize for a specific zstd compression level; 0 means default */ | |
66 | unsigned notificationLevel; /*< Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */ | |
67 | unsigned dictID; /*< force dictID value; 0 means auto mode (32-bits random value) */ | |
68 | } ZDICT_params_t; | |
69 | ||
70 | /*! ZDICT_finalizeDictionary(): | |
71 | * Given a custom content as a basis for dictionary, and a set of samples, | |
72 | * finalize dictionary by adding headers and statistics according to the zstd | |
73 | * dictionary format. | |
74 | * | |
75 | * Samples must be stored concatenated in a flat buffer `samplesBuffer`, | |
76 | * supplied with an array of sizes `samplesSizes`, providing the size of each | |
77 | * sample in order. The samples are used to construct the statistics, so they | |
78 | * should be representative of what you will compress with this dictionary. | |
79 | * | |
80 | * The compression level can be set in `parameters`. You should pass the | |
81 | * compression level you expect to use in production. The statistics for each | |
82 | * compression level differ, so tuning the dictionary for the compression level | |
83 | * can help quite a bit. | |
84 | * | |
85 | * You can set an explicit dictionary ID in `parameters`, or allow us to pick | |
86 | * a random dictionary ID for you, but we can't guarantee no collisions. | |
87 | * | |
88 | * The dstDictBuffer and the dictContent may overlap, and the content will be | |
89 | * appended to the end of the header. If the header + the content doesn't fit in | |
90 | * maxDictSize the beginning of the content is truncated to make room, since it | |
91 | * is presumed that the most profitable content is at the end of the dictionary, | |
92 | * since that is the cheapest to reference. | |
93 | * | |
94 | * `dictContentSize` must be >= ZDICT_CONTENTSIZE_MIN bytes. | |
95 | * `maxDictSize` must be >= max(dictContentSize, ZSTD_DICTSIZE_MIN). | |
96 | * | |
97 | * @return: size of dictionary stored into `dstDictBuffer` (<= `maxDictSize`), | |
98 | * or an error code, which can be tested by ZDICT_isError(). | |
99 | * Note: ZDICT_finalizeDictionary() will push notifications into stderr if | |
100 | * instructed to, using notificationLevel>0. | |
101 | * NOTE: This function currently may fail in several edge cases including: | |
102 | * * Not enough samples | |
103 | * * Samples are uncompressible | |
104 | * * Samples are all exactly the same | |
105 | */ | |
106 | ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dstDictBuffer, size_t maxDictSize, | |
107 | const void* dictContent, size_t dictContentSize, | |
108 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, | |
109 | ZDICT_params_t parameters); | |
110 | ||
7c673cae FG |
111 | |
112 | /*====== Helper functions ======*/ | |
113 | ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize); /**< extracts dictID; @return zero if error (not a valid dictionary) */ | |
f67539c2 | 114 | ZDICTLIB_API size_t ZDICT_getDictHeaderSize(const void* dictBuffer, size_t dictSize); /* returns dict header size; returns a ZSTD error code on failure */ |
7c673cae FG |
115 | ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode); |
116 | ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode); | |
117 | ||
118 | ||
119 | ||
120 | #ifdef ZDICT_STATIC_LINKING_ONLY | |
121 | ||
122 | /* ==================================================================================== | |
123 | * The definitions in this section are considered experimental. | |
124 | * They should never be used with a dynamic library, as they may change in the future. | |
125 | * They are provided for advanced usages. | |
126 | * Use them only in association with static linking. | |
127 | * ==================================================================================== */ | |
128 | ||
f67539c2 TL |
129 | #define ZDICT_CONTENTSIZE_MIN 128 |
130 | #define ZDICT_DICTSIZE_MIN 256 | |
7c673cae | 131 | |
11fdf7f2 | 132 | /*! ZDICT_cover_params_t: |
11fdf7f2 | 133 | * k and d are the only required parameters. |
9f95a23c | 134 | * For others, value 0 means default. |
11fdf7f2 TL |
135 | */ |
136 | typedef struct { | |
137 | unsigned k; /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */ | |
138 | unsigned d; /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */ | |
9f95a23c | 139 | unsigned steps; /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */ |
11fdf7f2 | 140 | unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */ |
9f95a23c | 141 | 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 */ |
f67539c2 TL |
142 | 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 */ |
143 | unsigned shrinkDictMaxRegression; /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */ | |
11fdf7f2 TL |
144 | ZDICT_params_t zParams; |
145 | } ZDICT_cover_params_t; | |
146 | ||
9f95a23c TL |
147 | typedef struct { |
148 | unsigned k; /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */ | |
149 | unsigned d; /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */ | |
150 | unsigned f; /* log of size of frequency array : constraint: 0 < f <= 31 : 1 means default(20)*/ | |
151 | unsigned steps; /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */ | |
152 | unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */ | |
153 | 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 */ | |
154 | unsigned accel; /* Acceleration level: constraint: 0 < accel <= 10, higher means faster and less accurate, 0 means default(1) */ | |
f67539c2 TL |
155 | 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 */ |
156 | unsigned shrinkDictMaxRegression; /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */ | |
157 | ||
9f95a23c TL |
158 | ZDICT_params_t zParams; |
159 | } ZDICT_fastCover_params_t; | |
11fdf7f2 TL |
160 | |
161 | /*! ZDICT_trainFromBuffer_cover(): | |
9f95a23c TL |
162 | * Train a dictionary from an array of samples using the COVER algorithm. |
163 | * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, | |
164 | * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. | |
165 | * The resulting dictionary will be saved into `dictBuffer`. | |
11fdf7f2 | 166 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
9f95a23c TL |
167 | * or an error code, which can be tested with ZDICT_isError(). |
168 | * See ZDICT_trainFromBuffer() for details on failure modes. | |
169 | * Note: ZDICT_trainFromBuffer_cover() requires about 9 bytes of memory for each input byte. | |
170 | * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. | |
171 | * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. | |
172 | * In general, it's recommended to provide a few thousands samples, though this can vary a lot. | |
11fdf7f2 TL |
173 | * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. |
174 | */ | |
175 | ZDICTLIB_API size_t ZDICT_trainFromBuffer_cover( | |
9f95a23c TL |
176 | void *dictBuffer, size_t dictBufferCapacity, |
177 | const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples, | |
178 | ZDICT_cover_params_t parameters); | |
11fdf7f2 TL |
179 | |
180 | /*! ZDICT_optimizeTrainFromBuffer_cover(): | |
181 | * The same requirements as above hold for all the parameters except `parameters`. | |
182 | * This function tries many parameter combinations and picks the best parameters. | |
9f95a23c TL |
183 | * `*parameters` is filled with the best parameters found, |
184 | * dictionary constructed with those parameters is stored in `dictBuffer`. | |
11fdf7f2 TL |
185 | * |
186 | * All of the parameters d, k, steps are optional. | |
9f95a23c | 187 | * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}. |
11fdf7f2 | 188 | * if steps is zero it defaults to its default value. |
9f95a23c | 189 | * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000]. |
11fdf7f2 TL |
190 | * |
191 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) | |
9f95a23c TL |
192 | * or an error code, which can be tested with ZDICT_isError(). |
193 | * On success `*parameters` contains the parameters selected. | |
194 | * See ZDICT_trainFromBuffer() for details on failure modes. | |
11fdf7f2 TL |
195 | * 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. |
196 | */ | |
197 | ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover( | |
9f95a23c TL |
198 | void* dictBuffer, size_t dictBufferCapacity, |
199 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, | |
200 | ZDICT_cover_params_t* parameters); | |
201 | ||
202 | /*! ZDICT_trainFromBuffer_fastCover(): | |
203 | * Train a dictionary from an array of samples using a modified version of COVER algorithm. | |
204 | * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, | |
205 | * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. | |
206 | * d and k are required. | |
207 | * All other parameters are optional, will use default values if not provided | |
208 | * The resulting dictionary will be saved into `dictBuffer`. | |
209 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) | |
210 | * or an error code, which can be tested with ZDICT_isError(). | |
211 | * See ZDICT_trainFromBuffer() for details on failure modes. | |
212 | * Note: ZDICT_trainFromBuffer_fastCover() requires 6 * 2^f bytes of memory. | |
213 | * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. | |
214 | * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. | |
215 | * In general, it's recommended to provide a few thousands samples, though this can vary a lot. | |
216 | * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. | |
217 | */ | |
218 | ZDICTLIB_API size_t ZDICT_trainFromBuffer_fastCover(void *dictBuffer, | |
219 | size_t dictBufferCapacity, const void *samplesBuffer, | |
220 | const size_t *samplesSizes, unsigned nbSamples, | |
221 | ZDICT_fastCover_params_t parameters); | |
222 | ||
223 | /*! ZDICT_optimizeTrainFromBuffer_fastCover(): | |
224 | * The same requirements as above hold for all the parameters except `parameters`. | |
225 | * This function tries many parameter combinations (specifically, k and d combinations) | |
226 | * and picks the best parameters. `*parameters` is filled with the best parameters found, | |
227 | * dictionary constructed with those parameters is stored in `dictBuffer`. | |
228 | * All of the parameters d, k, steps, f, and accel are optional. | |
229 | * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}. | |
230 | * if steps is zero it defaults to its default value. | |
231 | * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000]. | |
232 | * If f is zero, default value of 20 is used. | |
233 | * If accel is zero, default value of 1 is used. | |
234 | * | |
235 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) | |
236 | * or an error code, which can be tested with ZDICT_isError(). | |
237 | * On success `*parameters` contains the parameters selected. | |
238 | * See ZDICT_trainFromBuffer() for details on failure modes. | |
239 | * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 6 * 2^f bytes of memory for each thread. | |
240 | */ | |
241 | ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_fastCover(void* dictBuffer, | |
242 | size_t dictBufferCapacity, const void* samplesBuffer, | |
243 | const size_t* samplesSizes, unsigned nbSamples, | |
244 | ZDICT_fastCover_params_t* parameters); | |
11fdf7f2 | 245 | |
11fdf7f2 TL |
246 | typedef struct { |
247 | unsigned selectivityLevel; /* 0 means default; larger => select more => larger dictionary */ | |
248 | ZDICT_params_t zParams; | |
249 | } ZDICT_legacy_params_t; | |
250 | ||
251 | /*! ZDICT_trainFromBuffer_legacy(): | |
9f95a23c TL |
252 | * Train a dictionary from an array of samples. |
253 | * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, | |
254 | * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. | |
255 | * The resulting dictionary will be saved into `dictBuffer`. | |
11fdf7f2 TL |
256 | * `parameters` is optional and can be provided with values set to 0 to mean "default". |
257 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) | |
9f95a23c TL |
258 | * or an error code, which can be tested with ZDICT_isError(). |
259 | * See ZDICT_trainFromBuffer() for details on failure modes. | |
260 | * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. | |
261 | * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. | |
262 | * In general, it's recommended to provide a few thousands samples, though this can vary a lot. | |
11fdf7f2 | 263 | * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. |
9f95a23c | 264 | * Note: ZDICT_trainFromBuffer_legacy() will send notifications into stderr if instructed to, using notificationLevel>0. |
11fdf7f2 TL |
265 | */ |
266 | ZDICTLIB_API size_t ZDICT_trainFromBuffer_legacy( | |
9f95a23c TL |
267 | void *dictBuffer, size_t dictBufferCapacity, |
268 | const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples, | |
269 | ZDICT_legacy_params_t parameters); | |
11fdf7f2 TL |
270 | |
271 | /* Deprecation warnings */ | |
272 | /* It is generally possible to disable deprecation warnings from compiler, | |
273 | for example with -Wno-deprecated-declarations for gcc | |
274 | or _CRT_SECURE_NO_WARNINGS in Visual. | |
275 | Otherwise, it's also possible to manually define ZDICT_DISABLE_DEPRECATE_WARNINGS */ | |
276 | #ifdef ZDICT_DISABLE_DEPRECATE_WARNINGS | |
277 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API /* disable deprecation warnings */ | |
278 | #else | |
279 | # define ZDICT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) | |
280 | # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ | |
281 | # define ZDICT_DEPRECATED(message) [[deprecated(message)]] ZDICTLIB_API | |
282 | # elif (ZDICT_GCC_VERSION >= 405) || defined(__clang__) | |
283 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated(message))) | |
284 | # elif (ZDICT_GCC_VERSION >= 301) | |
285 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated)) | |
286 | # elif defined(_MSC_VER) | |
287 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API __declspec(deprecated(message)) | |
288 | # else | |
289 | # pragma message("WARNING: You need to implement ZDICT_DEPRECATED for this compiler") | |
290 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API | |
291 | # endif | |
292 | #endif /* ZDICT_DISABLE_DEPRECATE_WARNINGS */ | |
293 | ||
294 | ZDICT_DEPRECATED("use ZDICT_finalizeDictionary() instead") | |
7c673cae | 295 | size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity, |
11fdf7f2 | 296 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples); |
7c673cae FG |
297 | |
298 | ||
299 | #endif /* ZDICT_STATIC_LINKING_ONLY */ | |
300 | ||
301 | #if defined (__cplusplus) | |
302 | } | |
303 | #endif | |
304 | ||
305 | #endif /* DICTBUILDER_H_001 */ |