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 "include/private/GrTypesPriv.h"
10 #include "include/utils/SkRandom.h"
11 #include "src/gpu/GrMemoryPool.h"
12
13 #include <type_traits>
14
15 namespace {
16
17 // sizeof is a multiple of GrMemoryPool::kAlignment for 4, 8, or 16 byte alignment
18 struct alignas(GrMemoryPool::kAlignment) Aligned {
19 char buf[32];
20 };
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).
26 struct alignas(2) Unaligned {
27 char buf[30];
28 };
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(SkAlignTo(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
39 } // namespace
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>
run_stack(GrMemoryPool * pool,int loops)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;
51 }
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]);
56 } else {
57 delete objs[j];
58 }
59 }
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>
run_queue(GrMemoryPool * pool,int loops)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;
75 }
76 // Pop N objects off in FIFO order
77 for (int j = 0; j < kMaxObjects; ++j) {
78 if (pool) {
79 pool->release(objs[j]);
80 } else {
81 delete objs[j];
82 }
83 }
84
85 // Everything has been cleaned up for the next loop
86 }
87 }
88
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>
run_pushpop(GrMemoryPool * pool,int loops)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 }
105 }
106
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>
run_random(GrMemoryPool * pool,int loops)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;
128 };
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 {
156 public:
GrMemoryPoolBench(const char * name,RunBenchProc proc,int poolSize)157 GrMemoryPoolBench(const char* name, RunBenchProc proc, int poolSize)
158 : fPoolSize(poolSize)
159 , fProc(proc) {
160 fName.printf("grmemorypool_%s", name);
161 }
162
isSuitableFor(Backend backend)163 bool isSuitableFor(Backend backend) override {
164 return backend == kNonRendering_Backend;
165 }
166
167 protected:
onGetName()168 const char* onGetName() override {
169 return fName.c_str();
170 }
171
onDraw(int loops,SkCanvas *)172 void onDraw(int loops, SkCanvas*) override {
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);
179 }
180
181 SkString fName;
182 int fPoolSize;
183 RunBenchProc fProc;
184
185 using INHERITED = Benchmark;
186 };
187
188 ///////////////////////////////////////////////////////////////////////////////////////////////////
189
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); )
221