blob: f2486a58438bb137a62adb7ae9d9b54e4699fe41 [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 "include/core/SkTypes.h"
mtkleinf0599002015-07-13 06:18:39 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "bench/Benchmark.h"
11#include "include/private/SkTDArray.h"
12#include "include/private/SkTemplates.h"
13#include "include/utils/SkRandom.h"
14#include "src/gpu/GrMemoryPool.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
Brian Salomon6986c652019-12-12 10:58:47 -050024 void* operator new(size_t size) { return gBenchPool->allocate(size); }
25 void operator delete(void* mem) {
26 if (mem) {
27 return gBenchPool->release(mem);
28 }
29 }
bsalomon@google.com4da34e32012-06-19 15:40:27 +000030#endif
Brian Salomon6986c652019-12-12 10:58:47 -050031 static std::unique_ptr<GrMemoryPool> gBenchPool;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000032};
Brian Salomon6986c652019-12-12 10:58:47 -050033std::unique_ptr<GrMemoryPool> A::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
bsalomon@google.com4da34e32012-06-19 15:40:27 +000034
35/**
36 * This benchmark creates and deletes objects in stack order
37 */
tfarinaf168b862014-06-19 12:32:29 -070038class GrMemoryPoolBenchStack : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000039public:
mtklein36352bf2015-03-25 18:17:31 -070040 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000041 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000042 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000043
bsalomon@google.com4da34e32012-06-19 15:40:27 +000044protected:
mtkleinf0599002015-07-13 06:18:39 -070045 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000046 return "grmemorypool_stack";
47 }
48
mtkleina1ebeb22015-10-01 09:43:39 -070049 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000050 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000051 enum {
52 kMaxObjects = 4 * (1 << 10),
53 };
54 A* objects[kMaxObjects];
55
benjaminwagner12634482016-03-31 06:13:22 -070056 // We delete if a random number [-1, 1] is < the thresh. Otherwise,
bsalomon@google.com4da34e32012-06-19 15:40:27 +000057 // we allocate. We start allocate-biased and ping-pong to delete-biased
benjaminwagner12634482016-03-31 06:13:22 -070058 SkScalar delThresh = -SK_ScalarHalf;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000059 const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000060 int s = 0;
61
62 int count = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000063 for (int i = 0; i < loops; i++, ++s) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000064 if (kSwitchThreshPeriod == s) {
65 delThresh = -delThresh;
66 s = 0;
67 }
benjaminwagner12634482016-03-31 06:13:22 -070068 SkScalar del = r.nextSScalar1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000070 (kMaxObjects == count || del < delThresh)) {
71 delete objects[count-1];
72 --count;
73 } else {
74 objects[count] = new A;
75 ++count;
76 }
77 }
78 for (int i = 0; i < count; ++i) {
79 delete objects[i];
80 }
81 }
82
83private:
tfarinaf168b862014-06-19 12:32:29 -070084 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000085};
86
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000087struct B {
88 int gStuff[10];
89#if OVERRIDE_NEW
Brian Salomon6986c652019-12-12 10:58:47 -050090 void* operator new(size_t size) { return gBenchPool->allocate(size); }
91 void operator delete(void* mem) {
92 if (mem) {
93 return gBenchPool->release(mem);
94 }
95 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000096#endif
Brian Salomon6986c652019-12-12 10:58:47 -050097 static std::unique_ptr<GrMemoryPool> gBenchPool;
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000098};
Brian Salomon6986c652019-12-12 10:58:47 -050099std::unique_ptr<GrMemoryPool> B::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000100
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000101/**
102 * This benchmark creates objects and deletes them in random order
103 */
tfarinaf168b862014-06-19 12:32:29 -0700104class GrMemoryPoolBenchRandom : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000105public:
mtklein36352bf2015-03-25 18:17:31 -0700106 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000107 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000108 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000109
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000110protected:
mtkleinf0599002015-07-13 06:18:39 -0700111 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000112 return "grmemorypool_random";
113 }
114
mtkleina1ebeb22015-10-01 09:43:39 -0700115 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000116 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000117 enum {
118 kMaxObjects = 4 * (1 << 10),
119 };
Ben Wagner145dbcd2016-11-03 14:40:50 -0400120 std::unique_ptr<B> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000121
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000122 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000123 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
halcanary96fcdcc2015-08-27 07:41:13 -0700124 if (nullptr == objects[idx].get()) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000125 objects[idx].reset(new B);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000126 } else {
mtklein852f15d2016-03-17 10:51:27 -0700127 objects[idx].reset();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000128 }
129 }
130 }
131
132private:
tfarinaf168b862014-06-19 12:32:29 -0700133 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000134};
135
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000136struct C {
137 int gStuff[10];
138#if OVERRIDE_NEW
Brian Salomon6986c652019-12-12 10:58:47 -0500139 void* operator new(size_t size) { return gBenchPool->allocate(size); }
140 void operator delete(void* mem) {
141 if (mem) {
142 return gBenchPool->release(mem);
143 }
144 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000145#endif
Brian Salomon6986c652019-12-12 10:58:47 -0500146 static std::unique_ptr<GrMemoryPool> gBenchPool;
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000147};
Brian Salomon6986c652019-12-12 10:58:47 -0500148std::unique_ptr<GrMemoryPool> C::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000149
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000150/**
151 * This benchmark creates objects and deletes them in queue order
152 */
tfarinaf168b862014-06-19 12:32:29 -0700153class GrMemoryPoolBenchQueue : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000154 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000155 M = 4 * (1 << 10),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000156 };
157public:
mtklein36352bf2015-03-25 18:17:31 -0700158 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000159 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000160 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000161
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000162protected:
mtkleinf0599002015-07-13 06:18:39 -0700163 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000164 return "grmemorypool_queue";
165 }
166
mtkleina1ebeb22015-10-01 09:43:39 -0700167 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000168 SkRandom r;
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000169 C* objects[M];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000170 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000171 uint32_t count = r.nextRangeU(0, M-1);
172 for (uint32_t i = 0; i < count; i++) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000173 objects[i] = new C;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000174 }
175 for (uint32_t i = 0; i < count; i++) {
176 delete objects[i];
177 }
178 }
179 }
180
181private:
tfarinaf168b862014-06-19 12:32:29 -0700182 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000183};
184
185///////////////////////////////////////////////////////////////////////////////
186
mtklein@google.com410e6e82013-09-13 19:52:27 +0000187DEF_BENCH( return new GrMemoryPoolBenchStack(); )
188DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
189DEF_BENCH( return new GrMemoryPoolBenchQueue(); )