Skip to content

Commit 0279cb8

Browse files
authored
Improve performance of functions with dynamic arguments (#345)
* Deprecated functions with dynamic arguments * Bump version, update changelog
1 parent 39cafd4 commit 0279cb8

File tree

13 files changed

+598
-181
lines changed

13 files changed

+598
-181
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
## 2.2.0-wip
2+
3+
- Performance of functions that take `dynamic` arguments improved.
4+
- Most of the members that take `dynamic` arguments are deprecated. New members
5+
with non-`dynamic` arguments added.
6+
7+
Deprecated members:
8+
- `Matrix4.translate`
9+
10+
Instead use: `Matrix4.translateByDouble`, `Matrix4.translateByVector3`,
11+
`Matrix4.translateByVector4`.
12+
13+
- `Matrix4.leftTranslate`
14+
15+
Instead use: `Matrix4.leftTranslateByDouble`,
16+
`Matrix4.leftTranslateByVector3`, `Matrix4.leftTranslateByVector4`.
17+
18+
- `Matrix4.scale`
19+
20+
Instead use: `Matrix4.scaleByDouble`, `Matrix4.scaleByVector3`,
21+
`Matrix4.scaleByVector4`.
22+
23+
- `Matrix4.scaled`
24+
25+
Instead use: `Matrix4.scaledByDouble`, `Matrix4.scaledByVector3`,
26+
`Matrix4.scaledByVector4`.
27+
128
## 2.1.5
229

330
- Fixed `operator -()` of Quaternion (Contributed by tlserver)

benchmark/matrix_bench.dart

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// All rights reserved. Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// ignore_for_file: deprecated_member_use_from_same_package
6+
57
import 'dart:math' as math;
68
import 'dart:typed_data';
79

@@ -321,6 +323,148 @@ class Matrix3TransposeMultiplyBenchmark extends BenchmarkBase {
321323
}
322324
}
323325

326+
class Matrix4TranslateByDoubleGenericBenchmark extends BenchmarkBase {
327+
Matrix4TranslateByDoubleGenericBenchmark()
328+
: super('Matrix4.translateByDoubleGeneric');
329+
330+
final temp = Matrix4.zero()..setIdentity();
331+
332+
static void main() {
333+
Matrix4TranslateByDoubleGenericBenchmark().report();
334+
}
335+
336+
@override
337+
void run() {
338+
for (var i = 0; i < 100; i++) {
339+
temp.translate(10.0, 20.0, 30.0);
340+
}
341+
}
342+
}
343+
344+
class Matrix4TranslateByVector3GenericBenchmark extends BenchmarkBase {
345+
Matrix4TranslateByVector3GenericBenchmark()
346+
: super('Matrix4.translateByVector3Generic');
347+
348+
final temp = Matrix4.zero()..setIdentity();
349+
final vec = Vector3(10.0, 20.0, 30.0);
350+
351+
static void main() {
352+
Matrix4TranslateByVector3GenericBenchmark().report();
353+
}
354+
355+
@override
356+
void run() {
357+
for (var i = 0; i < 100; i++) {
358+
temp.translate(vec);
359+
}
360+
}
361+
}
362+
363+
class Matrix4TranslateByVector4GenericBenchmark extends BenchmarkBase {
364+
Matrix4TranslateByVector4GenericBenchmark()
365+
: super('Matrix4.translateByVector4Generic');
366+
367+
final temp = Matrix4.zero()..setIdentity();
368+
final vec = Vector4(10.0, 20.0, 30.0, 40.0);
369+
370+
static void main() {
371+
Matrix4TranslateByVector4GenericBenchmark().report();
372+
}
373+
374+
@override
375+
void run() {
376+
for (var i = 0; i < 100; i++) {
377+
temp.translate(vec);
378+
}
379+
}
380+
}
381+
382+
class Matrix4TranslateByDoubleBenchmark extends BenchmarkBase {
383+
Matrix4TranslateByDoubleBenchmark() : super('Matrix4.translateByDouble');
384+
385+
final temp = Matrix4.zero()..setIdentity();
386+
387+
static void main() {
388+
Matrix4TranslateByDoubleBenchmark().report();
389+
}
390+
391+
// Call the benchmarked method with random arguments to make sure TFA won't
392+
// specialize it based on the arguments passed and wasm-opt won't inline it,
393+
// for fair comparison with the generic case.
394+
@override
395+
void setup() {
396+
for (var i = 0; i < 10; i++) {
397+
temp.translateByDouble(
398+
i.toDouble(), (i * 10).toDouble(), (i * 5).toDouble(), 1.0);
399+
}
400+
}
401+
402+
@override
403+
void run() {
404+
for (var i = 0; i < 100; i++) {
405+
temp.translateByDouble(10.0, 20.0, 30.0, 1.0);
406+
}
407+
}
408+
}
409+
410+
class Matrix4TranslateByVector3Benchmark extends BenchmarkBase {
411+
Matrix4TranslateByVector3Benchmark() : super('Matrix4.translateByVector3');
412+
413+
final temp = Matrix4.zero()..setIdentity();
414+
final vec = Vector3(10.0, 20.0, 30.0);
415+
416+
static void main() {
417+
Matrix4TranslateByVector3Benchmark().report();
418+
}
419+
420+
// Call the benchmarked method with random arguments to make sure TFA won't
421+
// specialize it based on the arguments passed and wasm-opt won't inline it,
422+
// for fair comparison with the generic case.
423+
@override
424+
void setup() {
425+
for (var i = 0; i < 10; i++) {
426+
temp.translateByVector3(
427+
Vector3(i.toDouble(), (i * 10).toDouble(), (i * 5).toDouble()));
428+
}
429+
}
430+
431+
@override
432+
void run() {
433+
for (var i = 0; i < 100; i++) {
434+
temp.translateByVector3(vec);
435+
}
436+
}
437+
}
438+
439+
class Matrix4TranslateByVector4Benchmark extends BenchmarkBase {
440+
Matrix4TranslateByVector4Benchmark() : super('Matrix4.translateByVector4');
441+
442+
final temp = Matrix4.zero()..setIdentity();
443+
final vec = Vector4(10.0, 20.0, 30.0, 40.0);
444+
445+
static void main() {
446+
Matrix4TranslateByVector4Benchmark().report();
447+
}
448+
449+
// Call the benchmarked method with random arguments to make sure TFA won't
450+
// specialize it based on the arguments passed and wasm-opt won't inline it,
451+
// for fair comparison with the generic case.
452+
@override
453+
void setup() {
454+
for (var i = 0; i < 10; i++) {
455+
temp.translateByVector4(Vector4(i.toDouble(), (i * 10).toDouble(),
456+
(i * 5).toDouble(), (i * 20).toDouble()));
457+
}
458+
}
459+
460+
@override
461+
void run() {
462+
for (var i = 0; i < 100; i++) {
463+
temp.translateByVector4(vec);
464+
}
465+
}
466+
}
467+
324468
void main() {
325469
MatrixMultiplyBenchmark.main();
326470
SIMDMatrixMultiplyBenchmark.main();
@@ -335,4 +479,10 @@ void main() {
335479
Matrix3TransformVector3Benchmark.main();
336480
Matrix3TransformVector2Benchmark.main();
337481
Matrix3TransposeMultiplyBenchmark.main();
482+
Matrix4TranslateByDoubleGenericBenchmark.main();
483+
Matrix4TranslateByVector3GenericBenchmark.main();
484+
Matrix4TranslateByVector4GenericBenchmark.main();
485+
Matrix4TranslateByDoubleBenchmark.main();
486+
Matrix4TranslateByVector3Benchmark.main();
487+
Matrix4TranslateByVector4Benchmark.main();
338488
}

lib/src/vector_math/matrix2.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,21 @@ class Matrix2 {
215215
}
216216

217217
/// Returns a new vector or matrix by multiplying this with [arg].
218+
@pragma('wasm:prefer-inline')
219+
@pragma('vm:prefer-inline')
220+
@pragma('dart2js:prefer-inline')
218221
dynamic operator *(dynamic arg) {
222+
final Object result;
219223
if (arg is double) {
220-
return scaled(arg);
221-
}
222-
if (arg is Vector2) {
223-
return transformed(arg);
224-
}
225-
if (arg is Matrix2) {
226-
return multiplied(arg);
224+
result = scaled(arg);
225+
} else if (arg is Vector2) {
226+
result = transformed(arg);
227+
} else if (arg is Matrix2) {
228+
result = multiplied(arg);
229+
} else {
230+
throw ArgumentError(arg);
227231
}
228-
throw ArgumentError(arg);
232+
return result;
229233
}
230234

231235
/// Returns new matrix after component wise this + [arg]

lib/src/vector_math/matrix3.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,21 @@ class Matrix3 {
326326
}
327327

328328
/// Returns a new vector or matrix by multiplying this with [arg].
329+
@pragma('wasm:prefer-inline')
330+
@pragma('vm:prefer-inline')
331+
@pragma('dart2js:prefer-inline')
329332
dynamic operator *(dynamic arg) {
333+
final Object result;
330334
if (arg is double) {
331-
return scaled(arg);
332-
}
333-
if (arg is Vector3) {
334-
return transformed(arg);
335-
}
336-
if (arg is Matrix3) {
337-
return multiplied(arg);
335+
result = scaled(arg);
336+
} else if (arg is Vector3) {
337+
result = transformed(arg);
338+
} else if (arg is Matrix3) {
339+
result = multiplied(arg);
340+
} else {
341+
throw ArgumentError(arg);
338342
}
339-
throw ArgumentError(arg);
343+
return result;
340344
}
341345

342346
/// Returns new matrix after component wise this + [arg]

0 commit comments

Comments
 (0)