| 1 |
# Dictionary builder |
| 2 |
|
| 3 |
This is an *experimental* dictionary builder for Zstandard, S2, LZ4, deflate and more. |
| 4 |
|
| 5 |
This diverges from the Zstandard dictionary builder, and may have some failure scenarios for very small or uniform inputs. |
| 6 |
|
| 7 |
Dictionaries returned should all be valid, but if very little data is supplied, it may not be able to generate a dictionary. |
| 8 |
|
| 9 |
With a large, diverse sample set, it will generate a dictionary that can compete with the Zstandard dictionary builder, |
| 10 |
but for very similar data it will not be able to generate a dictionary that is as good. |
| 11 |
|
| 12 |
Feedback is welcome. |
| 13 |
|
| 14 |
## Usage |
| 15 |
|
| 16 |
First of all a collection of *samples* must be collected. |
| 17 |
|
| 18 |
These samples should be representative of the input data and should not contain any complete duplicates. |
| 19 |
|
| 20 |
Only the *beginning* of the samples is important, the rest can be truncated. |
| 21 |
Beyond something like 64KB the input is not important anymore. |
| 22 |
The commandline tool can do this truncation for you. |
| 23 |
|
| 24 |
## Command line |
| 25 |
|
| 26 |
To install the command line tool run: |
| 27 |
|
| 28 |
``` |
| 29 |
$ go install github.com/klauspost/compress/dict/cmd/builddict@latest |
| 30 |
``` |
| 31 |
|
| 32 |
Collect the samples in a directory, for example `samples/`. |
| 33 |
|
| 34 |
Then run the command line tool. Basic usage is just to pass the directory with the samples: |
| 35 |
|
| 36 |
``` |
| 37 |
$ builddict samples/ |
| 38 |
``` |
| 39 |
|
| 40 |
This will build a Zstandard dictionary and write it to `dictionary.bin` in the current folder. |
| 41 |
|
| 42 |
The dictionary can be used with the Zstandard command line tool: |
| 43 |
|
| 44 |
``` |
| 45 |
$ zstd -D dictionary.bin input |
| 46 |
``` |
| 47 |
|
| 48 |
### Options |
| 49 |
|
| 50 |
The command line tool has a few options: |
| 51 |
|
| 52 |
- `-format`. Output type. "zstd" "s2" or "raw". Default "zstd". |
| 53 |
|
| 54 |
Output a dictionary in Zstandard format, S2 format or raw bytes. |
| 55 |
The raw bytes can be used with Deflate, LZ4, etc. |
| 56 |
|
| 57 |
- `-hash` Hash bytes match length. Minimum match length. Must be 4-8 (inclusive) Default 6. |
| 58 |
|
| 59 |
The hash bytes are used to define the shortest matches to look for. |
| 60 |
Shorter matches can generate a more fractured dictionary with less compression, but can for certain inputs be better. |
| 61 |
Usually lengths around 6-8 are best. |
| 62 |
|
| 63 |
- `-len` Specify custom output size. Default 114688. |
| 64 |
- `-max` Max input length to index per input file. Default 32768. All inputs are truncated to this. |
| 65 |
- `-o` Output name. Default `dictionary.bin`. |
| 66 |
- `-q` Do not print progress |
| 67 |
- `-dictID` zstd dictionary ID. 0 will be random. Default 0. |
| 68 |
- `-zcompat` Generate dictionary compatible with zstd 1.5.5 and older. Default false. |
| 69 |
- `-zlevel` Zstandard compression level. |
| 70 |
|
| 71 |
The Zstandard compression level to use when compressing the samples. |
| 72 |
The dictionary will be built using the specified encoder level, |
| 73 |
which will reflect speed and make the dictionary tailored for that level. |
| 74 |
Default will use level 4 (best). |
| 75 |
|
| 76 |
Valid values are 1-4, where 1 = fastest, 2 = default, 3 = better, 4 = best. |
| 77 |
|
| 78 |
## Library |
| 79 |
|
| 80 |
The `github.com/klaupost/compress/dict` package can be used to build dictionaries in code. |
| 81 |
The caller must supply a collection of (pre-truncated) samples, and the options to use. |
| 82 |
The options largely correspond to the command line options. |
| 83 |
|
| 84 |
```Go |
| 85 |
package main |
| 86 |
|
| 87 |
import ( |
| 88 |
"github.com/klaupost/compress/dict" |
| 89 |
"github.com/klauspost/compress/zstd" |
| 90 |
) |
| 91 |
|
| 92 |
func main() { |
| 93 |
var samples [][]byte |
| 94 |
|
| 95 |
// ... Fill samples with representative data. |
| 96 |
|
| 97 |
dict, err := dict.BuildZstdDict(samples, dict.Options{ |
| 98 |
HashLen: 6, |
| 99 |
MaxDictSize: 114688, |
| 100 |
ZstdDictID: 0, // Random |
| 101 |
ZstdCompat: false, |
| 102 |
ZstdLevel: zstd.SpeedBestCompression, |
| 103 |
}) |
| 104 |
// ... Handle error, etc. |
| 105 |
} |
| 106 |
``` |
| 107 |
|
| 108 |
There are similar functions for S2 and raw dictionaries (`BuildS2Dict` and `BuildRawDict`). |