-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbenchmarks1.dart
514 lines (436 loc) · 11.6 KB
/
benchmarks1.dart
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
// Copyright (c) 2025, 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 'package:benchmark_harness/benchmark_harness.dart';
import 'dart:js_interop';
// Static js-interop benchmarks.
//
// Type checks:
//
// JSInterop.as.Foo - x as Foo
// JSInterop.as.T.Foo - x as T, where T is Foo
//
// Type tests:
//
// JSInterop.isA.Foo - x.isA<Foo>()
//
// Calls.
//
// JSInterop.call.moveFoo -
// Repeatedly copy the same Foo (some kind of JSObject) to JavaScript and
// back.
//
// JSInterop.call.{inline,hoisted,implicit}3ArgsSSS
// Passes three String arguments, using different positioning of `.toJS`
// conversions.
//
// JSInterop.call.{inline,hoisted,implicit}3ArgsIII
// Similar to JSInterop.call.*ArgsSSS but passing integer Numbers instead.
//
// JSInterop.call.inline7Args
// Compare with JSInterop.call.inline3ArgsSSS to assess the cost of more
// arguments.
/// Aliases for `Object.assign` with different types.
///
/// `Object.assign` with one argument returns the argument. It is a fast
/// JavaScript method that returns a JSObject.
@JS('Object')
extension type ObjectMethods(JSObject _) implements JSObject {
@JS('assign')
external static JSObject moveJSObject(JSObject o);
@JS('assign')
external static JSArray moveJSArray(JSArray o);
@JS('assign')
external static JSUint8Array moveJSUint8Array(JSUint8Array o);
}
// Alias for `.toString()` with different types.
//
// `x.toString()`, when called on a JSString receiver, calls
// `String.prototype.toString`, which is fast, since it returns the
// receiver. Additional arguments are ignored.
extension ToStringMethod on JSAny {
@JS('toString')
external JSString toString1(JSAny? a);
@JS('toString')
external JSString toString3SSS(JSString a, JSString b, JSString c);
@JS('toString')
external JSString toString3NNN(JSNumber a, JSNumber b, JSNumber c);
// This version uses Dart primitive types which implies an implicit
// conversion.
@JS('toString')
external String toString3ConvertedSSS(String a, String b, String c);
// This version uses Dart primitive types which implies an implicit
// conversion.
@JS('toString')
external String toString3ConvertedIII(int a, int b, int c);
@JS('toString')
external JSString toString7(
JSAny? a1,
JSAny? a2,
JSAny? a3,
JSAny? a4,
JSAny? a5,
JSAny? a6,
JSAny? a7,
);
}
extension type Date._(JSObject _) implements JSObject {
external Date.forTimestamp(int _);
external static int now();
}
const int N = 1000;
class Base extends BenchmarkBase {
Base(super.name);
}
List<Object?> allJSObjects() {
return List<Object?>.generate(N, (i) => createObject(), growable: false);
}
List<Object?> allJSAnys() {
return allJSObjects()
..[1] = 123.toJS
..[2] = 'x'.toJS;
}
List<Object?> allJSAnyQs() {
return allJSObjects()
..[0] = null
..[1] = 123.toJS
..[2] = 'x'.toJS;
}
final List<Object?> allJSNumbers = List<Object?>.generate(
N,
(i) => i.toJS,
growable: false,
);
final List<Object?> allJSStrings = List<Object?>.generate(
N,
(i) => '$i'.toJS,
growable: false,
);
final List<Object?> allJSUint8Array = List<Object?>.generate(
N,
(_) => JSUint8Array.withLength(2),
growable: false,
);
abstract class CastsBase extends Base {
final List<Object?> data;
CastsBase(String name, {required this.data}) : super('JSInterop.as.$name');
}
class Casts1 extends CastsBase {
Casts1() : super('JSObject', data: allJSObjects());
void run() {
for (final o in data) {
sink = o as JSObject;
}
}
}
class Casts2 extends CastsBase {
Casts2() : super('JSAny', data: allJSAnys());
void run() {
for (final o in data) {
sink = o as JSAny;
}
}
}
class Casts3 extends CastsBase {
Casts3() : super('JSAnyQ', data: allJSAnyQs());
void run() {
for (final o in data) {
sink = o as JSAny?;
}
}
}
class Casts4 extends CastsBase {
Casts4() : super('JSNumber', data: allJSNumbers);
void run() {
for (final o in data) {
sink = o as JSNumber;
}
}
}
class Casts5 extends CastsBase {
Casts5() : super('JSString', data: allJSStrings);
void run() {
for (final o in data) {
sink = o as JSString;
}
}
}
class Casts6 extends CastsBase {
Casts6() : super('JSUint8Array', data: allJSUint8Array);
void run() {
for (final o in data) {
sink = o as JSUint8Array;
}
}
}
class CastsT<T> extends CastsBase {
CastsT(String name, {required super.data}) : super('T.$name');
void run() {
for (final o in data) {
sink = o as T;
}
}
}
class Calls1SSS extends Base {
Calls1SSS() : super('JSInterop.calls.inline3ArgsSSS');
void run() {
String s = 'hello';
for (int i = 0; i < N; i++) {
// Pass in arguments with '.toJS' conversion.
// There is potential for a compiler to hoist the conversions.
// Implicit conversion of result of `toString3` to JSString.
// Explicit `.toDart` conversion of result JSString to Dart String.
sink = s = s.toJS.toString3SSS('a'.toJS, 'b'.toJS, 'c'.toJS).toDart;
}
}
}
class Calls1NNN extends Base {
Calls1NNN() : super('JSInterop.calls.inline3ArgsIII');
void run() {
String s = 'hello';
for (int i = 0; i < N; i++) {
// Pass in arguments with '.toJS' conversion.
// There is potential for a compiler to hoist the conversions.
// Implicit conversion of result of `toString3` to JSString.
// Explicit `.toDart` conversion of result JSString to Dart String.
sink = s = s.toJS.toString3NNN(1.toJS, 2.toJS, 3.toJS).toDart;
}
}
}
class Calls2SSS extends Base {
Calls2SSS() : super('JSInterop.calls.hoisted3ArgsSSS');
static final _a = 'a'.toJS;
static final _b = 'b'.toJS;
static final _c = 'c'.toJS;
void run() {
String s = 'hello';
for (int i = 0; i < N; i++) {
// Manually hoisted argument conversions.
// Implicit conversion of result of `toString3` to JSString.
// Explicit `.toDart` conversion of result JSString to Dart String.
sink = s = s.toJS.toString3SSS(_a, _b, _c).toDart;
}
}
}
class Calls2NNN extends Base {
Calls2NNN() : super('JSInterop.calls.hoisted3ArgsIII');
static final _a = 1.toJS;
static final _b = 2.toJS;
static final _c = 3.toJS;
void run() {
String s = 'hello';
for (int i = 0; i < N; i++) {
// Manually hoisted argument conversions.
// Implicit conversion of result of `toString3` to JSString.
// Explicit `.toDart` conversion of result JSString to Dart String.
sink = s = s.toJS.toString3NNN(_a, _b, _c).toDart;
}
}
}
class Calls3SSS extends Base {
Calls3SSS() : super('JSInterop.calls.implicit3ArgsSSS');
void run() {
String s = 'hello';
for (int i = 0; i < N; i++) {
// Implicit conversion of arguments and result as they have a primitive
// type.
sink = s = s.toJS.toString3ConvertedSSS('a', 'b', 'c');
}
}
}
class Calls3NNN extends Base {
Calls3NNN() : super('JSInterop.calls.implicit3ArgsIII');
void run() {
String s = 'hello';
for (int i = 0; i < N; i++) {
// Implicit conversion of arguments and result as they have a primitive
// type.
sink = s = s.toJS.toString3ConvertedIII(1, 2, 3);
}
}
}
class Calls4 extends Base {
Calls4() : super('JSInterop.calls.inline7Args');
void run() {
String s = 'hello';
for (int i = 0; i < N; i++) {
sink = s = s.toJS
.toString7(
'1'.toJS,
'2'.toJS,
'3'.toJS,
'4'.toJS,
'5'.toJS,
'6'.toJS,
'7'.toJS,
)
.toDart;
}
}
}
class Calls5 extends Base {
Calls5() : super('JSInterop.calls.moveJSObject');
static final _o = JSObject();
void run() {
JSObject o = _o;
for (int i = 0; i < N; i++) {
// Implicit conversion of result to JSObject.
sink = o = ObjectMethods.moveJSObject(o);
}
}
}
class Calls6 extends Base {
Calls6() : super('JSInterop.calls.moveJSArray');
static final _o = JSArray.withLength(2);
void run() {
JSArray o = _o;
for (int i = 0; i < N; i++) {
// Implicit conversion of result to JSArray.
sink = o = ObjectMethods.moveJSArray(o);
}
}
}
class Calls7 extends Base {
Calls7() : super('JSInterop.calls.moveJSUint8Array');
static final _o = JSUint8Array.withLength(2);
void run() {
JSUint8Array o = _o;
for (int i = 0; i < N; i++) {
// Implicit conversion of result to JSUint8Array.
sink = o = ObjectMethods.moveJSUint8Array(o);
}
}
}
List<JSAny?> _objects() => List<JSAny?>.generate(
N,
(i) => switch (i % 7) {
0 => JSObject(),
1 => i.toJS,
2 => JSArray(),
3 => '$i'.toJS,
4 => Date.forTimestamp(Date.now()),
5 => null,
6 => JSUint8Array.withLength(2),
int() => throw StateError('unexpected: $i'),
},
growable: false,
);
abstract class IsABase extends Base {
IsABase(String name) : super('JSInterop.isA.$name');
final List<JSAny?> objects = _objects();
}
class IsA1 extends IsABase {
IsA1() : super('JSObject');
void run() {
for (final o in objects) {
sink = o.isA<JSObject>();
}
}
}
class IsA2 extends IsABase {
IsA2() : super('JSAny');
void run() {
for (final o in objects) {
sink = o.isA<JSAny>();
}
}
}
class IsA3 extends IsABase {
IsA3() : super('JSString');
void run() {
for (final o in objects) {
sink = o.isA<JSString>();
}
}
}
class IsA4 extends IsABase {
IsA4() : super('JSArray');
void run() {
for (final o in objects) {
sink = o.isA<JSArray>();
}
}
}
class IsA5 extends IsABase {
IsA5() : super('Date');
void run() {
for (final o in objects) {
sink = o.isA<Date>();
}
}
}
class IsA6 extends IsABase {
IsA6() : super('DateQ');
void run() {
for (final o in objects) {
sink = o.isA<Date?>();
}
}
}
class IsA7 extends IsABase {
IsA7() : super('JSUint8Array');
void run() {
for (final o in objects) {
sink = o.isA<JSUint8Array>();
}
}
}
/// Returns a new JSObject, but with a type that is hard for an optimizing
/// compiler to know exactly.
Object? createObject() {
if (inscrutableTrue) return JSObject();
if (inscrutableTrue) return 123.toJS;
if (inscrutableTrue) return 'x'.toJS;
if (inscrutableTrue) return true.toJS;
if (inscrutableTrue) return StringBuffer();
return null;
}
bool get inscrutableTrue => DateTime.now().millisecondsSinceEpoch > 42;
Object? sink;
void main() {
print(Date.forTimestamp(Date.now()));
final benchmarks = [
IsA1(),
IsA2(),
IsA3(),
IsA4(),
IsA5(),
IsA6(),
IsA7(),
Calls1SSS(),
Calls1NNN(),
Calls2SSS(),
Calls2NNN(),
Calls3SSS(),
Calls3NNN(),
Calls4(),
Calls5(),
Calls6(),
Calls7(),
Casts1(),
Casts2(),
Casts3(),
Casts4(),
Casts5(),
Casts6(),
CastsT<JSObject>('JSObject', data: allJSObjects()),
CastsT<JSAny>('JSAny', data: allJSAnys()),
CastsT<JSAny?>('JSAnyQ', data: allJSAnyQs()),
CastsT<JSNumber>('JSNumber', data: allJSNumbers),
CastsT<JSString>('JSString', data: allJSStrings),
CastsT<JSUint8Array>('JSUint8Array', data: allJSUint8Array),
];
// Warmup all benchmarks to ensure JIT compilers see full polymorphism.
for (var benchmark in benchmarks) {
benchmark.setup();
benchmark.run();
benchmark.run();
if (sink == null) throw StateError('unexpected');
}
for (var benchmark in benchmarks) {
benchmark.run();
}
for (var benchmark in benchmarks) {
benchmark.report();
}
}