-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathCompressionUtils.cpp
209 lines (172 loc) · 6.49 KB
/
CompressionUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* CompressionUtils.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "flow/CompressionUtils.h"
#include "flow/Arena.h"
#include "flow/Error.h"
#include "flow/IRandom.h"
#include "flow/UnitTest.h"
#ifdef ZSTD_LIB_SUPPORTED
#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>
static constexpr int ZSTD_COMPRESSION_LEVEL_1 = 1;
#endif
namespace {
std::unordered_set<CompressionFilter> getSupportedFilters() {
std::unordered_set<CompressionFilter> filters;
filters.insert(CompressionFilter::NONE);
#ifdef ZSTD_LIB_SUPPORTED
filters.insert(CompressionFilter::ZSTD);
#endif
ASSERT_GE(filters.size(), 1);
return filters;
}
} // namespace
std::unordered_set<CompressionFilter> CompressionUtils::supportedFilters = getSupportedFilters();
StringRef CompressionUtils::compress(const CompressionFilter filter, const StringRef& data, Arena& arena) {
checkFilterSupported(filter);
if (filter == CompressionFilter::NONE) {
return StringRef(arena, data);
}
#ifdef ZSTD_LIB_SUPPORTED
if (filter == CompressionFilter::ZSTD) {
return CompressionUtils::compress(filter, data, ZSTD_COMPRESSION_LEVEL_1, arena);
}
#endif
throw internal_error(); // We should never get here
}
StringRef CompressionUtils::compress(const CompressionFilter filter, const StringRef& data, int level, Arena& arena) {
checkFilterSupported(filter);
if (filter == CompressionFilter::NONE) {
return StringRef(arena, data);
}
#ifdef ZSTD_LIB_SUPPORTED
if (filter == CompressionFilter::ZSTD) {
const char* src = reinterpret_cast<const char*>(data.begin());
size_t destSize = ZSTD_compressBound(data.size());
std::unique_ptr<uint8_t[]> dest = std::make_unique<uint8_t[]>(destSize);
size_t bytes = ZSTD_compress(dest.get(), destSize, src, data.size(), level);
if (ZSTD_isError(bytes)) {
throw internal_error();
}
return StringRef(arena, StringRef(dest.get(), bytes));
}
#endif
throw internal_error(); // We should never get here
}
StringRef CompressionUtils::decompress(const CompressionFilter filter, const StringRef& data, Arena& arena) {
checkFilterSupported(filter);
if (filter == CompressionFilter::NONE) {
return StringRef(arena, data);
}
#ifdef ZSTD_LIB_SUPPORTED
if (filter == CompressionFilter::ZSTD) {
const char* src = reinterpret_cast<const char*>(data.begin());
size_t destSize = ZSTD_decompressBound(src, data.size());
std::unique_ptr<uint8_t[]> dest = std::make_unique<uint8_t[]>(destSize);
size_t bytes = ZSTD_decompress(dest.get(), destSize, src, data.size());
if (ZSTD_isError(bytes)) {
throw internal_error();
}
return StringRef(arena, StringRef(dest.get(), bytes));
}
#endif
throw internal_error(); // We should never get here
}
int CompressionUtils::getDefaultCompressionLevel(CompressionFilter filter) {
checkFilterSupported(filter);
if (filter == CompressionFilter::NONE) {
return -1;
}
#ifdef ZSTD_LIB_SUPPORTED
if (filter == CompressionFilter::ZSTD) {
// optimize for high speed compression, larger levels have a high cpu cost and not much compression ratio
// improvement, according to benchmarks
return ZSTD_COMPRESSION_LEVEL_1;
}
#endif
throw internal_error(); // We should never get here
}
CompressionFilter CompressionUtils::getRandomFilter() {
ASSERT_GE(supportedFilters.size(), 1);
std::vector<CompressionFilter> filters;
filters.insert(filters.end(), CompressionUtils::supportedFilters.begin(), CompressionUtils::supportedFilters.end());
ASSERT_GE(filters.size(), 1);
CompressionFilter res;
if (filters.size() == 1) {
res = filters[0];
} else {
int idx = deterministicRandom()->randomInt(0, filters.size());
res = filters[idx];
}
ASSERT(supportedFilters.find(res) != supportedFilters.end());
return res;
}
// Only used to link unit tests
void forceLinkCompressionUtilsTest() {}
namespace {
void testCompression(CompressionFilter filter) {
Arena arena;
const int size = deterministicRandom()->randomInt(512, 1024);
Standalone<StringRef> uncompressed = makeString(size);
deterministicRandom()->randomBytes(mutateString(uncompressed), size);
Standalone<StringRef> compressed = CompressionUtils::compress(filter, uncompressed, arena);
ASSERT_NE(compressed.compare(uncompressed), 0);
StringRef verify = CompressionUtils::decompress(filter, compressed, arena);
ASSERT_EQ(verify.compare(uncompressed), 0);
}
void testCompression2(CompressionFilter filter) {
Arena arena;
const int size = deterministicRandom()->randomInt(512, 1024);
std::string s(size, 'x');
Standalone<StringRef> uncompressed = Standalone<StringRef>(StringRef(s));
printf("Size before: %d\n", (int)uncompressed.size());
Standalone<StringRef> compressed = CompressionUtils::compress(filter, uncompressed, arena);
ASSERT_NE(compressed.compare(uncompressed), 0);
printf("Size after: %d\n", (int)compressed.size());
// Assert compressed size is less than half.
ASSERT(compressed.size() * 2 < uncompressed.size());
StringRef verify = CompressionUtils::decompress(filter, compressed, arena);
ASSERT_EQ(verify.compare(uncompressed), 0);
}
} // namespace
TEST_CASE("/CompressionUtils/noCompression") {
Arena arena;
const int size = deterministicRandom()->randomInt(512, 1024);
Standalone<StringRef> uncompressed = makeString(size);
deterministicRandom()->randomBytes(mutateString(uncompressed), size);
Standalone<StringRef> compressed = CompressionUtils::compress(CompressionFilter::NONE, uncompressed, arena);
ASSERT_EQ(compressed.compare(uncompressed), 0);
StringRef verify = CompressionUtils::decompress(CompressionFilter::NONE, compressed, arena);
ASSERT_EQ(verify.compare(uncompressed), 0);
TraceEvent("NoCompressionDone");
return Void();
}
#ifdef ZSTD_LIB_SUPPORTED
TEST_CASE("/CompressionUtils/zstdCompression") {
testCompression(CompressionFilter::ZSTD);
TraceEvent("ZstdCompressionDone");
return Void();
}
TEST_CASE("/CompressionUtils/zstdCompression2") {
testCompression2(CompressionFilter::ZSTD);
TraceEvent("ZstdCompression2Done");
return Void();
}
#endif