Skip to content

Commit ef64387

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Avoid using deep array copy in places where we only want to copy shallow
The `jsArrayFromDartList` method is a deep array copy: It will call out to JS to allocate an array and initialize it by calling back to Dart for each dart list element and `jsify()` each element (which may - if the element is a list - call `jsArrayFromDartList` again). For the use cases changed in this CL we know we have a `List<JSAny>` so there's actually no reason to do this recursively, we only need to allocate a JS array and fill in the elements from the dart list (without any processing such as `jsify(0)`ing the elements). We also add some fast cases to `List<JSAny?>.toJS` for length=0/1/2/3 Change-Id: Idf82e104a743b883c8af0bd1b3b3234dff6e3d5b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/414780 Reviewed-by: Srujan Gaddam <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent 54c8719 commit ef64387

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

sdk/lib/_internal/wasm/lib/js_array.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ class JSArrayImpl<T extends JSAny?> implements List<T> {
543543
js.JS<WasmExternRef?>(
544544
'(a, t) => a.concat(t)',
545545
toExternRef,
546-
other.toExternRef,
546+
other.toJS.toExternRef,
547547
),
548548
);
549549
} else {

sdk/lib/_internal/wasm/lib/js_helper.dart

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ extension StringToExternRef on String? {
8383
: jsStringFromDartString(this!).toExternRef;
8484
}
8585

86-
extension ListOfObjectToExternRef on List<Object?>? {
87-
WasmExternRef? get toExternRef =>
88-
this == null ? WasmExternRef.nullRef : jsArrayFromDartList(this!);
89-
}
90-
9186
extension JSValueToExternRef on JSValue? {
9287
WasmExternRef? get toExternRef => JSValue.unbox(this);
9388
}
@@ -533,7 +528,7 @@ ByteBuffer toDartByteBuffer(WasmExternRef? ref) =>
533528
toDartByteData(
534529
callConstructorVarArgsRaw(
535530
getConstructorString('DataView'),
536-
[JSValue(ref)].toExternRef,
531+
[JSValue(ref) as JSAny].toJS.toExternRef,
537532
),
538533
).buffer;
539534

@@ -571,7 +566,48 @@ List<int> jsIntTypedArrayToDartIntTypedData(
571566
}
572567

573568
JSArray<T> toJSArray<T extends JSAny?>(List<T> list) {
574-
int length = list.length;
569+
final length = list.length;
570+
571+
if (length <= 4) {
572+
if (length == 0) {
573+
return JSArray<T>.withLength(0);
574+
}
575+
final list0 = list[0].toExternRef;
576+
if (length == 1) {
577+
return JSValue(JS<WasmExternRef>("o => [o]", list0)) as JSArray<T>;
578+
}
579+
final list1 = list[1].toExternRef;
580+
if (length == 2) {
581+
return JSValue(JS<WasmExternRef>("(o0, o1) => [o0, o1]", list0, list1))
582+
as JSArray<T>;
583+
}
584+
final list2 = list[2].toExternRef;
585+
if (length == 3) {
586+
return JSValue(
587+
JS<WasmExternRef>(
588+
"(o0, o1, o2) => [o0, o1, o2]",
589+
list0,
590+
list1,
591+
list2,
592+
),
593+
)
594+
as JSArray<T>;
595+
}
596+
final list3 = list[3].toExternRef;
597+
if (length == 4) {
598+
return JSValue(
599+
JS<WasmExternRef>(
600+
"(o0, o1, o2, o3) => [o0, o1, o2, o3]",
601+
list0,
602+
list1,
603+
list2,
604+
list3,
605+
),
606+
)
607+
as JSArray<T>;
608+
}
609+
}
610+
575611
JSArray<T> result = JSArray<T>.withLength(length);
576612
for (int i = 0; i < length; i++) {
577613
result[i] = list[i];

sdk/lib/_internal/wasm/lib/js_interop_unsafe_patch.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension JSObjectUnsafeUtilExtension on JSObject {
4949
if (arg2 != null) arg2,
5050
if (arg3 != null) arg3,
5151
if (arg4 != null) arg4,
52-
].toExternRef,
52+
].toJS.toExternRef,
5353
),
5454
);
5555

@@ -59,7 +59,7 @@ extension JSObjectUnsafeUtilExtension on JSObject {
5959
callMethodVarArgsRaw(
6060
toExternRef,
6161
method.toExternRef,
62-
(arguments ?? <JSAny?>[]).toExternRef,
62+
(arguments ?? <JSAny?>[]).toJS.toExternRef,
6363
),
6464
);
6565

@@ -89,7 +89,7 @@ extension JSFunctionUnsafeUtilExtension on JSFunction {
8989
if (arg2 != null) arg2,
9090
if (arg3 != null) arg3,
9191
if (arg4 != null) arg4,
92-
].toExternRef,
92+
].toJS.toExternRef,
9393
),
9494
);
9595

@@ -98,7 +98,7 @@ extension JSFunctionUnsafeUtilExtension on JSFunction {
9898
_box<JSObject>(
9999
callConstructorVarArgsRaw(
100100
toExternRef,
101-
(arguments ?? <JSAny?>[]).toExternRef,
101+
(arguments ?? <JSAny?>[]).toJS.toExternRef,
102102
),
103103
);
104104
}

0 commit comments

Comments
 (0)