-
Notifications
You must be signed in to change notification settings - Fork 24
/
enaml_like_benchmark.cpp
248 lines (231 loc) · 16.4 KB
/
enaml_like_benchmark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*-----------------------------------------------------------------------------
| Copyright (c) 2020, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
// Time updating an EditVariable in a set of constraints typical of enaml use.
#define ANKERL_NANOBENCH_IMPLEMENT
#include "nanobench.h"
#define AM_IMPLEMENTATION
#include "amoeba.h"
void build_solver(am_Solver* S, am_Var* width, am_Var* height)
{
// Create custom strength
am_Num mmedium = AM_MEDIUM * 1.25;
am_Num smedium = AM_MEDIUM * 100;
// Create the variable
am_Var *left = am_newvariable(S);
am_Var *top = am_newvariable(S);
am_Var *contents_top = am_newvariable(S);
am_Var *contents_bottom = am_newvariable(S);
am_Var *contents_left = am_newvariable(S);
am_Var *contents_right = am_newvariable(S);
am_Var *midline = am_newvariable(S);
am_Var *ctleft = am_newvariable(S);
am_Var *ctheight = am_newvariable(S);
am_Var *cttop = am_newvariable(S);
am_Var *ctwidth = am_newvariable(S);
am_Var *lb1left = am_newvariable(S);
am_Var *lb1height = am_newvariable(S);
am_Var *lb1top = am_newvariable(S);
am_Var *lb1width = am_newvariable(S);
am_Var *lb2left = am_newvariable(S);
am_Var *lb2height = am_newvariable(S);
am_Var *lb2top = am_newvariable(S);
am_Var *lb2width = am_newvariable(S);
am_Var *lb3left = am_newvariable(S);
am_Var *lb3height = am_newvariable(S);
am_Var *lb3top = am_newvariable(S);
am_Var *lb3width = am_newvariable(S);
am_Var *fl1left = am_newvariable(S);
am_Var *fl1height = am_newvariable(S);
am_Var *fl1top = am_newvariable(S);
am_Var *fl1width = am_newvariable(S);
am_Var *fl2left = am_newvariable(S);
am_Var *fl2height = am_newvariable(S);
am_Var *fl2top = am_newvariable(S);
am_Var *fl2width = am_newvariable(S);
am_Var *fl3left = am_newvariable(S);
am_Var *fl3height = am_newvariable(S);
am_Var *fl3top = am_newvariable(S);
am_Var *fl3width = am_newvariable(S);
// Add the edit variables
am_addedit(width, AM_STRONG);
am_addedit(height, AM_STRONG);
// Add the constraints
const
struct Info {
struct Item {
am_Var *var;
am_Num mul;
} term[5];
am_Num constant;
int relation;
am_Num strength;
} constraints[] = {
{ {{left}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{height}}, 0, AM_EQUAL, AM_MEDIUM },
{ {{top}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{width}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{height}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{top,-1},{contents_top}}, -10, AM_EQUAL, AM_REQUIRED },
{ {{lb3height}}, -16, AM_EQUAL, AM_STRONG },
{ {{lb3height}}, -16, AM_GREATEQUAL, AM_STRONG },
{ {{ctleft}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{cttop}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctwidth}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctheight}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl3left}}, 0, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctheight}}, -24, AM_GREATEQUAL, smedium },
{ {{ctwidth}}, -1.67772e+07, AM_LESSEQUAL, smedium },
{ {{ctheight}}, -24, AM_LESSEQUAL, smedium },
{ {{fl3top}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl3width}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl3height}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1width}}, -67, AM_EQUAL, AM_WEAK },
{ {{lb2width}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb2height}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl2height}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb3left}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl2width}}, -125, AM_GREATEQUAL, AM_STRONG },
{ {{fl2height}}, -21, AM_EQUAL, AM_STRONG },
{ {{fl2height}}, -21, AM_GREATEQUAL, AM_STRONG },
{ {{lb3top}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb3width}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1left}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl1width}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1width}}, -67, AM_GREATEQUAL, AM_STRONG },
{ {{fl2left}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb2width}}, -66, AM_EQUAL, AM_WEAK },
{ {{lb2width}}, -66, AM_GREATEQUAL, AM_STRONG },
{ {{lb2height}}, -16, AM_EQUAL, AM_STRONG },
{ {{fl1height}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl1top}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb2top}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb2top,-1},{lb3top},{lb2height,-1}}, -10, AM_EQUAL, mmedium },
{ {{lb3top,-1},{lb3height,-1},{fl3top}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb3top,-1},{lb3height,-1},{fl3top}}, -10, AM_EQUAL, mmedium },
{ {{contents_bottom},{fl3height,-1},{fl3top,-1}}, -0, AM_EQUAL, mmedium },
{ {{fl1top},{contents_top,-1}}, 0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl1top},{contents_top,-1}}, 0, AM_EQUAL, mmedium },
{ {{contents_bottom},{fl3height,-1},{fl3top,-1}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{left,-1},{width,-1},{contents_right}}, 10, AM_EQUAL, AM_REQUIRED },
{ {{top,-1},{height,-1},{contents_bottom}}, 10, AM_EQUAL, AM_REQUIRED },
{ {{left,-1},{contents_left}}, -10, AM_EQUAL, AM_REQUIRED },
{ {{lb3left},{contents_left,-1}}, 0, AM_EQUAL, mmedium },
{ {{fl1left},{midline,-1}}, 0, AM_EQUAL, AM_STRONG },
{ {{fl2left},{midline,-1}}, 0, AM_EQUAL, AM_STRONG },
{ {{ctleft},{midline,-1}}, 0, AM_EQUAL, AM_STRONG },
{ {{fl1top},{fl1height,0.5},{lb1top,-1},{lb1height,-0.5}}, 0, AM_EQUAL, AM_STRONG },
{ {{lb1left},{contents_left,-1}}, 0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1left},{contents_left,-1}}, 0, AM_EQUAL, mmedium },
{ {{lb1left,-1},{fl1left},{lb1width,-1}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1left,-1},{fl1left},{lb1width,-1}}, -10, AM_EQUAL, mmedium },
{ {{fl1left,-1},{contents_right},{fl1width,-1}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{width}}, 0, AM_EQUAL, AM_MEDIUM },
{ {{fl1top,-1},{fl2top},{fl1height,-1}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl1top,-1},{fl2top},{fl1height,-1}}, -10, AM_EQUAL, mmedium },
{ {{cttop},{fl2top,-1},{fl2height,-1}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctheight,-1},{cttop,-1},{fl3top}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{contents_bottom},{fl3height,-1},{fl3top,-1}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{cttop},{fl2top,-1},{fl2height,-1}}, -10, AM_EQUAL, mmedium },
{ {{fl1left,-1},{contents_right},{fl1width,-1}}, -0, AM_EQUAL, mmedium },
{ {{lb2top,-1},{lb2height,-0.5},{fl2top},{fl2height,0.5}}, 0, AM_EQUAL, AM_STRONG },
{ {{contents_left,-1},{lb2left}}, 0, AM_GREATEQUAL, AM_REQUIRED },
{ {{contents_left,-1},{lb2left}}, 0, AM_EQUAL, mmedium },
{ {{fl2left},{lb2width,-1},{lb2left,-1}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctheight,-1},{cttop,-1},{fl3top}}, -10, AM_EQUAL, mmedium },
{ {{contents_bottom},{fl3height,-1},{fl3top,-1}}, -0, AM_EQUAL, mmedium },
{ {{lb1top}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1width}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1height}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl2left},{lb2width,-1},{lb2left,-1}}, -10, AM_EQUAL, mmedium },
{ {{fl2left,-1},{fl2width,-1},{contents_right}}, -0, AM_EQUAL, mmedium },
{ {{fl2left,-1},{fl2width,-1},{contents_right}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb3left},{contents_left,-1}}, 0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1left}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctheight,0.5},{cttop},{lb3top,-1},{lb3height,-0.5}}, 0, AM_EQUAL, AM_STRONG },
{ {{ctleft},{lb3left,-1},{lb3width,-1}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctwidth,-1},{ctleft,-1},{contents_right}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctleft},{lb3left,-1},{lb3width,-1}}, -10, AM_EQUAL, mmedium },
{ {{fl3left},{contents_left,-1}}, 0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl3left},{contents_left,-1}}, 0, AM_EQUAL, mmedium },
{ {{ctwidth,-1},{ctleft,-1},{contents_right}}, -0, AM_EQUAL, mmedium },
{ {{fl3left,-1},{contents_right},{fl3width,-1}}, -0, AM_EQUAL, mmedium },
{ {{contents_top,-1},{lb1top}}, 0, AM_GREATEQUAL, AM_REQUIRED },
{ {{contents_top,-1},{lb1top}}, 0, AM_EQUAL, mmedium },
{ {{fl3left,-1},{contents_right},{fl3width,-1}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb2top},{lb1top,-1},{lb1height,-1}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb2top,-1},{lb3top},{lb2height,-1}}, -10, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb2top},{lb1top,-1},{lb1height,-1}}, -10, AM_EQUAL, mmedium },
{ {{fl1height}}, -21, AM_EQUAL, AM_STRONG },
{ {{fl1height}}, -21, AM_GREATEQUAL, AM_STRONG },
{ {{lb2left}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb2height}}, -16, AM_GREATEQUAL, AM_STRONG },
{ {{fl2top}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{fl2width}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{lb1height}}, -16, AM_GREATEQUAL, AM_STRONG },
{ {{lb1height}}, -16, AM_EQUAL, AM_STRONG },
{ {{fl3width}}, -125, AM_GREATEQUAL, AM_STRONG },
{ {{fl3height}}, -21, AM_EQUAL, AM_STRONG },
{ {{fl3height}}, -21, AM_GREATEQUAL, AM_STRONG },
{ {{lb3height}}, -0, AM_GREATEQUAL, AM_REQUIRED },
{ {{ctwidth}}, -119, AM_GREATEQUAL, smedium },
{ {{lb3width}}, -24, AM_EQUAL, AM_WEAK },
{ {{lb3width}}, -24, AM_GREATEQUAL, AM_STRONG },
{ {{fl1width}}, -125, AM_GREATEQUAL, AM_STRONG },
};
for (const auto& constraint : constraints) {
am_Constraint *c = am_newconstraint(S, constraint.strength);
for (auto* p = constraint.term; p->var; ++p) {
am_addterm(c, p->var, p->mul ? p->mul : 1);
}
am_addconstant(c, constraint.constant);
am_setrelation(c, constraint.relation);
int r = am_add(c);
assert(r == AM_OK);
}
}
int main()
{
ankerl::nanobench::Bench().minEpochIterations(100).run("building solver", [&] {
am_Solver *S = am_newsolver(NULL, NULL);
am_Var *width = am_newvariable(S);
am_Var *height = am_newvariable(S);
build_solver(S, width, height);
ankerl::nanobench::doNotOptimizeAway(S); //< prevent the compiler to optimize away the S
am_delsolver(S);
});
struct Size
{
int width;
int height;
};
Size sizes[] = {
{ 400, 600 },
{ 600, 400 },
{ 800, 1200 },
{ 1200, 800 },
{ 400, 800 },
{ 800, 400 }
};
am_Solver *S = am_newsolver(NULL, NULL);
am_Var *widthVar = am_newvariable(S);
am_Var *heightVar = am_newvariable(S);
build_solver(S, widthVar, heightVar);
for (const Size& size : sizes)
{
am_Num width = size.width;
am_Num height = size.height;
ankerl::nanobench::Bench().minEpochIterations(100000).run("suggest value " + std::to_string(size.width) + "x" + std::to_string(size.height), [&] {
am_suggest(widthVar, width);
am_suggest(heightVar, height);
am_updatevars(S);
});
}
am_delsolver(S);
return 0;
}
// cc: flags+='-O2 -std=c++11'