snappy 0.3.0-java → 0.5.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/main.yml +2 -2
  3. data/.github/workflows/publish.yml +7 -13
  4. data/Dockerfile +1 -1
  5. data/Gemfile +1 -0
  6. data/README.md +20 -1
  7. data/Rakefile +1 -1
  8. data/ext/extconf.rb +13 -11
  9. data/lib/snappy/shim.rb +3 -23
  10. data/lib/snappy/version.rb +1 -1
  11. data/lib/snappy/writer.rb +1 -1
  12. data/lib/snappy_ext.jar +0 -0
  13. data/snappy.gemspec +1 -0
  14. data/test/snappy_test.rb +29 -4
  15. data/vendor/snappy/BUILD.bazel +211 -0
  16. data/vendor/snappy/CMakeLists.txt +176 -31
  17. data/vendor/snappy/CONTRIBUTING.md +9 -4
  18. data/vendor/snappy/MODULE.bazel +23 -0
  19. data/vendor/snappy/NEWS +27 -0
  20. data/vendor/snappy/README.md +52 -35
  21. data/vendor/snappy/WORKSPACE +27 -0
  22. data/vendor/snappy/WORKSPACE.bzlmod +0 -0
  23. data/vendor/snappy/cmake/config.h.in +30 -23
  24. data/vendor/snappy/snappy-internal.h +218 -25
  25. data/vendor/snappy/snappy-sinksource.cc +26 -9
  26. data/vendor/snappy/snappy-sinksource.h +11 -11
  27. data/vendor/snappy/snappy-stubs-internal.cc +1 -1
  28. data/vendor/snappy/snappy-stubs-internal.h +231 -306
  29. data/vendor/snappy/snappy-stubs-public.h.in +0 -11
  30. data/vendor/snappy/snappy-test.cc +88 -198
  31. data/vendor/snappy/snappy-test.h +102 -285
  32. data/vendor/snappy/snappy.cc +1412 -425
  33. data/vendor/snappy/snappy.h +60 -10
  34. data/vendor/snappy/snappy_benchmark.cc +398 -0
  35. data/vendor/snappy/snappy_compress_fuzzer.cc +21 -16
  36. data/vendor/snappy/snappy_test_data.cc +57 -0
  37. data/vendor/snappy/snappy_test_data.h +68 -0
  38. data/vendor/snappy/snappy_test_tool.cc +471 -0
  39. data/vendor/snappy/snappy_uncompress_fuzzer.cc +3 -2
  40. data/vendor/snappy/snappy_unittest.cc +183 -666
  41. metadata +13 -8
@@ -39,7 +39,9 @@
39
39
  #ifndef THIRD_PARTY_SNAPPY_SNAPPY_H__
40
40
  #define THIRD_PARTY_SNAPPY_SNAPPY_H__
41
41
 
42
- #include <cstddef>
42
+ #include <stddef.h>
43
+ #include <stdint.h>
44
+
43
45
  #include <string>
44
46
 
45
47
  #include "snappy-stubs-public.h"
@@ -48,13 +50,38 @@ namespace snappy {
48
50
  class Source;
49
51
  class Sink;
50
52
 
53
+ struct CompressionOptions {
54
+ // Compression level.
55
+ // Level 1 is the fastest
56
+ // Level 2 is a little slower but provides better compression. Level 2 is
57
+ // **EXPERIMENTAL** for the time being. It might happen that we decide to
58
+ // fall back to level 1 in the future.
59
+ // Levels 3+ are currently not supported. We plan to support levels up to
60
+ // 9 in the future.
61
+ // If you played with other compression algorithms, level 1 is equivalent to
62
+ // fast mode (level 1) of LZ4, level 2 is equivalent to LZ4's level 2 mode
63
+ // and compresses somewhere around zstd:-3 and zstd:-2 but generally with
64
+ // faster decompression speeds than snappy:1 and zstd:-3.
65
+ int level = DefaultCompressionLevel();
66
+
67
+ constexpr CompressionOptions() = default;
68
+ constexpr CompressionOptions(int compression_level)
69
+ : level(compression_level) {}
70
+ static constexpr int MinCompressionLevel() { return 1; }
71
+ static constexpr int MaxCompressionLevel() { return 2; }
72
+ static constexpr int DefaultCompressionLevel() { return 1; }
73
+ };
74
+
51
75
  // ------------------------------------------------------------------------
52
76
  // Generic compression/decompression routines.
53
77
  // ------------------------------------------------------------------------
54
78
 
55
- // Compress the bytes read from "*source" and append to "*sink". Return the
79
+ // Compress the bytes read from "*reader" and append to "*writer". Return the
56
80
  // number of bytes written.
57
- size_t Compress(Source* source, Sink* sink);
81
+ // First version is to preserve ABI.
82
+ size_t Compress(Source* reader, Sink* writer);
83
+ size_t Compress(Source* reader, Sink* writer,
84
+ CompressionOptions options);
58
85
 
59
86
  // Find the uncompressed length of the given stream, as given by the header.
60
87
  // Note that the true length could deviate from this; the stream could e.g.
@@ -63,20 +90,34 @@ namespace snappy {
63
90
  // Also note that this leaves "*source" in a state that is unsuitable for
64
91
  // further operations, such as RawUncompress(). You will need to rewind
65
92
  // or recreate the source yourself before attempting any further calls.
66
- bool GetUncompressedLength(Source* source, uint32* result);
93
+ bool GetUncompressedLength(Source* source, uint32_t* result);
67
94
 
68
95
  // ------------------------------------------------------------------------
69
96
  // Higher-level string based routines (should be sufficient for most users)
70
97
  // ------------------------------------------------------------------------
71
98
 
72
- // Sets "*compressed" to the compressed version of "input[0,input_length-1]".
99
+ // Sets "*compressed" to the compressed version of "input[0..input_length-1]".
73
100
  // Original contents of *compressed are lost.
74
101
  //
75
102
  // REQUIRES: "input[]" is not an alias of "*compressed".
103
+ // First version is to preserve ABI.
76
104
  size_t Compress(const char* input, size_t input_length,
77
105
  std::string* compressed);
106
+ size_t Compress(const char* input, size_t input_length,
107
+ std::string* compressed, CompressionOptions options);
108
+
109
+ // Same as `Compress` above but taking an `iovec` array as input. Note that
110
+ // this function preprocesses the inputs to compute the sum of
111
+ // `iov[0..iov_cnt-1].iov_len` before reading. To avoid this, use
112
+ // `RawCompressFromIOVec` below.
113
+ // First version is to preserve ABI.
114
+ size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
115
+ std::string* compressed);
116
+ size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
117
+ std::string* compressed,
118
+ CompressionOptions options);
78
119
 
79
- // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed".
120
+ // Decompresses "compressed[0..compressed_length-1]" to "*uncompressed".
80
121
  // Original contents of "*uncompressed" are lost.
81
122
  //
82
123
  // REQUIRES: "compressed[]" is not an alias of "*uncompressed".
@@ -117,10 +158,19 @@ namespace snappy {
117
158
  // RawCompress(input, input_length, output, &output_length);
118
159
  // ... Process(output, output_length) ...
119
160
  // delete [] output;
120
- void RawCompress(const char* input,
121
- size_t input_length,
122
- char* compressed,
161
+ void RawCompress(const char* input, size_t input_length, char* compressed,
123
162
  size_t* compressed_length);
163
+ void RawCompress(const char* input, size_t input_length, char* compressed,
164
+ size_t* compressed_length, CompressionOptions options);
165
+
166
+ // Same as `RawCompress` above but taking an `iovec` array as input. Note that
167
+ // `uncompressed_length` is the total number of bytes to be read from the
168
+ // elements of `iov` (_not_ the number of elements in `iov`).
169
+ void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
170
+ char* compressed, size_t* compressed_length);
171
+ void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
172
+ char* compressed, size_t* compressed_length,
173
+ CompressionOptions options);
124
174
 
125
175
  // Given data in "compressed[0..compressed_length-1]" generated by
126
176
  // calling the Snappy::Compress routine, this routine
@@ -200,7 +250,7 @@ namespace snappy {
200
250
  static constexpr int kMinHashTableBits = 8;
201
251
  static constexpr size_t kMinHashTableSize = 1 << kMinHashTableBits;
202
252
 
203
- static constexpr int kMaxHashTableBits = 14;
253
+ static constexpr int kMaxHashTableBits = 15;
204
254
  static constexpr size_t kMaxHashTableSize = 1 << kMaxHashTableBits;
205
255
  } // end namespace snappy
206
256
 
@@ -0,0 +1,398 @@
1
+ // Copyright 2020 Google Inc. All Rights Reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are
5
+ // met:
6
+ //
7
+ // * Redistributions of source code must retain the above copyright
8
+ // notice, this list of conditions and the following disclaimer.
9
+ // * Redistributions in binary form must reproduce the above
10
+ // copyright notice, this list of conditions and the following disclaimer
11
+ // in the documentation and/or other materials provided with the
12
+ // distribution.
13
+ // * Neither the name of Google Inc. nor the names of its
14
+ // contributors may be used to endorse or promote products derived from
15
+ // this software without specific prior written permission.
16
+ //
17
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ #include <cstddef>
30
+ #include <cstdint>
31
+ #include <string>
32
+ #include <vector>
33
+
34
+ #include "benchmark/benchmark.h"
35
+ #include "snappy-internal.h"
36
+ #include "snappy-sinksource.h"
37
+ #include "snappy-test.h"
38
+ #include "snappy.h"
39
+ #include "snappy_test_data.h"
40
+
41
+ namespace snappy {
42
+
43
+ namespace {
44
+
45
+ void FilesAndLevels(benchmark::internal::Benchmark* benchmark) {
46
+ for (int i = 0; i < ARRAYSIZE(kTestDataFiles); ++i) {
47
+ for (int level = snappy::CompressionOptions::MinCompressionLevel();
48
+ level <= snappy::CompressionOptions::MaxCompressionLevel(); ++level) {
49
+ benchmark->ArgPair(i, level);
50
+ }
51
+ }
52
+ }
53
+
54
+ void BM_UFlat(benchmark::State& state) {
55
+ // Pick file to process based on state.range(0).
56
+ int file_index = state.range(0);
57
+
58
+ CHECK_GE(file_index, 0);
59
+ CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
60
+ std::string contents =
61
+ ReadTestDataFile(kTestDataFiles[file_index].filename,
62
+ kTestDataFiles[file_index].size_limit);
63
+
64
+ std::string zcontents;
65
+ snappy::Compress(
66
+ contents.data(), contents.size(), &zcontents,
67
+ snappy::CompressionOptions{/*level=*/static_cast<int>(state.range(1))});
68
+ char* dst = new char[contents.size()];
69
+
70
+ for (auto s : state) {
71
+ CHECK(snappy::RawUncompress(zcontents.data(), zcontents.size(), dst));
72
+ benchmark::DoNotOptimize(dst);
73
+ }
74
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
75
+ static_cast<int64_t>(contents.size()));
76
+ state.SetLabel(kTestDataFiles[file_index].label);
77
+
78
+ delete[] dst;
79
+ }
80
+ BENCHMARK(BM_UFlat)->Apply(FilesAndLevels);
81
+
82
+ struct SourceFiles {
83
+ SourceFiles() {
84
+ for (int i = 0; i < kFiles; i++) {
85
+ std::string contents = ReadTestDataFile(kTestDataFiles[i].filename,
86
+ kTestDataFiles[i].size_limit);
87
+ max_size = std::max(max_size, contents.size());
88
+ sizes[i] = contents.size();
89
+ snappy::Compress(contents.data(), contents.size(), &zcontents[i]);
90
+ }
91
+ }
92
+ static constexpr int kFiles = ARRAYSIZE(kTestDataFiles);
93
+ std::string zcontents[kFiles];
94
+ size_t sizes[kFiles];
95
+ size_t max_size = 0;
96
+ };
97
+
98
+ void BM_UFlatMedley(benchmark::State& state) {
99
+ static const SourceFiles* const source = new SourceFiles();
100
+
101
+ std::vector<char> dst(source->max_size);
102
+
103
+ for (auto s : state) {
104
+ for (int i = 0; i < SourceFiles::kFiles; i++) {
105
+ CHECK(snappy::RawUncompress(source->zcontents[i].data(),
106
+ source->zcontents[i].size(), dst.data()));
107
+ benchmark::DoNotOptimize(dst);
108
+ }
109
+ }
110
+
111
+ int64_t source_sizes = 0;
112
+ for (int i = 0; i < SourceFiles::kFiles; i++) {
113
+ source_sizes += static_cast<int64_t>(source->sizes[i]);
114
+ }
115
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
116
+ source_sizes);
117
+ }
118
+ BENCHMARK(BM_UFlatMedley);
119
+
120
+ void BM_UValidate(benchmark::State& state) {
121
+ // Pick file to process based on state.range(0).
122
+ int file_index = state.range(0);
123
+
124
+ CHECK_GE(file_index, 0);
125
+ CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
126
+ std::string contents =
127
+ ReadTestDataFile(kTestDataFiles[file_index].filename,
128
+ kTestDataFiles[file_index].size_limit);
129
+
130
+ std::string zcontents;
131
+ snappy::Compress(
132
+ contents.data(), contents.size(), &zcontents,
133
+ snappy::CompressionOptions{/*level=*/static_cast<int>(state.range(1))});
134
+
135
+ for (auto s : state) {
136
+ CHECK(snappy::IsValidCompressedBuffer(zcontents.data(), zcontents.size()));
137
+ }
138
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
139
+ static_cast<int64_t>(contents.size()));
140
+ state.SetLabel(kTestDataFiles[file_index].label);
141
+ }
142
+ BENCHMARK(BM_UValidate)->Apply(FilesAndLevels);
143
+
144
+ void BM_UValidateMedley(benchmark::State& state) {
145
+ static const SourceFiles* const source = new SourceFiles();
146
+
147
+ for (auto s : state) {
148
+ for (int i = 0; i < SourceFiles::kFiles; i++) {
149
+ CHECK(snappy::IsValidCompressedBuffer(source->zcontents[i].data(),
150
+ source->zcontents[i].size()));
151
+ }
152
+ }
153
+
154
+ int64_t source_sizes = 0;
155
+ for (int i = 0; i < SourceFiles::kFiles; i++) {
156
+ source_sizes += static_cast<int64_t>(source->sizes[i]);
157
+ }
158
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
159
+ source_sizes);
160
+ }
161
+ BENCHMARK(BM_UValidateMedley);
162
+
163
+ void BM_UIOVecSource(benchmark::State& state) {
164
+ // Pick file to process based on state.range(0).
165
+ int file_index = state.range(0);
166
+ int level = state.range(1);
167
+
168
+ CHECK_GE(file_index, 0);
169
+ CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
170
+ std::string contents =
171
+ ReadTestDataFile(kTestDataFiles[file_index].filename,
172
+ kTestDataFiles[file_index].size_limit);
173
+
174
+ // Create `iovec`s of the `contents`.
175
+ const int kNumEntries = 10;
176
+ struct iovec iov[kNumEntries];
177
+ size_t used_so_far = 0;
178
+ for (int i = 0; i < kNumEntries; ++i) {
179
+ iov[i].iov_base = const_cast<char*>(contents.data()) + used_so_far;
180
+ if (used_so_far == contents.size()) {
181
+ iov[i].iov_len = 0;
182
+ continue;
183
+ }
184
+ if (i == kNumEntries - 1) {
185
+ iov[i].iov_len = contents.size() - used_so_far;
186
+ } else {
187
+ iov[i].iov_len = contents.size() / kNumEntries;
188
+ }
189
+ used_so_far += iov[i].iov_len;
190
+ }
191
+
192
+ char* dst = new char[snappy::MaxCompressedLength(contents.size())];
193
+ size_t zsize = 0;
194
+ for (auto s : state) {
195
+ snappy::RawCompressFromIOVec(iov, contents.size(), dst, &zsize,
196
+ snappy::CompressionOptions{/*level=*/level});
197
+ benchmark::DoNotOptimize(iov);
198
+ }
199
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
200
+ static_cast<int64_t>(contents.size()));
201
+ const double compression_ratio =
202
+ static_cast<double>(zsize) / std::max<size_t>(1, contents.size());
203
+ state.SetLabel(StrFormat("%s (%.2f %%)", kTestDataFiles[file_index].label,
204
+ 100.0 * compression_ratio));
205
+ VLOG(0) << StrFormat("compression for %s: %d -> %d bytes",
206
+ kTestDataFiles[file_index].label, contents.size(),
207
+ zsize);
208
+
209
+ delete[] dst;
210
+ }
211
+ BENCHMARK(BM_UIOVecSource)->Apply(FilesAndLevels);
212
+
213
+ void BM_UIOVecSink(benchmark::State& state) {
214
+ // Pick file to process based on state.range(0).
215
+ int file_index = state.range(0);
216
+
217
+ CHECK_GE(file_index, 0);
218
+ CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
219
+ std::string contents =
220
+ ReadTestDataFile(kTestDataFiles[file_index].filename,
221
+ kTestDataFiles[file_index].size_limit);
222
+
223
+ std::string zcontents;
224
+ snappy::Compress(contents.data(), contents.size(), &zcontents);
225
+
226
+ // Uncompress into an iovec containing ten entries.
227
+ const int kNumEntries = 10;
228
+ struct iovec iov[kNumEntries];
229
+ char* dst = new char[contents.size()];
230
+ size_t used_so_far = 0;
231
+ for (int i = 0; i < kNumEntries; ++i) {
232
+ iov[i].iov_base = dst + used_so_far;
233
+ if (used_so_far == contents.size()) {
234
+ iov[i].iov_len = 0;
235
+ continue;
236
+ }
237
+
238
+ if (i == kNumEntries - 1) {
239
+ iov[i].iov_len = contents.size() - used_so_far;
240
+ } else {
241
+ iov[i].iov_len = contents.size() / kNumEntries;
242
+ }
243
+ used_so_far += iov[i].iov_len;
244
+ }
245
+
246
+ for (auto s : state) {
247
+ CHECK(snappy::RawUncompressToIOVec(zcontents.data(), zcontents.size(), iov,
248
+ kNumEntries));
249
+ benchmark::DoNotOptimize(iov);
250
+ }
251
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
252
+ static_cast<int64_t>(contents.size()));
253
+ state.SetLabel(kTestDataFiles[file_index].label);
254
+
255
+ delete[] dst;
256
+ }
257
+ BENCHMARK(BM_UIOVecSink)->DenseRange(0, 4);
258
+
259
+ void BM_UFlatSink(benchmark::State& state) {
260
+ // Pick file to process based on state.range(0).
261
+ int file_index = state.range(0);
262
+
263
+ CHECK_GE(file_index, 0);
264
+ CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
265
+ std::string contents =
266
+ ReadTestDataFile(kTestDataFiles[file_index].filename,
267
+ kTestDataFiles[file_index].size_limit);
268
+
269
+ std::string zcontents;
270
+ snappy::Compress(
271
+ contents.data(), contents.size(), &zcontents,
272
+ snappy::CompressionOptions{/*level=*/static_cast<int>(state.range(1))});
273
+ char* dst = new char[contents.size()];
274
+
275
+ for (auto s : state) {
276
+ snappy::ByteArraySource source(zcontents.data(), zcontents.size());
277
+ snappy::UncheckedByteArraySink sink(dst);
278
+ CHECK(snappy::Uncompress(&source, &sink));
279
+ benchmark::DoNotOptimize(sink);
280
+ }
281
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
282
+ static_cast<int64_t>(contents.size()));
283
+ state.SetLabel(kTestDataFiles[file_index].label);
284
+
285
+ std::string s(dst, contents.size());
286
+ CHECK_EQ(contents, s);
287
+
288
+ delete[] dst;
289
+ }
290
+
291
+ BENCHMARK(BM_UFlatSink)->Apply(FilesAndLevels);
292
+
293
+ void BM_ZFlat(benchmark::State& state) {
294
+ // Pick file to process based on state.range(0).
295
+ int file_index = state.range(0);
296
+ int level = state.range(1);
297
+
298
+ CHECK_GE(file_index, 0);
299
+ CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
300
+ std::string contents =
301
+ ReadTestDataFile(kTestDataFiles[file_index].filename,
302
+ kTestDataFiles[file_index].size_limit);
303
+ char* dst = new char[snappy::MaxCompressedLength(contents.size())];
304
+
305
+ size_t zsize = 0;
306
+ for (auto s : state) {
307
+ snappy::RawCompress(contents.data(), contents.size(), dst, &zsize,
308
+ snappy::CompressionOptions{/*level=*/level});
309
+ benchmark::DoNotOptimize(dst);
310
+ }
311
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
312
+ static_cast<int64_t>(contents.size()));
313
+ const double compression_ratio =
314
+ static_cast<double>(zsize) / std::max<size_t>(1, contents.size());
315
+ state.SetLabel(StrFormat("%s (%.2f %%)", kTestDataFiles[file_index].label,
316
+ 100.0 * compression_ratio));
317
+ VLOG(0) << StrFormat("compression for %s: %d -> %d bytes",
318
+ kTestDataFiles[file_index].label, contents.size(),
319
+ zsize);
320
+ delete[] dst;
321
+ }
322
+
323
+ BENCHMARK(BM_ZFlat)->Apply(FilesAndLevels);
324
+
325
+ void BM_ZFlatAll(benchmark::State& state) {
326
+ const int num_files = ARRAYSIZE(kTestDataFiles);
327
+ int level = state.range(0);
328
+
329
+ std::vector<std::string> contents(num_files);
330
+ std::vector<char*> dst(num_files);
331
+
332
+ int64_t total_contents_size = 0;
333
+ for (int i = 0; i < num_files; ++i) {
334
+ contents[i] = ReadTestDataFile(kTestDataFiles[i].filename,
335
+ kTestDataFiles[i].size_limit);
336
+ dst[i] = new char[snappy::MaxCompressedLength(contents[i].size())];
337
+ total_contents_size += contents[i].size();
338
+ }
339
+
340
+ size_t zsize = 0;
341
+ for (auto s : state) {
342
+ for (int i = 0; i < num_files; ++i) {
343
+ snappy::RawCompress(contents[i].data(), contents[i].size(), dst[i],
344
+ &zsize, snappy::CompressionOptions{/*level=*/level});
345
+ benchmark::DoNotOptimize(dst);
346
+ }
347
+ }
348
+
349
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
350
+ total_contents_size);
351
+
352
+ for (char* dst_item : dst) {
353
+ delete[] dst_item;
354
+ }
355
+ state.SetLabel(StrFormat("%d kTestDataFiles", num_files));
356
+ }
357
+ BENCHMARK(BM_ZFlatAll)->DenseRange(1, 2);
358
+
359
+ void BM_ZFlatIncreasingTableSize(benchmark::State& state) {
360
+ CHECK_GT(ARRAYSIZE(kTestDataFiles), 0);
361
+ int level = state.range(0);
362
+ const std::string base_content = ReadTestDataFile(
363
+ kTestDataFiles[0].filename, kTestDataFiles[0].size_limit);
364
+
365
+ std::vector<std::string> contents;
366
+ std::vector<char*> dst;
367
+ int64_t total_contents_size = 0;
368
+ for (int table_bits = kMinHashTableBits; table_bits <= kMaxHashTableBits;
369
+ ++table_bits) {
370
+ std::string content = base_content;
371
+ content.resize(1 << table_bits);
372
+ dst.push_back(new char[snappy::MaxCompressedLength(content.size())]);
373
+ total_contents_size += content.size();
374
+ contents.push_back(std::move(content));
375
+ }
376
+
377
+ size_t zsize = 0;
378
+ for (auto s : state) {
379
+ for (size_t i = 0; i < contents.size(); ++i) {
380
+ snappy::RawCompress(contents[i].data(), contents[i].size(), dst[i],
381
+ &zsize, snappy::CompressionOptions{/*level=*/level});
382
+ benchmark::DoNotOptimize(dst);
383
+ }
384
+ }
385
+
386
+ state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
387
+ total_contents_size);
388
+
389
+ for (char* dst_item : dst) {
390
+ delete[] dst_item;
391
+ }
392
+ state.SetLabel(StrFormat("%d tables", contents.size()));
393
+ }
394
+ BENCHMARK(BM_ZFlatIncreasingTableSize)->DenseRange(1, 2);
395
+
396
+ } // namespace
397
+
398
+ } // namespace snappy
@@ -28,9 +28,10 @@
28
28
  //
29
29
  // libFuzzer harness for fuzzing snappy compression code.
30
30
 
31
+ #include <stddef.h>
32
+ #include <stdint.h>
33
+
31
34
  #include <cassert>
32
- #include <cstddef>
33
- #include <cstdint>
34
35
  #include <string>
35
36
 
36
37
  #include "snappy.h"
@@ -38,22 +39,26 @@
38
39
  // Entry point for LibFuzzer.
39
40
  extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
40
41
  std::string input(reinterpret_cast<const char*>(data), size);
42
+ for (int level = snappy::CompressionOptions::MinCompressionLevel();
43
+ level <= snappy::CompressionOptions::MaxCompressionLevel(); ++level) {
44
+ std::string compressed;
45
+ size_t compressed_size =
46
+ snappy::Compress(input.data(), input.size(), &compressed,
47
+ snappy::CompressionOptions{/*level=*/level});
41
48
 
42
- std::string compressed;
43
- size_t compressed_size =
44
- snappy::Compress(input.data(), input.size(), &compressed);
45
-
46
- (void)compressed_size; // Variable only used in debug builds.
47
- assert(compressed_size == compressed.size());
48
- assert(compressed.size() <= snappy::MaxCompressedLength(input.size()));
49
- assert(snappy::IsValidCompressedBuffer(compressed.data(), compressed.size()));
49
+ (void)compressed_size; // Variable only used in debug builds.
50
+ assert(compressed_size == compressed.size());
51
+ assert(compressed.size() <= snappy::MaxCompressedLength(input.size()));
52
+ assert(
53
+ snappy::IsValidCompressedBuffer(compressed.data(), compressed.size()));
50
54
 
51
- std::string uncompressed_after_compress;
52
- bool uncompress_succeeded = snappy::Uncompress(
53
- compressed.data(), compressed.size(), &uncompressed_after_compress);
55
+ std::string uncompressed_after_compress;
56
+ bool uncompress_succeeded = snappy::Uncompress(
57
+ compressed.data(), compressed.size(), &uncompressed_after_compress);
54
58
 
55
- (void)uncompress_succeeded; // Variable only used in debug builds.
56
- assert(uncompress_succeeded);
57
- assert(input == uncompressed_after_compress);
59
+ (void)uncompress_succeeded; // Variable only used in debug builds.
60
+ assert(uncompress_succeeded);
61
+ assert(input == uncompressed_after_compress);
62
+ }
58
63
  return 0;
59
64
  }
@@ -0,0 +1,57 @@
1
+ // Copyright 2020 Google Inc. All Rights Reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are
5
+ // met:
6
+ //
7
+ // * Redistributions of source code must retain the above copyright
8
+ // notice, this list of conditions and the following disclaimer.
9
+ // * Redistributions in binary form must reproduce the above
10
+ // copyright notice, this list of conditions and the following disclaimer
11
+ // in the documentation and/or other materials provided with the
12
+ // distribution.
13
+ // * Neither the name of Google Inc. nor the names of its
14
+ // contributors may be used to endorse or promote products derived from
15
+ // this software without specific prior written permission.
16
+ //
17
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ //
29
+ // Support code for reading test data.
30
+
31
+ #include "snappy_test_data.h"
32
+
33
+ #include <cstddef>
34
+ #include <cstdlib>
35
+ #include <string>
36
+
37
+ #include "snappy-test.h"
38
+
39
+ namespace snappy {
40
+
41
+ std::string ReadTestDataFile(const char* base, size_t size_limit) {
42
+ std::string srcdir;
43
+ const char* srcdir_env = std::getenv("srcdir"); // This is set by Automake.
44
+ if (srcdir_env) {
45
+ srcdir = std::string(srcdir_env) + "/";
46
+ }
47
+
48
+ std::string contents;
49
+ CHECK_OK(file::GetContents(srcdir + "testdata/" + base, &contents,
50
+ file::Defaults()));
51
+ if (size_limit > 0) {
52
+ contents = contents.substr(0, size_limit);
53
+ }
54
+ return contents;
55
+ }
56
+
57
+ } // namespace snappy