| 1 |
/* |
| 2 |
* Copyright 2012 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "bench/Benchmark.h" |
| 9 |
#include "src/base/SkRandom.h" |
| 10 |
#include "src/gpu/ganesh/GrMemoryPool.h" |
| 11 |
|
| 12 |
#include <type_traits> |
| 13 |
|
| 14 |
namespace { |
| 15 |
|
| 16 |
// sizeof is a multiple of GrMemoryPool::kAlignment for 4, 8, or 16 byte alignment |
| 17 |
struct alignas(GrMemoryPool::kAlignment) Aligned { |
| 18 |
char buf[32]; |
| 19 |
}; |
| 20 |
static_assert(sizeof(Aligned) == 32); |
| 21 |
static_assert(sizeof(Aligned) % GrMemoryPool::kAlignment == 0); |
| 22 |
|
| 23 |
// sizeof is not a multiple of GrMemoryPool::kAlignment (will not be a multiple of max_align_t |
| 24 |
// if it's 4, 8, or 16, as desired). |
| 25 |
struct alignas(2) Unaligned { |
| 26 |
char buf[30]; |
| 27 |
}; |
| 28 |
static_assert(sizeof(Unaligned) == 30); |
| 29 |
static_assert(sizeof(Unaligned) % GrMemoryPool::kAlignment != 0); |
| 30 |
|
| 31 |
// When max_align_t == 16, 8, or 4 the padded Unaligned will also be 32 |
| 32 |
static_assert(SkAlignTo(sizeof(Unaligned), GrMemoryPool::kAlignment) == sizeof(Aligned)); |
| 33 |
|
| 34 |
// All benchmarks create and delete the same number of objects. The key difference is the order |
| 35 |
// of operations, the size of the objects being allocated, and the size of the pool. |
| 36 |
typedef void (*RunBenchProc)(GrMemoryPool*, int); |
| 37 |
|
| 38 |
} // namespace |
| 39 |
|
| 40 |
// N objects are created, and then destroyed in reverse order (fully unwinding the cursor within |
| 41 |
// each block of the memory pool). |
| 42 |
template <typename T> |
| 43 |
static void run_stack(GrMemoryPool* pool, int loops) { |
| 44 |
static const int kMaxObjects = 4 * (1 << 10); |
| 45 |
T* objs[kMaxObjects]; |
| 46 |
for (int i = 0; i < loops; ++i) { |
| 47 |
// Push N objects into the pool (or heap if pool is null) |
| 48 |
for (int j = 0; j < kMaxObjects; ++j) { |
| 49 |
objs[j] = pool ? (T*) pool->allocate(sizeof(T)) : new T; |
| 50 |
} |
| 51 |
// Pop N objects off in LIFO order |
| 52 |
for (int j = kMaxObjects - 1; j >= 0; --j) { |
| 53 |
if (pool) { |
| 54 |
pool->release(objs[j]); |
| 55 |
} else { |
| 56 |
delete objs[j]; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Everything has been cleaned up for the next loop |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// N objects are created, and then destroyed in creation order (is not able to unwind the cursor |
| 65 |
// within each block, but can reclaim the block once everything is destroyed). |
| 66 |
template <typename T> |
| 67 |
static void run_queue(GrMemoryPool* pool, int loops) { |
| 68 |
static const int kMaxObjects = 4 * (1 << 10); |
| 69 |
T* objs[kMaxObjects]; |
| 70 |
for (int i = 0; i < loops; ++i) { |
| 71 |
// Push N objects into the pool (or heap if pool is null) |
| 72 |
for (int j = 0; j < kMaxObjects; ++j) { |
| 73 |
objs[j] = pool ? (T*) pool->allocate(sizeof(T)) : new T; |
| 74 |
} |
| 75 |
// Pop N objects off in FIFO order |
| 76 |
for (int j = 0; j < kMaxObjects; ++j) { |
| 77 |
if (pool) { |
| 78 |
pool->release(objs[j]); |
| 79 |
} else { |
| 80 |
delete objs[j]; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// Everything has been cleaned up for the next loop |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// N objects are created and immediately destroyed, so space at the start of the pool should be |
| 89 |
// immediately reclaimed. |
| 90 |
template <typename T> |
| 91 |
static void run_pushpop(GrMemoryPool* pool, int loops) { |
| 92 |
static const int kMaxObjects = 4 * (1 << 10); |
| 93 |
T* objs[kMaxObjects]; |
| 94 |
for (int i = 0; i < loops; ++i) { |
| 95 |
// Push N objects into the pool (or heap if pool is null) |
| 96 |
for (int j = 0; j < kMaxObjects; ++j) { |
| 97 |
if (pool) { |
| 98 |
objs[j] = (T*) pool->allocate(sizeof(T)); |
| 99 |
pool->release(objs[j]); |
| 100 |
} else { |
| 101 |
objs[j] = new T; |
| 102 |
delete objs[j]; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// Everything has been cleaned up for the next loop |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// N object creations and destructions are invoked in random order. |
| 111 |
template <typename T> |
| 112 |
static void run_random(GrMemoryPool* pool, int loops) { |
| 113 |
static const int kMaxObjects = 4 * (1 << 10); |
| 114 |
T* objs[kMaxObjects]; |
| 115 |
for (int i = 0; i < kMaxObjects; ++i) { |
| 116 |
objs[i] = nullptr; |
| 117 |
} |
| 118 |
|
| 119 |
auto del = [&](int j) { |
| 120 |
// Delete |
| 121 |
if (pool) { |
| 122 |
pool->release(objs[j]); |
| 123 |
} else { |
| 124 |
delete objs[j]; |
| 125 |
} |
| 126 |
objs[j] = nullptr; |
| 127 |
}; |
| 128 |
|
| 129 |
SkRandom r; |
| 130 |
for (int i = 0; i < loops; ++i) { |
| 131 |
// Execute 2*kMaxObjects operations, which should average to N create and N destroy, |
| 132 |
// followed by a small number of remaining deletions. |
| 133 |
for (int j = 0; j < 2 * kMaxObjects; ++j) { |
| 134 |
int k = r.nextRangeU(0, kMaxObjects-1); |
| 135 |
if (objs[k]) { |
| 136 |
del(k); |
| 137 |
} else { |
| 138 |
// Create |
| 139 |
objs[k] = pool ? (T*) pool->allocate(sizeof(T)) : new T; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// Ensure everything is null for the next loop |
| 144 |
for (int j = 0; j < kMaxObjects; ++j) { |
| 145 |
if (objs[j]) { |
| 146 |
del(j); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 153 |
|
| 154 |
class GrMemoryPoolBench : public Benchmark { |
| 155 |
public: |
| 156 |
GrMemoryPoolBench(const char* name, RunBenchProc proc, int poolSize) |
| 157 |
: fPoolSize(poolSize) |
| 158 |
, fProc(proc) { |
| 159 |
fName.printf("grmemorypool_%s", name); |
| 160 |
} |
| 161 |
|
| 162 |
bool isSuitableFor(Backend backend) override { |
| 163 |
return backend == Backend::kNonRendering; |
| 164 |
} |
| 165 |
|
| 166 |
protected: |
| 167 |
const char* onGetName() override { |
| 168 |
return fName.c_str(); |
| 169 |
} |
| 170 |
|
| 171 |
void onDraw(int loops, SkCanvas*) override { |
| 172 |
std::unique_ptr<GrMemoryPool> pool; |
| 173 |
if (fPoolSize > 0) { |
| 174 |
pool = GrMemoryPool::Make(fPoolSize, fPoolSize); |
| 175 |
} // else keep it null to test regular new/delete performance |
| 176 |
|
| 177 |
fProc(pool.get(), loops); |
| 178 |
} |
| 179 |
|
| 180 |
SkString fName; |
| 181 |
int fPoolSize; |
| 182 |
RunBenchProc fProc; |
| 183 |
|
| 184 |
using INHERITED = Benchmark; |
| 185 |
}; |
| 186 |
|
| 187 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 188 |
|
| 189 |
static const int kLargePool = 10 * (1 << 10); |
| 190 |
static const int kSmallPool = GrMemoryPool::kMinAllocationSize; |
| 191 |
|
| 192 |
DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_lg", run_stack<Aligned>, kLargePool); ) |
| 193 |
DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_sm", run_stack<Aligned>, kSmallPool); ) |
| 194 |
DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_ref", run_stack<Aligned>, 0); ) |
| 195 |
DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_lg", run_stack<Unaligned>, kLargePool); ) |
| 196 |
DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_sm", run_stack<Unaligned>, kSmallPool); ) |
| 197 |
DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_ref", run_stack<Unaligned>, 0); ) |
| 198 |
|
| 199 |
DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_lg", run_queue<Aligned>, kLargePool); ) |
| 200 |
DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_sm", run_queue<Aligned>, kSmallPool); ) |
| 201 |
DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_ref", run_queue<Aligned>, 0); ) |
| 202 |
DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_lg", run_queue<Unaligned>, kLargePool); ) |
| 203 |
DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_sm", run_queue<Unaligned>, kSmallPool); ) |
| 204 |
DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_ref", run_queue<Unaligned>, 0); ) |
| 205 |
|
| 206 |
DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_lg", run_pushpop<Aligned>, kLargePool); ) |
| 207 |
DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_sm", run_pushpop<Aligned>, kSmallPool); ) |
| 208 |
// DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_ref", run_pushpop<Aligned>, 0); ) |
| 209 |
DEF_BENCH( return new GrMemoryPoolBench("pushpop_unaligned_lg", run_pushpop<Unaligned>, kLargePool); ) |
| 210 |
DEF_BENCH( return new GrMemoryPoolBench("pushpop_unaligned_sm", run_pushpop<Unaligned>, kSmallPool); ) |
| 211 |
// DEF_BENCH( return new GrMemoryPoolBench("pushpop_unaligned_ref", run_pushpop<Unaligned>, 0); ) |
| 212 |
// pushpop_x_ref are not meaningful because the compiler completely optimizes away new T; delete *. |
| 213 |
|
| 214 |
DEF_BENCH( return new GrMemoryPoolBench("random_aligned_lg", run_random<Aligned>, kLargePool); ) |
| 215 |
DEF_BENCH( return new GrMemoryPoolBench("random_aligned_sm", run_random<Aligned>, kSmallPool); ) |
| 216 |
DEF_BENCH( return new GrMemoryPoolBench("random_aligned_ref", run_random<Aligned>, 0); ) |
| 217 |
DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_lg", run_random<Unaligned>, kLargePool); ) |
| 218 |
DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_sm", run_random<Unaligned>, kSmallPool); ) |
| 219 |
DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_ref", run_random<Unaligned>, 0); ) |