blob: bcc28c5c675fdedda8238575af578b2badab09e9 [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"
Hal Canary8a001442018-09-19 11:31:27 -040015
Mike Klein79aea6a2018-06-11 10:45:26 -040016#include <new>
bsalomon@google.com4da34e32012-06-19 15:40:27 +000017
18// change this to 0 to compare GrMemoryPool to default new / delete
19#define OVERRIDE_NEW 1
20
bsalomon@google.com4da34e32012-06-19 15:40:27 +000021struct A {
22 int gStuff[10];
23#if OVERRIDE_NEW
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000024 void* operator new (size_t size) { return gBenchPool.allocate(size); }
25 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
bsalomon@google.com4da34e32012-06-19 15:40:27 +000026#endif
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000027 static GrMemoryPool gBenchPool;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000028};
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000029GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
bsalomon@google.com4da34e32012-06-19 15:40:27 +000030
31/**
32 * This benchmark creates and deletes objects in stack order
33 */
tfarinaf168b862014-06-19 12:32:29 -070034class GrMemoryPoolBenchStack : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000035public:
mtklein36352bf2015-03-25 18:17:31 -070036 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000037 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000038 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000039
bsalomon@google.com4da34e32012-06-19 15:40:27 +000040protected:
mtkleinf0599002015-07-13 06:18:39 -070041 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000042 return "grmemorypool_stack";
43 }
44
mtkleina1ebeb22015-10-01 09:43:39 -070045 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000046 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000047 enum {
48 kMaxObjects = 4 * (1 << 10),
49 };
50 A* objects[kMaxObjects];
51
benjaminwagner12634482016-03-31 06:13:22 -070052 // We delete if a random number [-1, 1] is < the thresh. Otherwise,
bsalomon@google.com4da34e32012-06-19 15:40:27 +000053 // we allocate. We start allocate-biased and ping-pong to delete-biased
benjaminwagner12634482016-03-31 06:13:22 -070054 SkScalar delThresh = -SK_ScalarHalf;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000055 const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000056 int s = 0;
57
58 int count = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000059 for (int i = 0; i < loops; i++, ++s) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000060 if (kSwitchThreshPeriod == s) {
61 delThresh = -delThresh;
62 s = 0;
63 }
benjaminwagner12634482016-03-31 06:13:22 -070064 SkScalar del = r.nextSScalar1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000065 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000066 (kMaxObjects == count || del < delThresh)) {
67 delete objects[count-1];
68 --count;
69 } else {
70 objects[count] = new A;
71 ++count;
72 }
73 }
74 for (int i = 0; i < count; ++i) {
75 delete objects[i];
76 }
77 }
78
79private:
tfarinaf168b862014-06-19 12:32:29 -070080 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000081};
82
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000083struct B {
84 int gStuff[10];
85#if OVERRIDE_NEW
86 void* operator new (size_t size) { return gBenchPool.allocate(size); }
87 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
88#endif
89 static GrMemoryPool gBenchPool;
90};
91GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
92
bsalomon@google.com4da34e32012-06-19 15:40:27 +000093/**
94 * This benchmark creates objects and deletes them in random order
95 */
tfarinaf168b862014-06-19 12:32:29 -070096class GrMemoryPoolBenchRandom : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000097public:
mtklein36352bf2015-03-25 18:17:31 -070098 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000099 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000100 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000101
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000102protected:
mtkleinf0599002015-07-13 06:18:39 -0700103 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000104 return "grmemorypool_random";
105 }
106
mtkleina1ebeb22015-10-01 09:43:39 -0700107 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000108 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000109 enum {
110 kMaxObjects = 4 * (1 << 10),
111 };
Ben Wagner145dbcd2016-11-03 14:40:50 -0400112 std::unique_ptr<B> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000113
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000114 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000115 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
halcanary96fcdcc2015-08-27 07:41:13 -0700116 if (nullptr == objects[idx].get()) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000117 objects[idx].reset(new B);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000118 } else {
mtklein852f15d2016-03-17 10:51:27 -0700119 objects[idx].reset();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000120 }
121 }
122 }
123
124private:
tfarinaf168b862014-06-19 12:32:29 -0700125 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000126};
127
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000128struct C {
129 int gStuff[10];
130#if OVERRIDE_NEW
131 void* operator new (size_t size) { return gBenchPool.allocate(size); }
132 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
133#endif
134 static GrMemoryPool gBenchPool;
135};
136GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
137
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000138/**
139 * This benchmark creates objects and deletes them in queue order
140 */
tfarinaf168b862014-06-19 12:32:29 -0700141class GrMemoryPoolBenchQueue : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000142 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000143 M = 4 * (1 << 10),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000144 };
145public:
mtklein36352bf2015-03-25 18:17:31 -0700146 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000147 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000148 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000149
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000150protected:
mtkleinf0599002015-07-13 06:18:39 -0700151 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000152 return "grmemorypool_queue";
153 }
154
mtkleina1ebeb22015-10-01 09:43:39 -0700155 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000156 SkRandom r;
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000157 C* objects[M];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000158 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000159 uint32_t count = r.nextRangeU(0, M-1);
160 for (uint32_t i = 0; i < count; i++) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000161 objects[i] = new C;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000162 }
163 for (uint32_t i = 0; i < count; i++) {
164 delete objects[i];
165 }
166 }
167 }
168
169private:
tfarinaf168b862014-06-19 12:32:29 -0700170 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000171};
172
173///////////////////////////////////////////////////////////////////////////////
174
mtklein@google.com410e6e82013-09-13 19:52:27 +0000175DEF_BENCH( return new GrMemoryPoolBenchStack(); )
176DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
177DEF_BENCH( return new GrMemoryPoolBenchQueue(); )