-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnon_devirtualized_typed_data_access.dart
More file actions
623 lines (532 loc) · 17.9 KB
/
Copy pathnon_devirtualized_typed_data_access.dart
File metadata and controls
623 lines (532 loc) · 17.9 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:typed_data';
import 'dart:js_interop';
import 'dart:math' as math;
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:vector_math/vector_math.dart';
import 'package:vector_math/vector_math_64.dart' as vector_math_64;
import 'package:vector_math/vector_math_operations.dart';
// These benchmarks are a subset of the benchmarks from the package:vector_math
// package.
//
// We modify the benchmarks slightly
//
// * confuse the dart2wasm compiler about which typed data implementation is used
//
// * ensure certain benchmarks store their result in a global sink to avoid -
// thereby avoiding the runtime to inline everything and make it a NOP
//
class MatrixMultiplyBenchmark extends BenchmarkBase {
MatrixMultiplyBenchmark() : super('MatrixMultiply');
final A = identityFloat32ConfuseCompiler(Float32List(16));
final B = identityFloat32ConfuseCompiler(Float32List(16));
final C = identityFloat32ConfuseCompiler(Float32List(16));
static void main() {
MatrixMultiplyBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 200; i++) {
Matrix44Operations.multiply(C, 0, A, 0, B, 0);
globalSink = C;
}
}
}
class VectorTransformBenchmark extends BenchmarkBase {
VectorTransformBenchmark() : super('VectorTransform');
final A = identityFloat32ConfuseCompiler(Float32List(16));
final B = identityFloat32ConfuseCompiler(Float32List(4));
final C = identityFloat32ConfuseCompiler(Float32List(4));
static void main() {
VectorTransformBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 200; i++) {
Matrix44Operations.transform4(C, 0, A, 0, B, 0);
globalSink = C;
}
}
}
class ViewMatrixBenchmark extends BenchmarkBase {
ViewMatrixBenchmark() : super('setViewMatrix');
final M = Matrix4.zero();
final P = Vector3.zero();
final F = Vector3.zero();
final U = Vector3.zero();
static void main() {
ViewMatrixBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100; i++) {
setViewMatrix(M, P, F, U);
globalSink = M;
}
}
}
class Aabb2TransformBenchmark extends BenchmarkBase {
Aabb2TransformBenchmark() : super('aabb2Transform');
static final M = Matrix3.rotationZ(math.pi / 4);
static final P1 = Vector2(10.0, 10.0);
static final P2 = Vector2(20.0, 30.0);
static final P3 = Vector2(100.0, 50.0);
static final B1 = Aabb2.minMax(P1, P2);
static final B2 = Aabb2.minMax(P1, P3);
static final B3 = Aabb2.minMax(P2, P3);
static final temp = Aabb2();
static void main() {
Aabb2TransformBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100; i++) {
temp.copyFrom(B1);
temp.transform(M);
temp.copyFrom(B2);
temp.transform(M);
temp.copyFrom(B3);
temp.transform(M);
globalSink = temp;
}
}
}
class Aabb2RotateBenchmark extends BenchmarkBase {
Aabb2RotateBenchmark() : super('aabb2Rotate');
static final M = Matrix3.rotationZ(math.pi / 4);
static final P1 = Vector2(10.0, 10.0);
static final P2 = Vector2(20.0, 30.0);
static final P3 = Vector2(100.0, 50.0);
static final B1 = Aabb2.minMax(P1, P2);
static final B2 = Aabb2.minMax(P1, P3);
static final B3 = Aabb2.minMax(P2, P3);
static final temp = Aabb2();
static void main() {
Aabb2RotateBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100; i++) {
temp.copyFrom(B1);
temp.rotate(M);
temp.copyFrom(B2);
temp.rotate(M);
temp.copyFrom(B3);
temp.rotate(M);
globalSink = temp;
}
}
}
class Aabb3TransformBenchmark extends BenchmarkBase {
Aabb3TransformBenchmark() : super('aabb3Transform');
static final M = Matrix4.rotationZ(math.pi / 4);
static final P1 = Vector3(10.0, 10.0, 0.0);
static final P2 = Vector3(20.0, 30.0, 1.0);
static final P3 = Vector3(100.0, 50.0, 10.0);
static final B1 = Aabb3.minMax(P1, P2);
static final B2 = Aabb3.minMax(P1, P3);
static final B3 = Aabb3.minMax(P2, P3);
static final temp = Aabb3();
static void main() {
Aabb3TransformBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100; i++) {
temp.copyFrom(B1);
temp.transform(M);
temp.copyFrom(B2);
temp.transform(M);
temp.copyFrom(B3);
temp.transform(M);
globalSink = temp;
}
}
}
class Aabb3RotateBenchmark extends BenchmarkBase {
Aabb3RotateBenchmark() : super('aabb3Rotate');
static final M = Matrix4.rotationZ(math.pi / 4);
static final P1 = Vector3(10.0, 10.0, 0.0);
static final P2 = Vector3(20.0, 30.0, 1.0);
static final P3 = Vector3(100.0, 50.0, 10.0);
static final B1 = Aabb3.minMax(P1, P2);
static final B2 = Aabb3.minMax(P1, P3);
static final B3 = Aabb3.minMax(P2, P3);
static final temp = Aabb3();
static void main() {
Aabb3RotateBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100; i++) {
temp.copyFrom(B1);
temp.rotate(M);
temp.copyFrom(B2);
temp.rotate(M);
temp.copyFrom(B3);
temp.rotate(M);
globalSink = temp;
}
}
}
class Matrix3TransformVector3Benchmark extends BenchmarkBase {
Matrix3TransformVector3Benchmark() : super('Matrix3.transform(Vector3)');
final MX = Matrix3.rotationX(math.pi / 4);
final MY = Matrix3.rotationY(math.pi / 4);
final MZ = Matrix3.rotationZ(math.pi / 4);
final V1 = Vector3(10.0, 20.0, 1.0);
final V2 = Vector3(-10.0, 20.0, 1.0);
final V3 = Vector3(10.0, -20.0, 1.0);
static void main() {
Matrix3TransformVector3Benchmark().report();
}
@override
void run() {
for (var i = 0; i < 800; i++) {
globalSink = MX.transform(V1);
globalSink = MX.transform(V2);
globalSink = MX.transform(V3);
globalSink = MY.transform(V1);
globalSink = MY.transform(V2);
globalSink = MY.transform(V3);
globalSink = MZ.transform(V1);
globalSink = MZ.transform(V2);
globalSink = MZ.transform(V3);
}
}
}
class Matrix3TransformVector2Benchmark extends BenchmarkBase {
Matrix3TransformVector2Benchmark() : super('Matrix3.transform(Vector2)');
final MX = Matrix3.rotationX(math.pi / 4);
final MY = Matrix3.rotationY(math.pi / 4);
final MZ = Matrix3.rotationZ(math.pi / 4);
final V1 = Vector2(10.0, 20.0);
final V2 = Vector2(-10.0, 20.0);
final V3 = Vector2(10.0, -20.0);
static void main() {
Matrix3TransformVector2Benchmark().report();
}
@override
void run() {
for (var i = 0; i < 800; i++) {
globalSink = MX.transform2(V1);
globalSink = MX.transform2(V2);
globalSink = MX.transform2(V3);
globalSink = MY.transform2(V1);
globalSink = MY.transform2(V2);
globalSink = MY.transform2(V3);
globalSink = MZ.transform2(V1);
globalSink = MZ.transform2(V2);
globalSink = MZ.transform2(V3);
}
}
}
class ConstructorBenchmark extends BenchmarkBase {
ConstructorBenchmark() : super('Vector2()');
static void main() {
ConstructorBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100000; i++) {
globalSink = Vector2(100, 100);
}
}
}
class ConstructorZeroBenchmark extends BenchmarkBase {
ConstructorZeroBenchmark() : super('Vector2.zero()');
static void main() {
ConstructorZeroBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100000; i++) {
globalSink = Vector2.zero();
}
}
}
class ConstructorArrayBenchmark extends BenchmarkBase {
ConstructorArrayBenchmark() : super('Vector2.array()');
static void main() {
ConstructorArrayBenchmark().report();
}
@override
void run() {
for (var i = 0.0; i < 100000; i++) {
globalSink = Vector2.array([i, i]);
}
}
}
class ConstructorAllBenchmark extends BenchmarkBase {
ConstructorAllBenchmark() : super('Vector2.all()');
static void main() {
ConstructorAllBenchmark().report();
}
@override
void run() {
for (var i = 0.0; i < 100000; i++) {
globalSink = Vector2.all(i);
}
}
}
class ConstructorCopyBenchmark extends BenchmarkBase {
ConstructorCopyBenchmark() : super('Vector2.copy()');
static void main() {
ConstructorCopyBenchmark().report();
}
@override
void run() {
final copyFrom = Vector2(1, 1);
for (var i = 0.0; i < 100000; i++) {
globalSink = Vector2.copy(copyFrom);
}
}
}
class ConstructorFromFloat32ListBenchmark extends BenchmarkBase {
final list = identityFloat32ConfuseCompiler(Float32List.fromList([0.0, 0.0]));
ConstructorFromFloat32ListBenchmark() : super('Vector2.fromFloat32List()');
static void main() {
ConstructorFromFloat32ListBenchmark().report();
}
@override
void run() {
for (var i = 0.0; i < 100000; i++) {
globalSink = Vector2.fromFloat32List(list);
}
}
}
class ConstructorFromBufferBenchmark extends BenchmarkBase {
final buffer = identityByteBufferConfuseCompiler(Uint32List(2).buffer);
ConstructorFromBufferBenchmark() : super('Vector2.fromBuffer()');
static void main() {
ConstructorFromBufferBenchmark().report();
}
@override
void run() {
for (var i = 0.0; i < 100000; i++) {
globalSink = Vector2.fromBuffer(buffer, 0);
}
}
}
class ConstructorRandomBenchmark extends BenchmarkBase {
final random = math.Random();
ConstructorRandomBenchmark() : super('Vector2.random()');
static void main() {
ConstructorRandomBenchmark().report();
}
@override
void run() {
for (var i = 0.0; i < 100000; i++) {
globalSink = Vector2.random(random);
}
}
}
class SetFromBenchmark extends BenchmarkBase {
SetFromBenchmark() : super('Vector2.setFrom()');
final Vector2 v1 = Vector2(100, 100);
final Vector2 v2 = Vector2.zero();
static void main() {
SetFromBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100000; i++) {
v2.setFrom(v1);
globalSink = v2;
}
}
}
class DotProductBenchmark extends BenchmarkBase {
DotProductBenchmark() : super('Vector2.dot()');
final Vector2 v1 = Vector2(100, 100);
final Vector2 v2 = Vector2(100, 200);
static void main() {
DotProductBenchmark().report();
}
@override
void run() {
for (var i = 0; i < 100000; i++) {
globalSink = v1.dot(v2);
}
}
}
mixin TweenSetup on BenchmarkBase {
final beginTransform = vector_math_64.Matrix4.compose(
vector_math_64.Vector3(1.0, 1.0, 1.0),
vector_math_64.Quaternion.euler(0.0, 0.0, 0.0),
vector_math_64.Vector3(1.0, 1.0, 1.0),
);
final endTransform = vector_math_64.Matrix4.compose(
vector_math_64.Vector3(5.0, 260.0, 1.0),
vector_math_64.Quaternion.euler(0.0, 1.0, -0.7),
vector_math_64.Vector3(0.6, 0.6, 0.6),
);
@override
void run() {
var sum_traces = 0.0;
for (var i = 0; i <= 1024; i++) {
final t = i / 1024.0;
final m1 = lerp(beginTransform, endTransform, t);
final m2 = lerp(endTransform, beginTransform, t);
sum_traces += m1.trace();
sum_traces += m2.trace();
}
if (sum_traces < 6320 || sum_traces > 6321) {
throw StateError('Bad result: $sum_traces');
}
}
vector_math_64.Matrix4 lerp(
vector_math_64.Matrix4 begin, vector_math_64.Matrix4 end, double t);
}
class Matrix4TweenBenchmark1 extends BenchmarkBase with TweenSetup {
Matrix4TweenBenchmark1() : super('Matrix4TweenBenchmark1');
@override
vector_math_64.Matrix4 lerp(
vector_math_64.Matrix4 begin, vector_math_64.Matrix4 end, double t) {
final beginTranslation = vector_math_64.Vector3.zero();
final endTranslation = vector_math_64.Vector3.zero();
final beginRotation = vector_math_64.Quaternion.identity();
final endRotation = vector_math_64.Quaternion.identity();
final beginScale = vector_math_64.Vector3.zero();
final endScale = vector_math_64.Vector3.zero();
begin.decompose(beginTranslation, beginRotation, beginScale);
end.decompose(endTranslation, endRotation, endScale);
final lerpTranslation = beginTranslation * (1.0 - t) + endTranslation * t;
final lerpRotation =
(beginRotation.scaled(1.0 - t) + endRotation.scaled(t)).normalized();
final lerpScale = beginScale * (1.0 - t) + endScale * t;
return vector_math_64.Matrix4.compose(
lerpTranslation, lerpRotation, lerpScale);
}
}
class Matrix4TweenBenchmark2 extends BenchmarkBase with TweenSetup {
Matrix4TweenBenchmark2() : super('Matrix4TweenBenchmark2');
@override
vector_math_64.Matrix4 lerp(
vector_math_64.Matrix4 begin, vector_math_64.Matrix4 end, double t) {
begin.decompose(beginTranslation, beginRotation, beginScale);
end.decompose(endTranslation, endRotation, endScale);
vector_math_64.Vector3.mix(
beginTranslation, endTranslation, t, lerpTranslation);
final lerpRotation =
(beginRotation.scaled(1.0 - t) + endRotation.scaled(t)).normalized();
vector_math_64.Vector3.mix(beginScale, endScale, t, lerpScale);
return vector_math_64.Matrix4.compose(
lerpTranslation, lerpRotation, lerpScale);
}
// Pre-allocated vectors.
static final beginTranslation = vector_math_64.Vector3.zero();
static final endTranslation = vector_math_64.Vector3.zero();
static final lerpTranslation = vector_math_64.Vector3.zero();
static final beginRotation = vector_math_64.Quaternion.identity();
static final endRotation = vector_math_64.Quaternion.identity();
static final beginScale = vector_math_64.Vector3.zero();
static final endScale = vector_math_64.Vector3.zero();
static final lerpScale = vector_math_64.Vector3.zero();
}
class Matrix4TweenBenchmark3 extends BenchmarkBase with TweenSetup {
Matrix4TweenBenchmark3() : super('Matrix4TweenBenchmark3');
@override
vector_math_64.Matrix4 lerp(
vector_math_64.Matrix4 begin, vector_math_64.Matrix4 end, double t) {
begin.decompose(beginTranslation, beginRotation, beginScale);
end.decompose(endTranslation, endRotation, endScale);
vector_math_64.Vector3.mix(
beginTranslation, endTranslation, t, lerpTranslation);
final lerpRotation =
(beginRotation.scaled(1.0 - t) + endRotation.scaled(t)).normalized();
vector_math_64.Vector3.mix(beginScale, endScale, t, lerpScale);
return vector_math_64.Matrix4.compose(
lerpTranslation, lerpRotation, lerpScale);
}
late final beginTranslation = vector_math_64.Vector3.zero();
late final endTranslation = vector_math_64.Vector3.zero();
late final lerpTranslation = vector_math_64.Vector3.zero();
late final beginRotation = vector_math_64.Quaternion.identity();
late final endRotation = vector_math_64.Quaternion.identity();
late final beginScale = vector_math_64.Vector3.zero();
late final endScale = vector_math_64.Vector3.zero();
late final lerpScale = vector_math_64.Vector3.zero();
}
@JS('Float32Array')
extension type NewFloat32Array._(JSObject _) {
external NewFloat32Array(JSNumber length);
}
@JS('Float64Array')
extension type NewFloat64Array._(JSObject _) {
external NewFloat64Array(JSNumber length);
}
void main() {
confuseCompiler();
MatrixMultiplyBenchmark.main();
VectorTransformBenchmark.main();
ViewMatrixBenchmark.main();
Aabb2TransformBenchmark.main();
Aabb2RotateBenchmark.main();
Aabb3TransformBenchmark.main();
Aabb3RotateBenchmark.main();
Matrix3TransformVector3Benchmark.main();
Matrix3TransformVector2Benchmark.main();
ConstructorBenchmark.main();
ConstructorZeroBenchmark.main();
ConstructorArrayBenchmark.main();
ConstructorAllBenchmark.main();
ConstructorCopyBenchmark.main();
ConstructorFromFloat32ListBenchmark.main();
ConstructorFromBufferBenchmark.main();
ConstructorRandomBenchmark.main();
SetFromBenchmark.main();
DotProductBenchmark.main();
Matrix4TweenBenchmark1().report();
Matrix4TweenBenchmark2().report();
Matrix4TweenBenchmark3().report();
if (int.parse('1') == 0) {
print(globalSink);
}
}
// Makes dart2wasm compiler unable to devirtualize typed data accesses in other
// places.
//
// This kind of confusion happens very often in large apps (e.g. simply storing
// a typed data into a `Map` and getting it out again will make us loose
// concrete class information)
void confuseCompiler() {
Matrix4.fromFloat32List(dartViewMatrix4).hashCode;
Vector2.fromFloat32List(dartViewVector2).hashCode;
Vector3.fromFloat32List(dartViewVector3).hashCode;
vector_math_64.Matrix4.fromFloat64List(dartViewF64Matrix4).hashCode;
vector_math_64.Vector2.fromFloat64List(dartViewF64Vector2).hashCode;
vector_math_64.Vector3.fromFloat64List(dartViewF64Vector3).hashCode;
}
// Makes dart2wasm compiler unable to devirtualize typed data accesses in other
// places.
//
// This kind of confusion happens very often in large apps (e.g. simply storing
// a typed data into a `Map` and getting it out again will make us loose
// concrete class information)
Float32List identityFloat32ConfuseCompiler(Float32List list) =>
kTrue ? list : dartViewVector2;
// Makes dart2wasm compiler unable to devirtualize typed data accesses in other
// places.
//
// This kind of confusion happens very often in large apps (e.g. simply storing
// a typed data into a `Map` and getting it out again will make us loose
// concrete class information)
ByteBuffer identityByteBufferConfuseCompiler(ByteBuffer buffer) =>
kTrue ? buffer : dartViewVector2.buffer;
Object? globalSink;
final bool kTrue = int.parse('1') == 1;
final Float32List dartViewMatrix4 =
(NewFloat32Array(16.toJS) as JSFloat32Array).toDart;
final Float32List dartViewVector2 =
(NewFloat32Array(2.toJS) as JSFloat32Array).toDart;
final Float32List dartViewVector3 =
(NewFloat32Array(3.toJS) as JSFloat32Array).toDart;
final Float64List dartViewF64Matrix4 =
(NewFloat64Array(16.toJS) as JSFloat64Array).toDart;
final Float64List dartViewF64Vector2 =
(NewFloat64Array(2.toJS) as JSFloat64Array).toDart;
final Float64List dartViewF64Vector3 =
(NewFloat64Array(3.toJS) as JSFloat64Array).toDart;