2012-06-19 15:40:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2015-07-13 13:18:39 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
|
|
|
#include "include/private/SkTemplates.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/gpu/GrMemoryPool.h"
|
2018-09-19 15:31:27 +00:00
|
|
|
|
2018-06-11 14:45:26 +00:00
|
|
|
#include <new>
|
2012-06-19 15:40:27 +00:00
|
|
|
|
|
|
|
// change this to 0 to compare GrMemoryPool to default new / delete
|
|
|
|
#define OVERRIDE_NEW 1
|
|
|
|
|
|
|
|
struct A {
|
|
|
|
int gStuff[10];
|
|
|
|
#if OVERRIDE_NEW
|
2019-12-12 15:58:47 +00:00
|
|
|
void* operator new(size_t size) { return gBenchPool->allocate(size); }
|
|
|
|
void operator delete(void* mem) {
|
|
|
|
if (mem) {
|
|
|
|
return gBenchPool->release(mem);
|
|
|
|
}
|
|
|
|
}
|
2012-06-19 15:40:27 +00:00
|
|
|
#endif
|
2019-12-12 15:58:47 +00:00
|
|
|
static std::unique_ptr<GrMemoryPool> gBenchPool;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
2019-12-12 15:58:47 +00:00
|
|
|
std::unique_ptr<GrMemoryPool> A::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
|
2012-06-19 15:40:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This benchmark creates and deletes objects in stack order
|
|
|
|
*/
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrMemoryPoolBenchStack : public Benchmark {
|
2012-06-19 15:40:27 +00:00
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
protected:
|
2015-07-13 13:18:39 +00:00
|
|
|
const char* onGetName() override {
|
2012-06-19 15:40:27 +00:00
|
|
|
return "grmemorypool_stack";
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom r;
|
2012-06-19 15:40:27 +00:00
|
|
|
enum {
|
|
|
|
kMaxObjects = 4 * (1 << 10),
|
|
|
|
};
|
|
|
|
A* objects[kMaxObjects];
|
|
|
|
|
2016-03-31 13:13:22 +00:00
|
|
|
// We delete if a random number [-1, 1] is < the thresh. Otherwise,
|
2012-06-19 15:40:27 +00:00
|
|
|
// we allocate. We start allocate-biased and ping-pong to delete-biased
|
2016-03-31 13:13:22 +00:00
|
|
|
SkScalar delThresh = -SK_ScalarHalf;
|
2013-12-03 18:17:16 +00:00
|
|
|
const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
|
2012-06-19 15:40:27 +00:00
|
|
|
int s = 0;
|
|
|
|
|
|
|
|
int count = 0;
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++, ++s) {
|
2012-06-19 15:40:27 +00:00
|
|
|
if (kSwitchThreshPeriod == s) {
|
|
|
|
delThresh = -delThresh;
|
|
|
|
s = 0;
|
|
|
|
}
|
2016-03-31 13:13:22 +00:00
|
|
|
SkScalar del = r.nextSScalar1();
|
2012-08-23 18:09:54 +00:00
|
|
|
if (count &&
|
2012-06-19 15:40:27 +00:00
|
|
|
(kMaxObjects == count || del < delThresh)) {
|
|
|
|
delete objects[count-1];
|
|
|
|
--count;
|
|
|
|
} else {
|
|
|
|
objects[count] = new A;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
delete objects[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
2014-02-26 23:01:57 +00:00
|
|
|
struct B {
|
|
|
|
int gStuff[10];
|
|
|
|
#if OVERRIDE_NEW
|
2019-12-12 15:58:47 +00:00
|
|
|
void* operator new(size_t size) { return gBenchPool->allocate(size); }
|
|
|
|
void operator delete(void* mem) {
|
|
|
|
if (mem) {
|
|
|
|
return gBenchPool->release(mem);
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 23:01:57 +00:00
|
|
|
#endif
|
2019-12-12 15:58:47 +00:00
|
|
|
static std::unique_ptr<GrMemoryPool> gBenchPool;
|
2014-02-26 23:01:57 +00:00
|
|
|
};
|
2019-12-12 15:58:47 +00:00
|
|
|
std::unique_ptr<GrMemoryPool> B::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
|
2014-02-26 23:01:57 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
/**
|
|
|
|
* This benchmark creates objects and deletes them in random order
|
|
|
|
*/
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrMemoryPoolBenchRandom : public Benchmark {
|
2012-06-19 15:40:27 +00:00
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
protected:
|
2015-07-13 13:18:39 +00:00
|
|
|
const char* onGetName() override {
|
2012-06-19 15:40:27 +00:00
|
|
|
return "grmemorypool_random";
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom r;
|
2012-06-19 15:40:27 +00:00
|
|
|
enum {
|
|
|
|
kMaxObjects = 4 * (1 << 10),
|
|
|
|
};
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<B> objects[kMaxObjects];
|
2012-06-19 15:40:27 +00:00
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2012-06-19 15:40:27 +00:00
|
|
|
uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == objects[idx].get()) {
|
2014-02-26 23:01:57 +00:00
|
|
|
objects[idx].reset(new B);
|
2012-06-19 15:40:27 +00:00
|
|
|
} else {
|
2016-03-17 17:51:27 +00:00
|
|
|
objects[idx].reset();
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
2014-02-26 23:01:57 +00:00
|
|
|
struct C {
|
|
|
|
int gStuff[10];
|
|
|
|
#if OVERRIDE_NEW
|
2019-12-12 15:58:47 +00:00
|
|
|
void* operator new(size_t size) { return gBenchPool->allocate(size); }
|
|
|
|
void operator delete(void* mem) {
|
|
|
|
if (mem) {
|
|
|
|
return gBenchPool->release(mem);
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 23:01:57 +00:00
|
|
|
#endif
|
2019-12-12 15:58:47 +00:00
|
|
|
static std::unique_ptr<GrMemoryPool> gBenchPool;
|
2014-02-26 23:01:57 +00:00
|
|
|
};
|
2019-12-12 15:58:47 +00:00
|
|
|
std::unique_ptr<GrMemoryPool> C::gBenchPool = GrMemoryPool::Make(10 * (1 << 10), 10 * (1 << 10));
|
2014-02-26 23:01:57 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
/**
|
|
|
|
* This benchmark creates objects and deletes them in queue order
|
|
|
|
*/
|
2014-06-19 19:32:29 +00:00
|
|
|
class GrMemoryPoolBenchQueue : public Benchmark {
|
2012-06-19 15:40:27 +00:00
|
|
|
enum {
|
2013-09-10 19:23:38 +00:00
|
|
|
M = 4 * (1 << 10),
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
protected:
|
2015-07-13 13:18:39 +00:00
|
|
|
const char* onGetName() override {
|
2012-06-19 15:40:27 +00:00
|
|
|
return "grmemorypool_queue";
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom r;
|
2014-02-26 23:01:57 +00:00
|
|
|
C* objects[M];
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2012-06-19 15:40:27 +00:00
|
|
|
uint32_t count = r.nextRangeU(0, M-1);
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2014-02-26 23:01:57 +00:00
|
|
|
objects[i] = new C;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
delete objects[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new GrMemoryPoolBenchStack(); )
|
|
|
|
DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
|
|
|
|
DEF_BENCH( return new GrMemoryPoolBenchQueue(); )
|