Skip to content

Commit 5d3d965

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Use Function.prototype.call.bind(Number.prototype.toString) for small ints and doubles
This increases `{int,double}.toString()` speed when switching to JS strings (for `int`) and generally for `double`. Change-Id: I5a3be41220f35c5226b63f09f5a7f9c25e262eb4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403981 Commit-Queue: Martin Kustermann <kustermann@google.com> Reviewed-by: Slava Egorov <vegorov@google.com>
1 parent 4bcd8c2 commit 5d3d965

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

sdk/lib/_internal/wasm/lib/boxed_double.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,12 @@ final class BoxedDouble implements double {
341341
}
342342
}
343343
String result = jsStringToDartString(
344-
JSStringImpl(JS<WasmExternRef>("v => v.toString()", value)),
344+
JSStringImpl(
345+
JS<WasmExternRef?>(
346+
'Function.prototype.call.bind(Number.prototype.toString)',
347+
WasmF64.fromDouble(value),
348+
),
349+
),
345350
);
346351
if (this % 1.0 == 0.0 && result.indexOf('e') == -1) {
347352
result = '$result.0';

sdk/lib/_internal/wasm_js_compatibility/lib/boxed_int_to_string.dart

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ class BoxedInt {
2525
}
2626

2727
@pragma("wasm:prefer-inline")
28-
String _jsBigIntToString(int i, int radix) => JSStringImpl(
29-
JS<WasmExternRef?>(
30-
'Function.prototype.call.bind(BigInt.prototype.toString)',
31-
WasmI64.fromInt(i),
32-
WasmI32.fromInt(radix),
33-
),
34-
);
28+
String _jsBigIntToString(int i, int radix) {
29+
final upperBits = (i >> 31);
30+
final result =
31+
(upperBits == -1 || upperBits == 0)
32+
? JS<WasmExternRef?>(
33+
'Function.prototype.call.bind(Number.prototype.toString)',
34+
WasmI32.fromInt(i),
35+
WasmI32.fromInt(radix),
36+
)
37+
: JS<WasmExternRef?>(
38+
'Function.prototype.call.bind(BigInt.prototype.toString)',
39+
WasmI64.fromInt(i),
40+
WasmI32.fromInt(radix),
41+
);
42+
return JSStringImpl(result);
43+
}

0 commit comments

Comments
 (0)