blob: 290225dacb3bf9bb6348d4fd496ae75a3352983c [file] [log] [blame]
bsalomon@google.com4da34e32012-06-19 15:40:27 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
Michael Ludwig9abd5682020-01-08 13:50:42 -05009#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/utils/SkRandom.h"
11#include "src/gpu/GrMemoryPool.h"
Hal Canary8a001442018-09-19 11:31:27 -040012
Michael Ludwig9abd5682020-01-08 13:50:42 -050013#include <type_traits>
Michael Ludwigdff4c1d2020-01-08 15:53:59 +000014
Michael Ludwig9abd5682020-01-08 13:50:42 -050015namespace {
Michael Ludwigdff4c1d2020-01-08 15:53:59 +000016
Michael Ludwig9abd5682020-01-08 13:50:42 -050017// sizeof is a multiple of GrMemoryPool::kAlignment for 4, 8, or 16 byte alignment
Mike Kleinfb5850f2020-11-09 15:50:37 -060018struct alignas(GrMemoryPool::kAlignment) Aligned {
19 char buf[32];
20};
Michael Ludwig9abd5682020-01-08 13:50:42 -050021static_assert(sizeof(Aligned) == 32);
22static_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 Kleinfb5850f2020-11-09 15:50:37 -060026struct alignas(2) Unaligned {
27 char buf[30];
28};
Michael Ludwig9abd5682020-01-08 13:50:42 -050029static_assert(sizeof(Unaligned) == 30);
30static_assert(sizeof(Unaligned) % GrMemoryPool::kAlignment != 0);
31
32// When max_align_t == 16, 8, or 4 the padded Unaligned will also be 32
33static_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.
37typedef void (*RunBenchProc)(GrMemoryPool*, int);
38
John Stilesa6841be2020-08-06 14:11:56 -040039} // namespace
Michael Ludwig9abd5682020-01-08 13:50:42 -050040
41// N objects are created, and then destroyed in reverse order (fully unwinding the cursor within
42// each block of the memory pool).
43template <typename T>
44static 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 Ludwigdff4c1d2020-01-08 15:53:59 +000051 }
Michael Ludwig9abd5682020-01-08 13:50:42 -050052 // 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 Ludwigdff4c1d2020-01-08 15:53:59 +000056 } else {
Michael Ludwig9abd5682020-01-08 13:50:42 -050057 delete objs[j];
Michael Ludwigdff4c1d2020-01-08 15:53:59 +000058 }
59 }
Michael Ludwig9abd5682020-01-08 13:50:42 -050060
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).
67template <typename T>
68static 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 Ludwigdff4c1d2020-01-08 15:53:59 +000075 }
Michael Ludwig9abd5682020-01-08 13:50:42 -050076 // Pop N objects off in FIFO order
77 for (int j = 0; j < kMaxObjects; ++j) {
78 if (pool) {
79 pool->release(objs[j]);
Michael Ludwigdff4c1d2020-01-08 15:53:59 +000080 } else {
Michael Ludwig9abd5682020-01-08 13:50:42 -050081 delete objs[j];
Michael Ludwigdff4c1d2020-01-08 15:53:59 +000082 }
83 }
Michael Ludwig9abd5682020-01-08 13:50:42 -050084
85 // Everything has been cleaned up for the next loop
Michael Ludwigdff4c1d2020-01-08 15:53:59 +000086 }
Michael Ludwig9abd5682020-01-08 13:50:42 -050087}
Michael Ludwigf811fc32020-01-07 16:29:14 -050088
Michael Ludwig9abd5682020-01-08 13:50:42 -050089// N objects are created and immediately destroyed, so space at the start of the pool should be
90// immediately reclaimed.
91template <typename T>
92static 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 Ludwigdff4c1d2020-01-08 15:53:59 +0000105 }
Michael Ludwigdff4c1d2020-01-08 15:53:59 +0000106
Michael Ludwig9abd5682020-01-08 13:50:42 -0500107 // Everything has been cleaned up for the next loop
108 }
109}
110
111// N object creations and destructions are invoked in random order.
112template <typename T>
113static 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 Ludwigdff4c1d2020-01-08 15:53:59 +0000128 };
Michael Ludwig9abd5682020-01-08 13:50:42 -0500129
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
155class GrMemoryPoolBench : public Benchmark {
Michael Ludwigdff4c1d2020-01-08 15:53:59 +0000156public:
Michael Ludwig9abd5682020-01-08 13:50:42 -0500157 GrMemoryPoolBench(const char* name, RunBenchProc proc, int poolSize)
158 : fPoolSize(poolSize)
159 , fProc(proc) {
160 fName.printf("grmemorypool_%s", name);
161 }
162
Michael Ludwigdff4c1d2020-01-08 15:53:59 +0000163 bool isSuitableFor(Backend backend) override {
164 return backend == kNonRendering_Backend;
165 }
166
167protected:
168 const char* onGetName() override {
Michael Ludwig9abd5682020-01-08 13:50:42 -0500169 return fName.c_str();
Michael Ludwigdff4c1d2020-01-08 15:53:59 +0000170 }
171
172 void onDraw(int loops, SkCanvas*) override {
Michael Ludwig9abd5682020-01-08 13:50:42 -0500173 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 Ludwigdff4c1d2020-01-08 15:53:59 +0000179 }
180
Michael Ludwig9abd5682020-01-08 13:50:42 -0500181 SkString fName;
182 int fPoolSize;
183 RunBenchProc fProc;
184
John Stiles7571f9e2020-09-02 22:42:33 -0400185 using INHERITED = Benchmark;
Michael Ludwigdff4c1d2020-01-08 15:53:59 +0000186};
187
Michael Ludwig9abd5682020-01-08 13:50:42 -0500188///////////////////////////////////////////////////////////////////////////////////////////////////
Michael Ludwigdff4c1d2020-01-08 15:53:59 +0000189
Michael Ludwig9abd5682020-01-08 13:50:42 -0500190static const int kLargePool = 10 * (1 << 10);
191static const int kSmallPool = GrMemoryPool::kMinAllocationSize;
192
193DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_lg", run_stack<Aligned>, kLargePool); )
194DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_sm", run_stack<Aligned>, kSmallPool); )
195DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_ref", run_stack<Aligned>, 0); )
196DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_lg", run_stack<Unaligned>, kLargePool); )
197DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_sm", run_stack<Unaligned>, kSmallPool); )
198DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_ref", run_stack<Unaligned>, 0); )
199
200DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_lg", run_queue<Aligned>, kLargePool); )
201DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_sm", run_queue<Aligned>, kSmallPool); )
202DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_ref", run_queue<Aligned>, 0); )
203DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_lg", run_queue<Unaligned>, kLargePool); )
204DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_sm", run_queue<Unaligned>, kSmallPool); )
205DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_ref", run_queue<Unaligned>, 0); )
206
207DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_lg", run_pushpop<Aligned>, kLargePool); )
208DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_sm", run_pushpop<Aligned>, kSmallPool); )
209// DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_ref", run_pushpop<Aligned>, 0); )
210DEF_BENCH( return new GrMemoryPoolBench("pushpop_unaligned_lg", run_pushpop<Unaligned>, kLargePool); )
211DEF_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
215DEF_BENCH( return new GrMemoryPoolBench("random_aligned_lg", run_random<Aligned>, kLargePool); )
216DEF_BENCH( return new GrMemoryPoolBench("random_aligned_sm", run_random<Aligned>, kSmallPool); )
217DEF_BENCH( return new GrMemoryPoolBench("random_aligned_ref", run_random<Aligned>, 0); )
218DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_lg", run_random<Unaligned>, kLargePool); )
219DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_sm", run_random<Unaligned>, kSmallPool); )
220DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_ref", run_random<Unaligned>, 0); )