blob: a71de9767cd2cde47a0a501c032ddfd486639ab2 [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
mtkleinf0599002015-07-13 06:18:39 -07008#include "SkTypes.h"
9
tfarinaf168b862014-06-19 12:32:29 -070010#include "Benchmark.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000011#include "GrMemoryPool.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000012#include "SkRandom.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000013#include "SkTDArray.h"
reed@google.com9d1cff12013-04-18 18:43:26 +000014#include "SkTemplates.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040015#include <new>
bsalomon@google.com4da34e32012-06-19 15:40:27 +000016
17// change this to 0 to compare GrMemoryPool to default new / delete
18#define OVERRIDE_NEW 1
19
bsalomon@google.com4da34e32012-06-19 15:40:27 +000020struct A {
21 int gStuff[10];
22#if OVERRIDE_NEW
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000023 void* operator new (size_t size) { return gBenchPool.allocate(size); }
24 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
bsalomon@google.com4da34e32012-06-19 15:40:27 +000025#endif
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000026 static GrMemoryPool gBenchPool;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000027};
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000028GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
bsalomon@google.com4da34e32012-06-19 15:40:27 +000029
30/**
31 * This benchmark creates and deletes objects in stack order
32 */
tfarinaf168b862014-06-19 12:32:29 -070033class GrMemoryPoolBenchStack : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000034public:
mtklein36352bf2015-03-25 18:17:31 -070035 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000036 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000037 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038
bsalomon@google.com4da34e32012-06-19 15:40:27 +000039protected:
mtkleinf0599002015-07-13 06:18:39 -070040 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000041 return "grmemorypool_stack";
42 }
43
mtkleina1ebeb22015-10-01 09:43:39 -070044 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000045 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000046 enum {
47 kMaxObjects = 4 * (1 << 10),
48 };
49 A* objects[kMaxObjects];
50
benjaminwagner12634482016-03-31 06:13:22 -070051 // We delete if a random number [-1, 1] is < the thresh. Otherwise,
bsalomon@google.com4da34e32012-06-19 15:40:27 +000052 // we allocate. We start allocate-biased and ping-pong to delete-biased
benjaminwagner12634482016-03-31 06:13:22 -070053 SkScalar delThresh = -SK_ScalarHalf;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000054 const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000055 int s = 0;
56
57 int count = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 for (int i = 0; i < loops; i++, ++s) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000059 if (kSwitchThreshPeriod == s) {
60 delThresh = -delThresh;
61 s = 0;
62 }
benjaminwagner12634482016-03-31 06:13:22 -070063 SkScalar del = r.nextSScalar1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000065 (kMaxObjects == count || del < delThresh)) {
66 delete objects[count-1];
67 --count;
68 } else {
69 objects[count] = new A;
70 ++count;
71 }
72 }
73 for (int i = 0; i < count; ++i) {
74 delete objects[i];
75 }
76 }
77
78private:
tfarinaf168b862014-06-19 12:32:29 -070079 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000080};
81
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000082struct B {
83 int gStuff[10];
84#if OVERRIDE_NEW
85 void* operator new (size_t size) { return gBenchPool.allocate(size); }
86 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
87#endif
88 static GrMemoryPool gBenchPool;
89};
90GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
91
bsalomon@google.com4da34e32012-06-19 15:40:27 +000092/**
93 * This benchmark creates objects and deletes them in random order
94 */
tfarinaf168b862014-06-19 12:32:29 -070095class GrMemoryPoolBenchRandom : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000096public:
mtklein36352bf2015-03-25 18:17:31 -070097 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000098 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000099 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000100
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000101protected:
mtkleinf0599002015-07-13 06:18:39 -0700102 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000103 return "grmemorypool_random";
104 }
105
mtkleina1ebeb22015-10-01 09:43:39 -0700106 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000107 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000108 enum {
109 kMaxObjects = 4 * (1 << 10),
110 };
Ben Wagner145dbcd2016-11-03 14:40:50 -0400111 std::unique_ptr<B> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000112
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000113 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000114 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
halcanary96fcdcc2015-08-27 07:41:13 -0700115 if (nullptr == objects[idx].get()) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000116 objects[idx].reset(new B);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000117 } else {
mtklein852f15d2016-03-17 10:51:27 -0700118 objects[idx].reset();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000119 }
120 }
121 }
122
123private:
tfarinaf168b862014-06-19 12:32:29 -0700124 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000125};
126
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000127struct C {
128 int gStuff[10];
129#if OVERRIDE_NEW
130 void* operator new (size_t size) { return gBenchPool.allocate(size); }
131 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
132#endif
133 static GrMemoryPool gBenchPool;
134};
135GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
136
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000137/**
138 * This benchmark creates objects and deletes them in queue order
139 */
tfarinaf168b862014-06-19 12:32:29 -0700140class GrMemoryPoolBenchQueue : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000141 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000142 M = 4 * (1 << 10),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000143 };
144public:
mtklein36352bf2015-03-25 18:17:31 -0700145 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000146 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000147 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000148
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000149protected:
mtkleinf0599002015-07-13 06:18:39 -0700150 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000151 return "grmemorypool_queue";
152 }
153
mtkleina1ebeb22015-10-01 09:43:39 -0700154 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000155 SkRandom r;
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000156 C* objects[M];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000157 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000158 uint32_t count = r.nextRangeU(0, M-1);
159 for (uint32_t i = 0; i < count; i++) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000160 objects[i] = new C;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000161 }
162 for (uint32_t i = 0; i < count; i++) {
163 delete objects[i];
164 }
165 }
166 }
167
168private:
tfarinaf168b862014-06-19 12:32:29 -0700169 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000170};
171
172///////////////////////////////////////////////////////////////////////////////
173
mtklein@google.com410e6e82013-09-13 19:52:27 +0000174DEF_BENCH( return new GrMemoryPoolBenchStack(); )
175DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
176DEF_BENCH( return new GrMemoryPoolBenchQueue(); )