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