Skip to content

Commit 01628ae

Browse files
authored
Cleanup some test. NFC (#24861)
- Avoid assignment (prefer mutation) to self.cflags (see #24860 for why this is better). - Parameterize test_wasm_intrinsics_simd and remove unneeded arguments. - Use inline cflags where it improves test readability. - Remove use of EMSCRIPTEN_KEEPALIVE with `main` function.
1 parent 252ed11 commit 01628ae

File tree

5 files changed

+75
-116
lines changed

5 files changed

+75
-116
lines changed

test/core/test_indirectbr.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
*/
77

88
#include <stdio.h>
9-
int main(void) {
9+
10+
int main() {
1011
const void *addrs[2] = {&&FOO, &&BAR};
1112

1213
// confuse the optimizer so it doesn't hardcode the jump and avoid generating
1314
// an |indirectbr| instruction
1415
int which = 0;
15-
for (int x = 0; x < 1000; x++) which = (which + x * x) % 7;
16+
for (int x = 0; x < 1000; x++) {
17+
which = (which + x * x) % 7;
18+
}
1619
which = (which % 2) + 1;
1720

1821
goto *addrs[which];

test/embind/embind_benchmark.cpp

Lines changed: 54 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,23 @@ int counter = 0;
1212

1313
extern "C" {
1414

15-
int __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE get_counter()
16-
{
15+
int __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE get_counter() {
1716
return counter;
1817
}
1918

20-
void __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE increment_counter()
21-
{
19+
void __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE increment_counter() {
2220
++counter;
2321
}
2422

25-
int __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE sum_int(int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8, int v9)
26-
{
23+
int __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE sum_int(int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8, int v9) {
2724
return v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
2825
}
2926

30-
float __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE sum_float(float v1, float v2, float v3, float v4, float v5, float v6, float v7, float v8, float v9)
31-
{
27+
float __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE sum_float(float v1, float v2, float v3, float v4, float v5, float v6, float v7, float v8, float v9) {
3228
return v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
3329
}
3430

35-
int __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE returns_input(int i)
36-
{
31+
int __attribute__((noinline)) EMSCRIPTEN_KEEPALIVE returns_input(int i) {
3732
return i;
3833
}
3934

@@ -60,13 +55,11 @@ extern void call_through_interface2();
6055
extern void returns_val_benchmark();
6156
}
6257

63-
emscripten::val returns_val(emscripten::val value)
64-
{
58+
emscripten::val returns_val(emscripten::val value) {
6559
return emscripten::val(value.as<unsigned>() + 1);
6660
}
6761

68-
class Vec3
69-
{
62+
class Vec3 {
7063
public:
7164
Vec3():x(0),y(0),z(0) {}
7265
Vec3(float x_, float y_, float z_):x(x_),y(y_),z(z_) {}
@@ -75,8 +68,7 @@ class Vec3
7568

7669
Vec3 add(const Vec3 &lhs, const Vec3 &rhs) { return Vec3(lhs.x+rhs.x, lhs.y+rhs.y, lhs.z+rhs.z); }
7770

78-
class Transform
79-
{
71+
class Transform {
8072
public:
8173
Transform():scale(1) {}
8274

@@ -87,64 +79,52 @@ class Transform
8779
Vec3 __attribute__((noinline)) GetPosition() const { return pos; }
8880
Vec3 __attribute__((noinline)) GetRotation() const { return rot; }
8981
float __attribute__((noinline)) GetScale() const { return scale; }
90-
82+
9183
void __attribute__((noinline)) SetPosition(const Vec3 &pos_) { pos = pos_; }
9284
void __attribute__((noinline)) SetRotation(const Vec3 &rot_) { rot = rot_; }
9385
void __attribute__((noinline)) SetScale(float scale_) { scale = scale_; }
9486
};
9587
typedef std::shared_ptr<Transform> TransformPtr;
9688

97-
class GameObject
98-
{
89+
class GameObject {
9990
public:
100-
GameObject()
101-
{
91+
GameObject() {
10292
transform = std::make_shared<Transform>();
10393
}
10494
std::shared_ptr<Transform> transform;
105-
95+
10696
TransformPtr __attribute__((noinline)) GetTransform() const { return transform; }
10797
};
10898
typedef std::shared_ptr<GameObject> GameObjectPtr;
10999

110-
GameObjectPtr create_game_object()
111-
{
100+
GameObjectPtr create_game_object() {
112101
return std::make_shared<GameObject>();
113102
}
114103

115-
GameObjectPtr __attribute__((noinline)) pass_gameobject_ptr(GameObjectPtr p)
116-
{
104+
GameObjectPtr __attribute__((noinline)) pass_gameobject_ptr(GameObjectPtr p) {
117105
return p;
118106
}
119107

120-
class Foo
121-
{
108+
class Foo {
122109
public:
123-
Foo()
124-
:class_counter(0)
125-
{
126-
}
110+
Foo() : class_counter(0) {}
127111

128-
void __attribute__((noinline)) incr_global_counter()
129-
{
112+
void __attribute__((noinline)) incr_global_counter() {
130113
++counter;
131114
}
132115

133-
void __attribute__((noinline)) incr_class_counter()
134-
{
116+
void __attribute__((noinline)) incr_class_counter() {
135117
++class_counter;
136118
}
137119

138-
int class_counter_val() const
139-
{
120+
int class_counter_val() const {
140121
return class_counter;
141122
}
142123

143124
int class_counter;
144125
};
145126

146-
class Interface
147-
{
127+
class Interface {
148128
public:
149129
virtual ~Interface() {}
150130
virtual void call0() = 0;
@@ -247,14 +227,13 @@ void callInterface3(unsigned N, Interface& o) {
247227
}
248228
}
249229

250-
EMSCRIPTEN_BINDINGS(benchmark)
251-
{
230+
EMSCRIPTEN_BINDINGS(benchmark) {
252231
using namespace emscripten;
253-
232+
254233
class_<GameObject>("GameObject")
255234
.smart_ptr<GameObjectPtr>("GameObjectPtr")
256235
.function("GetTransform", &GameObject::GetTransform);
257-
236+
258237
class_<Transform>("Transform")
259238
.smart_ptr<TransformPtr>("TransformPtr")
260239
.function("GetPosition", &Transform::GetPosition)
@@ -263,22 +242,22 @@ EMSCRIPTEN_BINDINGS(benchmark)
263242
.function("SetPosition", &Transform::SetPosition)
264243
.function("SetRotation", &Transform::SetRotation)
265244
.function("SetScale", &Transform::SetScale);
266-
245+
267246
value_array<Vec3>("Vec3")
268247
.element(&Vec3::x)
269248
.element(&Vec3::y)
270249
.element(&Vec3::z);
271-
250+
272251
function("create_game_object", &create_game_object);
273252
function("pass_gameobject_ptr", &pass_gameobject_ptr);
274253
function("add", &add);
275-
254+
276255
function("get_counter", &get_counter);
277256
function("increment_counter", &increment_counter);
278257
function("returns_input", &returns_input);
279258
function("sum_int", &sum_int);
280259
function("sum_float", &sum_float);
281-
260+
282261
class_<Foo>("Foo")
283262
.constructor<>()
284263
.function("incr_global_counter", &Foo::incr_global_counter)
@@ -298,11 +277,9 @@ EMSCRIPTEN_BINDINGS(benchmark)
298277
function("returns_val", &returns_val);
299278
}
300279

301-
void __attribute__((noinline)) emscripten_get_now_benchmark(int N)
302-
{
280+
void __attribute__((noinline)) emscripten_get_now_benchmark(int N) {
303281
volatile float t = emscripten_get_now();
304-
for(int i = 0; i < N; ++i)
305-
{
282+
for (int i = 0; i < N; ++i) {
306283
emscripten_get_now();
307284
emscripten_get_now();
308285
emscripten_get_now();
@@ -318,11 +295,9 @@ void __attribute__((noinline)) emscripten_get_now_benchmark(int N)
318295
printf("C++ emscripten_get_now %d iters: %f msecs.\n", N, (t2-t));
319296
}
320297

321-
void __attribute__((noinline)) increment_counter_benchmark(int N)
322-
{
298+
void __attribute__((noinline)) increment_counter_benchmark(int N) {
323299
volatile float t = emscripten_get_now();
324-
for(int i = 0; i < N; ++i)
325-
{
300+
for ( int i = 0; i < N; ++i) {
326301
increment_counter();
327302
increment_counter();
328303
increment_counter();
@@ -338,12 +313,10 @@ void __attribute__((noinline)) increment_counter_benchmark(int N)
338313
printf("C++ increment_counter %d iters: %f msecs.\n", N, (t2-t));
339314
}
340315

341-
void __attribute__((noinline)) increment_class_counter_benchmark(int N)
342-
{
316+
void __attribute__((noinline)) increment_class_counter_benchmark(int N) {
343317
Foo foo;
344318
volatile float t = emscripten_get_now();
345-
for(int i = 0; i < N; ++i)
346-
{
319+
for (int i = 0; i < N; ++i) {
347320
foo.incr_class_counter();
348321
foo.incr_class_counter();
349322
foo.incr_class_counter();
@@ -359,12 +332,10 @@ void __attribute__((noinline)) increment_class_counter_benchmark(int N)
359332
printf("C++ increment_class_counter %d iters: %f msecs. result: %d\n", N, (t2-t), foo.class_counter);
360333
}
361334

362-
void __attribute__((noinline)) returns_input_benchmark()
363-
{
335+
void __attribute__((noinline)) returns_input_benchmark() {
364336
volatile int r = 0;
365337
volatile float t = emscripten_get_now();
366-
for(int i = 0; i < 100000; ++i)
367-
{
338+
for (int i = 0; i < 100000; ++i) {
368339
r += returns_input(i);
369340
r += returns_input(i);
370341
r += returns_input(i);
@@ -380,12 +351,10 @@ void __attribute__((noinline)) returns_input_benchmark()
380351
printf("C++ returns_input 100000 iters: %f msecs.\n", (t2-t));
381352
}
382353

383-
void __attribute__((noinline)) sum_int_benchmark()
384-
{
354+
void __attribute__((noinline)) sum_int_benchmark() {
385355
volatile float t = emscripten_get_now();
386356
volatile int r = 0;
387-
for(int i = 0; i < 100000; ++i)
388-
{
357+
for (int i = 0; i < 100000; ++i) {
389358
r += sum_int(i,2,3,4,5,6,7,8,9);
390359
r += sum_int(i,2,3,4,5,6,7,8,9);
391360
r += sum_int(i,2,3,4,5,6,7,8,9);
@@ -401,12 +370,10 @@ void __attribute__((noinline)) sum_int_benchmark()
401370
printf("C++ sum_int 100000 iters: %f msecs.\n", (t2-t));
402371
}
403372

404-
void __attribute__((noinline)) sum_float_benchmark()
405-
{
373+
void __attribute__((noinline)) sum_float_benchmark() {
406374
volatile float f = 0.f;
407375
volatile float t = emscripten_get_now();
408-
for(int i = 0; i < 100000; ++i)
409-
{
376+
for (int i = 0; i < 100000; ++i) {
410377
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
411378
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
412379
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
@@ -426,12 +393,12 @@ void __attribute__((noinline)) move_gameobjects_benchmark()
426393
{
427394
const int N = 10000;
428395
GameObjectPtr objects[N];
429-
for(int i = 0; i < N; ++i)
396+
for (int i = 0; i < N; ++i) {
430397
objects[i] = create_game_object();
431-
398+
}
399+
432400
volatile float t = emscripten_get_now();
433-
for(int i = 0; i < N; ++i)
434-
{
401+
for (int i = 0; i < N; ++i) {
435402
TransformPtr t = objects[i]->GetTransform();
436403
Vec3 pos = add(t->GetPosition(), Vec3(2.f, 0.f, 1.f));
437404
Vec3 rot = add(t->GetRotation(), Vec3(0.1f, 0.2f, 0.3f));
@@ -441,21 +408,22 @@ void __attribute__((noinline)) move_gameobjects_benchmark()
441408
volatile float t2 = emscripten_get_now();
442409

443410
Vec3 accum;
444-
for(int i = 0; i < N; ++i)
411+
for (int i = 0; i < N; ++i) {
445412
accum = add(add(accum, objects[i]->GetTransform()->GetPosition()), objects[i]->GetTransform()->GetRotation());
413+
}
446414
printf("C++ move_gameobjects %d iters: %f msecs. Result: %f\n", N, (t2-t), accum.x+accum.y+accum.z);
447415
}
448416

449417
void __attribute__((noinline)) pass_gameobject_ptr_benchmark()
450418
{
451419
const int N = 100000;
452420
GameObjectPtr objects[N];
453-
for(int i = 0; i < N; ++i)
421+
for (int i = 0; i < N; ++i) {
454422
objects[i] = create_game_object();
455-
423+
}
424+
456425
volatile float t = emscripten_get_now();
457-
for(int i = 0; i < N; ++i)
458-
{
426+
for (int i = 0; i < N; ++i) {
459427
objects[i] = pass_gameobject_ptr(objects[i]);
460428
}
461429
volatile float t2 = emscripten_get_now();
@@ -494,22 +462,20 @@ void __attribute__((noinline)) numeric_val_array_benchmark() {
494462
// val::array(const std::vector<T>& vec) with opt numeric types: 27.500000 msecs.
495463
}
496464

497-
int EMSCRIPTEN_KEEPALIVE main()
498-
{
499-
for(int i = 1000; i <= 100000; i *= 10)
465+
int main() {
466+
for (int i = 1000; i <= 100000; i *= 10) {
500467
emscripten_get_now_benchmark(i);
468+
}
501469

502470
printf("\n");
503-
for(int i = 1000; i <= 100000; i *= 10)
504-
{
471+
for (int i = 1000; i <= 100000; i *= 10) {
505472
increment_counter_benchmark(i);
506473
increment_counter_benchmark_js(i);
507474
increment_counter_benchmark_embind_js(i);
508475
printf("\n");
509476
}
510477

511-
for(int i = 1000; i <= 100000; i *= 10)
512-
{
478+
for (int i = 1000; i <= 100000; i *= 10) {
513479
increment_class_counter_benchmark(i);
514480
increment_class_counter_benchmark_embind_js(i);
515481
printf("\n");

0 commit comments

Comments
 (0)