Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(840)

Side by Side Diff: bench/TopoSortBench.cpp

Issue 1414503003: Add SkTTopoSort (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: clean up Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | src/core/SkTTopoSort.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 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
8 #include "Benchmark.h"
9 #include "SkRandom.h"
10 #include "SkString.h"
11 #include "SkTTopoSort.h"
12
13 ///////////////////////////////////////////////////////////////////////////////
14 // Shared with TopoSortTest.cpp
15 ///////////////////////////////////////////////////////////////////////////////
16
17 // A helper object to test the topological sorting code
18 class TopoTestNode {
19 public:
20 TopoTestNode(int id) : fID(id), fOutputPos(-1), fTempMark(false) { }
21
22 void dependsOn(TopoTestNode* src) {
23 *fDependencies.append() = src;
24 }
25
26 int id() const { return fID; }
27 void reset() { fOutputPos = -1; }
28
29 int outputPos() const { return fOutputPos; }
30
31 // check that the topological sort is valid for this node
32 void check() {
33 SkASSERT(-1 != fOutputPos);
34
35 for (int i = 0; i < fDependencies.count(); ++i) {
36 SkASSERT(-1 != fDependencies[i]->outputPos());
37 // This node should've been output after all the nodes on which it d epends
38 SkASSERT(fOutputPos > fDependencies[i]->outputPos());
39 }
40 }
41
42 // The following 7 methods are needed by the topological sort
43 static void SetTempMark(TopoTestNode* node) { node->fTempMark = true; }
44 static void ResetTempMark(TopoTestNode* node) { node->fTempMark = false; }
45 static bool IsTempMarked(TopoTestNode* node) { return node->fTempMark; }
46 static void Output(TopoTestNode* node, int outputPos) {
47 SkASSERT(-1 != outputPos);
48 node->fOutputPos = outputPos;
49 }
50 static bool WasOutput(TopoTestNode* node) { return (-1 != node->fOutputPos); }
51 static int NumDependencies(TopoTestNode* node) { return node->fDependencies. count(); }
52 static TopoTestNode* Dependency(TopoTestNode* node, int index) {
53 return node->fDependencies[index];
54 }
55
56 private:
57 int fID;
58 int fOutputPos;
59 bool fTempMark;
60
61 SkTDArray<TopoTestNode*> fDependencies;
62 };
63
64 static void alloc_nodes(SkTDArray<TopoTestNode*>* graph, int num) {
65 graph->setReserve(num);
66
67 for (int i = 0; i < num; ++i) {
68 *graph->append() = new TopoTestNode(i);
69 }
70 }
71
72 static void dealloc_nodes(SkTDArray<TopoTestNode*>* graph) {
73 for (int i = 0; i < graph->count(); ++i) {
74 delete (*graph)[i];
75 }
76 }
77
78 #ifdef SK_DEBUG
79 static void print(const SkTDArray<TopoTestNode*>& graph) {
80 for (int i = 0; i < graph.count(); ++i) {
81 SkDebugf("%d, ", graph[i]->id());
82 }
83 SkDebugf("\n");
84 }
85 #endif
86
87 // randomize the array
88 static void shuffle(SkTDArray<TopoTestNode*>* graph, SkRandom* rand) {
89 for (int i = graph->count()-1; i > 0; --i) {
90 int swap = rand->nextU() % (i+1);
91
92 TopoTestNode* tmp = (*graph)[i];
93 (*graph)[i] = (*graph)[swap];
94 (*graph)[swap] = tmp;
95 }
96 }
97
98 ///////////////////////////////////////////////////////////////////////////////
99 ///////////////////////////////////////////////////////////////////////////////
100
101 class TopoSortBench : public Benchmark {
102 public:
103 TopoSortBench() { }
104
105 ~TopoSortBench() override {
106 dealloc_nodes(&fGraph);
107 }
108
109 bool isSuitableFor(Backend backend) override {
110 return kNonRendering_Backend == backend;
111 }
112
113 protected:
114 const char* onGetName() override {
115 return "sort_topo_rand";
116 }
117
118 // Delayed initialization only done if onDraw will be called.
119 void onDelayedSetup() override {
120 alloc_nodes(&fGraph, kNumElements);
121
122 for (int i = kNumElements-1; i > 0; --i) {
123 int numEdges = fRand.nextU() % (kMaxEdges+1);
124
125 for (int j = 0; j < numEdges; ++j) {
126 int dep = fRand.nextU() % i;
127
128 fGraph[i]->dependsOn(fGraph[dep]);
129 }
130 }
131 }
132
133 void onDraw(int loops, SkCanvas*) override {
134 for (int i = 0; i < loops; ++i) {
135 for (int j = 0; j < fGraph.count(); ++j) {
136 fGraph[j]->reset();
137 }
138
139 shuffle(&fGraph, &fRand);
140
141 SkDEBUGCODE(bool actualResult =) SkTTopoSort<TopoTestNode>(&fGraph);
142 SkASSERT(actualResult);
143
144 #ifdef SK_DEBUG
145 for (int j = 0; j < fGraph.count(); ++j) {
146 fGraph[j]->check();
147 }
148 #endif
149 }
150 }
151
152 private:
153 static const int kNumElements = 1000;
154 static const int kMaxEdges = 5;
155
156 SkTDArray<TopoTestNode*> fGraph;
157 SkRandom fRand;
158
159 typedef Benchmark INHERITED;
160 };
161
162 ///////////////////////////////////////////////////////////////////////////////
163
164 DEF_BENCH( return new TopoSortBench(); )
165
OLDNEW
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | src/core/SkTTopoSort.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698