Files
sdk/tests/web/wasm/string_copy_test.dart
Martin Kustermann 9b11bf4da9 [dart2wasm] Take advantage of fast js-string builtins
Some wasm engines have started to optimize the `js-string` builtin
proposal (e.g. V8) and those that haven't yet are probaly going to
do soon.

So we can start taking advantage of it in dart2wasm.

=> We make use of them in the JS<->Dart string copy code.

=> This will also provide a better baseline when evaluating whether
   switching to JS stringes entirely makes sense.

A somewhat unrelated (but necessary for this CL) change is to tighten
the types we use in `@pragma('wasm:import')` and
`@pragma('wasm:export')` in some cases:

We should only use 'pure' wasm types (i.e. not wasm struct / function
types we define for dart classes & functions) and mostly non-composed
types in import/exports as the `--closed-world` optimizations from
binaryen rely on that (and error otherwise).

Overall this leads to significant improvements in Dart<->JS
string copies.

The `WasmDataTransfer.*{From,To}BrowserString` benchmarks
improve something between 50-100%.

Change-Id: I2048113c462ecb2047402c0616d2b3b1f45773f5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400641
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
2024-12-16 00:55:26 -08:00

35 lines
1.1 KiB
Dart

// 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:js_interop';
import 'package:expect/expect.dart';
main() async {
final String oneByteString = makeLongString('012345789');
final String twoByteString = makeLongString('01234中6789');
Expect.equals(oneByteString, roundTrip(oneByteString));
Expect.equals(twoByteString, roundTrip(twoByteString));
}
/// Ensure we make a very long string, ensuring that we'll also hit slow paths
/// in the string copy implementation.
String makeLongString(String string) {
while (string.length < 1024 * 1024) {
string = string + string;
}
return string;
}
/// Copies the string to JS and back again to a dart internal String.
String roundTrip(String dartString) {
final JSString jsString = dartString.toJS;
// Using string interpolation will force conversion to internal string (vs
// `JSStringImpl`)
final string = 'A${jsString.toDart}Z';
return string.substring(1, string.length - 1);
}