Flutter Engine
The Flutter Engine
GrMemoryPoolBench.cpp
Go to the documentation of this file.
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"
11
12#include <type_traits>
13
14namespace {
15
16// sizeof is a multiple of GrMemoryPool::kAlignment for 4, 8, or 16 byte alignment
17struct alignas(GrMemoryPool::kAlignment) Aligned {
18 char buf[32];
19};
20static_assert(sizeof(Aligned) == 32);
21static_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).
25struct alignas(2) Unaligned {
26 char buf[30];
27};
28static_assert(sizeof(Unaligned) == 30);
29static_assert(sizeof(Unaligned) % GrMemoryPool::kAlignment != 0);
30
31// When max_align_t == 16, 8, or 4 the padded Unaligned will also be 32
32static_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.
36typedef 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).
42template <typename T>
43static 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).
66template <typename T>
67static 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.
90template <typename T>
91static 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.
111template <typename T>
112static 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
155public:
156 GrMemoryPoolBench(const char* name, RunBenchProc proc, int poolSize)
157 : fPoolSize(poolSize)
158 , fProc(proc) {
159 fName.printf("grmemorypool_%s", name);
160 }
161
164 }
165
166protected:
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) {
175 } // else keep it null to test regular new/delete performance
176
177 fProc(pool.get(), loops);
178 }
179
182 RunBenchProc fProc;
183
185};
186
187///////////////////////////////////////////////////////////////////////////////////////////////////
188
189static const int kLargePool = 10 * (1 << 10);
191
192DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_lg", run_stack<Aligned>, kLargePool); )
193DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_sm", run_stack<Aligned>, kSmallPool); )
194DEF_BENCH( return new GrMemoryPoolBench("stack_aligned_ref", run_stack<Aligned>, 0); )
195DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_lg", run_stack<Unaligned>, kLargePool); )
196DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_sm", run_stack<Unaligned>, kSmallPool); )
197DEF_BENCH( return new GrMemoryPoolBench("stack_unaligned_ref", run_stack<Unaligned>, 0); )
198
199DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_lg", run_queue<Aligned>, kLargePool); )
200DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_sm", run_queue<Aligned>, kSmallPool); )
201DEF_BENCH( return new GrMemoryPoolBench("queue_aligned_ref", run_queue<Aligned>, 0); )
202DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_lg", run_queue<Unaligned>, kLargePool); )
203DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_sm", run_queue<Unaligned>, kSmallPool); )
204DEF_BENCH( return new GrMemoryPoolBench("queue_unaligned_ref", run_queue<Unaligned>, 0); )
205
206DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_lg", run_pushpop<Aligned>, kLargePool); )
207DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_sm", run_pushpop<Aligned>, kSmallPool); )
208// DEF_BENCH( return new GrMemoryPoolBench("pushpop_aligned_ref", run_pushpop<Aligned>, 0); )
209DEF_BENCH( return new GrMemoryPoolBench("pushpop_unaligned_lg", run_pushpop<Unaligned>, kLargePool); )
210DEF_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
214DEF_BENCH( return new GrMemoryPoolBench("random_aligned_lg", run_random<Aligned>, kLargePool); )
215DEF_BENCH( return new GrMemoryPoolBench("random_aligned_sm", run_random<Aligned>, kSmallPool); )
216DEF_BENCH( return new GrMemoryPoolBench("random_aligned_ref", run_random<Aligned>, 0); )
217DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_lg", run_random<Unaligned>, kLargePool); )
218DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_sm", run_random<Unaligned>, kSmallPool); )
219DEF_BENCH( return new GrMemoryPoolBench("random_unaligned_ref", run_random<Unaligned>, 0); )
AutoreleasePool pool
#define DEF_BENCH(code)
Definition: Benchmark.h:20
const char * backend
static void run_queue(GrMemoryPool *pool, int loops)
static void run_pushpop(GrMemoryPool *pool, int loops)
static const int kLargePool
static const int kSmallPool
static void run_stack(GrMemoryPool *pool, int loops)
static void run_random(GrMemoryPool *pool, int loops)
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition: SkAlign.h:33
const char * onGetName() override
void onDraw(int loops, SkCanvas *) override
GrMemoryPoolBench(const char *name, RunBenchProc proc, int poolSize)
bool isSuitableFor(Backend backend) override
static constexpr size_t kAlignment
Definition: GrMemoryPool.h:40
static std::unique_ptr< GrMemoryPool > Make(size_t preallocSize, size_t minAllocSize)
static constexpr size_t kMinAllocationSize
Definition: GrMemoryPool.h:44
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition: SkRandom.h:80
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
const char * c_str() const
Definition: SkString.h:133
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
#define T
Definition: precompiler.cc:65