|
2 | 2 |
|
3 | 3 | import java.lang.foreign.Arena; |
4 | 4 | import java.lang.foreign.MemorySegment; |
| 5 | +import java.lang.invoke.MethodHandle; |
5 | 6 | import java.nio.charset.StandardCharsets; |
6 | 7 | import java.util.List; |
7 | 8 |
|
8 | 9 | import static java.lang.foreign.ValueLayout.JAVA_BYTE; |
| 10 | +import static java.lang.foreign.ValueLayout.JAVA_INT; |
9 | 11 | import static java.lang.foreign.ValueLayout.JAVA_LONG; |
10 | 12 |
|
11 | 13 | /// An immutable zstd dictionary — the feature that makes compressing many small, |
@@ -86,6 +88,95 @@ public static ZstdDictionary train(List<byte[]> samples, int maxDictBytes) { |
86 | 88 | } |
87 | 89 | } |
88 | 90 |
|
| 91 | + /// Trains a dictionary with the COVER algorithm, auto-tuning its parameters |
| 92 | + /// for the best dictionary it can find. Higher quality than {@link #train}, |
| 93 | + /// but slower; for a faster near-equal result use {@link #trainFastCover}. |
| 94 | + /// |
| 95 | + /// @param samples representative payloads to learn from |
| 96 | + /// @param maxDictBytes upper bound on the produced dictionary size |
| 97 | + /// @return the trained dictionary |
| 98 | + /// @throws ZstdException if training fails |
| 99 | + public static ZstdDictionary trainCover(List<byte[]> samples, int maxDictBytes) { |
| 100 | + return trainCover(samples, maxDictBytes, 0); |
| 101 | + } |
| 102 | + |
| 103 | + /// Trains a COVER dictionary optimised for a specific compression level. |
| 104 | + /// |
| 105 | + /// @param samples representative payloads to learn from |
| 106 | + /// @param maxDictBytes upper bound on the produced dictionary size |
| 107 | + /// @param compressionLevel the level the dictionary will be used at (0 = default) |
| 108 | + /// @return the trained dictionary |
| 109 | + /// @throws ZstdException if training fails |
| 110 | + public static ZstdDictionary trainCover(List<byte[]> samples, int maxDictBytes, int compressionLevel) { |
| 111 | + return optimize(samples, maxDictBytes, compressionLevel, false); |
| 112 | + } |
| 113 | + |
| 114 | + /// Trains a dictionary with the fast COVER algorithm, auto-tuning its |
| 115 | + /// parameters. The recommended optimiser: nearly the quality of |
| 116 | + /// {@link #trainCover} at a fraction of the time. |
| 117 | + /// |
| 118 | + /// @param samples representative payloads to learn from |
| 119 | + /// @param maxDictBytes upper bound on the produced dictionary size |
| 120 | + /// @return the trained dictionary |
| 121 | + /// @throws ZstdException if training fails |
| 122 | + public static ZstdDictionary trainFastCover(List<byte[]> samples, int maxDictBytes) { |
| 123 | + return trainFastCover(samples, maxDictBytes, 0); |
| 124 | + } |
| 125 | + |
| 126 | + /// Trains a fast COVER dictionary optimised for a specific compression level. |
| 127 | + /// |
| 128 | + /// @param samples representative payloads to learn from |
| 129 | + /// @param maxDictBytes upper bound on the produced dictionary size |
| 130 | + /// @param compressionLevel the level the dictionary will be used at (0 = default) |
| 131 | + /// @return the trained dictionary |
| 132 | + /// @throws ZstdException if training fails |
| 133 | + public static ZstdDictionary trainFastCover(List<byte[]> samples, int maxDictBytes, int compressionLevel) { |
| 134 | + return optimize(samples, maxDictBytes, compressionLevel, true); |
| 135 | + } |
| 136 | + |
| 137 | + private static ZstdDictionary optimize(List<byte[]> samples, int maxDictBytes, |
| 138 | + int compressionLevel, boolean fast) { |
| 139 | + if (samples.isEmpty()) { |
| 140 | + throw new ZstdException("cannot train a dictionary from zero samples"); |
| 141 | + } |
| 142 | + try (Arena arena = Arena.ofConfined()) { |
| 143 | + long total = 0; |
| 144 | + for (byte[] s : samples) { |
| 145 | + total += s.length; |
| 146 | + } |
| 147 | + MemorySegment flat = arena.allocate(Math.max(total, 1)); |
| 148 | + MemorySegment sizes = arena.allocate(JAVA_LONG, samples.size()); |
| 149 | + long offset = 0; |
| 150 | + for (int i = 0; i < samples.size(); i++) { |
| 151 | + byte[] s = samples.get(i); |
| 152 | + MemorySegment.copy(s, 0, flat, JAVA_BYTE, offset, s.length); |
| 153 | + sizes.setAtIndex(JAVA_LONG, i, s.length); |
| 154 | + offset += s.length; |
| 155 | + } |
| 156 | + // zeroed params (auto-tune k/d/steps); set single-threaded + target level. |
| 157 | + // fastCover: nbThreads@16, compressionLevel@44, size 56. |
| 158 | + // cover: nbThreads@12, compressionLevel@32, size 48. |
| 159 | + MemorySegment params = arena.allocate(fast ? 56 : 48); |
| 160 | + params.set(JAVA_INT, fast ? 16 : 12, 1); |
| 161 | + params.set(JAVA_INT, fast ? 44 : 32, compressionLevel); |
| 162 | + MethodHandle handle = fast ? Bindings.ZDICT_OPTIMIZE_FASTCOVER : Bindings.ZDICT_OPTIMIZE_COVER; |
| 163 | + MemorySegment dictBuf = arena.allocate(maxDictBytes); |
| 164 | + long produced; |
| 165 | + try { |
| 166 | + produced = (long) handle.invokeExact( |
| 167 | + dictBuf, (long) maxDictBytes, flat, sizes, samples.size(), params); |
| 168 | + } catch (Throwable t) { |
| 169 | + throw rethrow(t); |
| 170 | + } |
| 171 | + if (zdictIsError(produced)) { |
| 172 | + throw new ZstdException("dictionary training failed: " + zdictErrorName(produced)); |
| 173 | + } |
| 174 | + byte[] out = new byte[Math.toIntExact(produced)]; |
| 175 | + MemorySegment.copy(dictBuf, JAVA_BYTE, 0, out, 0, out.length); |
| 176 | + return new ZstdDictionary(out); |
| 177 | + } |
| 178 | + } |
| 179 | + |
89 | 180 | /// The dictionary id zstd stamps into frames compressed with this dictionary, |
90 | 181 | /// or `0` for a raw/content-only dictionary with no header. |
91 | 182 | /// |
|
0 commit comments