forked from pmem/CacheLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPtrCompressionBench.cpp
More file actions
158 lines (133 loc) · 5.44 KB
/
PtrCompressionBench.cpp
File metadata and controls
158 lines (133 loc) · 5.44 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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 <folly/Benchmark.h>
#include <folly/init/Init.h>
#include <set>
#include <vector>
#include "cachelib/allocator/Util.h"
#include "cachelib/allocator/memory/MemoryAllocator.h"
using namespace facebook::cachelib;
using CompressedPtr = MemoryAllocator::CompressedPtr;
using AllocPair = std::pair<void*, CompressedPtr>;
namespace {
const unsigned int numPools = 4;
std::unique_ptr<MemoryAllocator> m;
std::vector<AllocPair> validAllocs;
std::vector<AllocPair> validAllocsAlt;
std::set<uint32_t> getAllocSizes() {
// defaults from tao for allocation sizes.
constexpr double factor = 1.25;
return util::generateAllocSizes(factor);
}
void buildAllocs(size_t poolSize) {
std::unordered_map<PoolId, const std::set<uint32_t>> pools;
std::string poolName = "foo";
for (unsigned int i = 0; i < numPools; i++) {
auto sizes = getAllocSizes();
auto pid =
m->addPool(poolName + folly::to<std::string>(i), poolSize, sizes);
pools.insert({pid, sizes});
}
auto makeAllocs = [](PoolId pid, MemoryAllocator* ma,
const std::set<uint32_t>& sizes) {
unsigned int numAllocations = 0;
do {
numAllocations = 0;
for (const auto size : sizes) {
void* alloc = ma->allocate(pid, size);
XDCHECK_GE(size, CompressedPtr::getMinAllocSize());
if (alloc != nullptr) {
validAllocs.push_back(
{alloc, ma->compress(alloc, false /* isMultiTiered */)});
validAllocsAlt.push_back({alloc, ma->compressAlt(alloc)});
numAllocations++;
}
}
} while (numAllocations > 0);
};
for (auto pool : pools) {
makeAllocs(pool.first, m.get(), pool.second);
}
}
} // namespace
BENCHMARK(CompressionAlt) {
for (const auto& alloc : validAllocsAlt) {
CompressedPtr c = m->compressAlt(alloc.first);
folly::doNotOptimizeAway(c);
}
}
BENCHMARK_RELATIVE(Compression) {
for (const auto& alloc : validAllocs) {
CompressedPtr c = m->compress(alloc.first, false /* isMultiTiered */);
folly::doNotOptimizeAway(c);
}
}
BENCHMARK(DeCompressAlt) {
for (const auto& alloc : validAllocsAlt) {
void* ptr = m->unCompressAlt(alloc.second);
folly::doNotOptimizeAway(ptr);
}
}
BENCHMARK_RELATIVE(DeCompress) {
for (const auto& alloc : validAllocs) {
void* ptr = m->unCompress(alloc.second, false /* isMultiTiered */);
folly::doNotOptimizeAway(ptr);
}
}
int main(int argc, char** argv) {
folly::init(&argc, &argv);
auto allocSizes = getAllocSizes();
// create enough memory for all the pools and alloc classes.
const size_t poolSize = allocSizes.size() * 2 * Slab::kSize;
// allocate enough memory for all the pools plus slab headers.
const size_t totalSize = numPools * poolSize + 2 * Slab::kSize;
MemoryAllocator::Config c(allocSizes, false /* enableZeroedSlabAllocs */,
true /* disableCoredump */, true /* lockMemory */);
m = std::make_unique<MemoryAllocator>(c, totalSize);
buildAllocs(poolSize);
folly::runBenchmarks();
return 0;
}
/*
Current results with -bm_min_iters=10000 on
CLANG and inlining
============================================================================
cachelib/allocator/benchmarks/PtrCompressionBench.cpprelative time/iter iters/s
============================================================================
Compression 2.36us 423.14K
DeCompress 1.41us 707.58K
CompressionAlt 1.30us 771.84K
DeCompressAlt 4.55us 219.96K
============================================================================**
GCC without inlining
============================================================================
cachelib/allocator/benchmarks/PtrCompressionBench.cpprelative time/iter iters/s
============================================================================
Compression 2.25us 443.80K
DeCompress 832.93ns 1.20M
CompressionAlt 829.69ns 1.21M
DeCompressAlt 4.62us 216.64K
============================================================================
GCC with always inlining
============================================================================
cachelib/allocator/benchmarks/PtrCompressionBench.cpprelative time/iter iters/s
============================================================================
Compression 3.34us 299.13K
DeCompress 752.00ns 1.33M
CompressionAlt 283.99ns 3.52M
DeCompressAlt 6.31us 158.42K
============================================================================
*/