2012-05-14 14:09:24 +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.
|
|
|
|
*/
|
2014-06-19 19:32:29 +00:00
|
|
|
#include <memory>
|
|
|
|
#include "Benchmark.h"
|
2012-05-16 18:21:56 +00:00
|
|
|
#include "SkRefCnt.h"
|
2012-05-14 14:09:24 +00:00
|
|
|
#include "SkThread.h"
|
2012-05-16 18:21:56 +00:00
|
|
|
#include "SkWeakRefCnt.h"
|
2012-05-14 14:09:24 +00:00
|
|
|
|
|
|
|
enum {
|
2013-09-10 19:23:38 +00:00
|
|
|
M = 2
|
2012-05-14 14:09:24 +00:00
|
|
|
};
|
|
|
|
|
2014-07-11 15:42:11 +00:00
|
|
|
class AtomicInc32 : public Benchmark {
|
|
|
|
public:
|
|
|
|
AtomicInc32() : fX(0) {}
|
|
|
|
|
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() {
|
|
|
|
return "atomic_inc_32";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(const int loops, SkCanvas*) {
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
|
|
|
sk_atomic_inc(&fX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t fX;
|
|
|
|
typedef Benchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AtomicInc64 : public Benchmark {
|
|
|
|
public:
|
|
|
|
AtomicInc64() : fX(0) {}
|
|
|
|
|
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() {
|
|
|
|
return "atomic_inc_64";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(const int loops, SkCanvas*) {
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
|
|
|
sk_atomic_inc(&fX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int64_t fX;
|
|
|
|
typedef Benchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class RefCntBench_Stack : public Benchmark {
|
2012-05-14 14:09:24 +00:00
|
|
|
public:
|
2013-11-21 06:21:58 +00:00
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
2012-05-14 14:09:24 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-05-14 14:09:24 +00:00
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() {
|
|
|
|
return "ref_cnt_stack";
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) {
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
2012-05-14 14:09:24 +00:00
|
|
|
SkRefCnt ref;
|
|
|
|
for (int j = 0; j < M; ++j) {
|
|
|
|
ref.ref();
|
|
|
|
ref.unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-05-14 14:09:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class PlacedRefCnt : public SkRefCnt {
|
|
|
|
public:
|
2012-06-21 20:25:03 +00:00
|
|
|
SK_DECLARE_INST_COUNT(PlacedRefCnt)
|
|
|
|
|
2012-05-14 14:09:24 +00:00
|
|
|
PlacedRefCnt() : SkRefCnt() { }
|
2013-03-05 18:50:01 +00:00
|
|
|
void operator delete(void*) { }
|
2012-06-21 20:25:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkRefCnt INHERITED;
|
2012-05-14 14:09:24 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class RefCntBench_Heap : public Benchmark {
|
2012-05-14 14:09:24 +00:00
|
|
|
public:
|
2013-11-21 06:21:58 +00:00
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
2012-05-14 14:09:24 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-05-14 14:09:24 +00:00
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() {
|
|
|
|
return "ref_cnt_heap";
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) {
|
2012-05-14 14:09:24 +00:00
|
|
|
char memory[sizeof(PlacedRefCnt)];
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2012-05-14 14:09:24 +00:00
|
|
|
PlacedRefCnt* ref = new (memory) PlacedRefCnt();
|
|
|
|
for (int j = 0; j < M; ++j) {
|
|
|
|
ref->ref();
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-05-14 14:09:24 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class RefCntBench_New : public Benchmark {
|
2012-05-16 18:21:56 +00:00
|
|
|
public:
|
2013-11-21 06:21:58 +00:00
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
2012-05-16 18:21:56 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-05-16 18:21:56 +00:00
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() {
|
|
|
|
return "ref_cnt_new";
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) {
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
2012-05-16 18:21:56 +00:00
|
|
|
SkRefCnt* ref = new SkRefCnt();
|
|
|
|
for (int j = 0; j < M; ++j) {
|
|
|
|
ref->ref();
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-05-16 18:21:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class WeakRefCntBench_Stack : public Benchmark {
|
2012-05-16 18:21:56 +00:00
|
|
|
public:
|
2013-11-21 06:21:58 +00:00
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
2012-05-16 18:21:56 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-05-16 18:21:56 +00:00
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() {
|
|
|
|
return "ref_cnt_stack_weak";
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) {
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
2012-05-16 18:21:56 +00:00
|
|
|
SkWeakRefCnt ref;
|
|
|
|
for (int j = 0; j < M; ++j) {
|
|
|
|
ref.ref();
|
|
|
|
ref.unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-05-16 18:21:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class PlacedWeakRefCnt : public SkWeakRefCnt {
|
|
|
|
public:
|
|
|
|
PlacedWeakRefCnt() : SkWeakRefCnt() { }
|
2013-03-05 18:50:01 +00:00
|
|
|
void operator delete(void*) { }
|
2012-05-16 18:21:56 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class WeakRefCntBench_Heap : public Benchmark {
|
2012-05-16 18:21:56 +00:00
|
|
|
public:
|
2013-11-21 06:21:58 +00:00
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
2012-05-16 18:21:56 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-05-16 18:21:56 +00:00
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() {
|
|
|
|
return "ref_cnt_heap_weak";
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) {
|
2012-05-16 18:21:56 +00:00
|
|
|
char memory[sizeof(PlacedWeakRefCnt)];
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2012-05-16 18:21:56 +00:00
|
|
|
PlacedWeakRefCnt* ref = new (memory) PlacedWeakRefCnt();
|
|
|
|
for (int j = 0; j < M; ++j) {
|
|
|
|
ref->ref();
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-05-16 18:21:56 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class WeakRefCntBench_New : public Benchmark {
|
2012-05-16 18:21:56 +00:00
|
|
|
public:
|
2013-11-21 06:21:58 +00:00
|
|
|
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
|
|
|
|
return backend == kNonRendering_Backend;
|
2012-05-16 18:21:56 +00:00
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
|
2012-05-16 18:21:56 +00:00
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() {
|
|
|
|
return "ref_cnt_new_weak";
|
|
|
|
}
|
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
virtual void onDraw(const int loops, SkCanvas*) {
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
2012-05-16 18:21:56 +00:00
|
|
|
SkWeakRefCnt* ref = new SkWeakRefCnt();
|
|
|
|
for (int j = 0; j < M; ++j) {
|
|
|
|
ref->ref();
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
ref->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-05-16 18:21:56 +00:00
|
|
|
};
|
|
|
|
|
2012-05-14 14:09:24 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-07-11 15:42:11 +00:00
|
|
|
DEF_BENCH( return new AtomicInc32(); )
|
|
|
|
DEF_BENCH( return new AtomicInc64(); )
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new RefCntBench_Stack(); )
|
|
|
|
DEF_BENCH( return new RefCntBench_Heap(); )
|
|
|
|
DEF_BENCH( return new RefCntBench_New(); )
|
2012-05-16 18:21:56 +00:00
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new WeakRefCntBench_Stack(); )
|
|
|
|
DEF_BENCH( return new WeakRefCntBench_Heap(); )
|
|
|
|
DEF_BENCH( return new WeakRefCntBench_New(); )
|