Skip to content

Commit

Permalink
[dart2wasm] Remove short input threshold to fall back to native UTF-8…
Browse files Browse the repository at this point in the history
… decoder

When decoding a JS array input as UTF-8 we currently fall back to the
decoder implemented in Dart when the input is small.

When benchmarked with `benchmark_harness`, for one byte array, I get
6.0us when decoded with the Dart decoder, and 5.8us when decoded with JS
`TextDecoder`.

So it looks like Dart decoder is never faster when the input is a JS
array. Remove the threshold and always call JS when the input is a JS
array.

(This threshold was copied from dart2js, where accessing JS data is less
expensive.)

Change-Id: I1a34fd1f38718e34a1ff7bac0d7c6178bf6dd469
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405002
Reviewed-by: Martin Kustermann <[email protected]>
Commit-Queue: Ömer Ağacan <[email protected]>
  • Loading branch information
osa1 authored and Commit Queue committed Jan 17, 2025
1 parent b93c826 commit d2ce8e7
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions sdk/lib/_internal/wasm/lib/js_string_convert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,14 @@ JSStringImpl? decodeUtf8JS(
bool allowMalformed,
) {
final length = end - start;
if (length >= _shortInputThreshold) {
final JSAny? decoder = allowMalformed ? _decoderNonFatal : _decoder;
if (decoder != null) {
final arrayRef = codeUnits.toJSArrayExternRef(start, length);
final textDecoderResult = _useTextDecoder(
externRefForJSAny(decoder),
arrayRef,
);
if (textDecoderResult != null) {
return textDecoderResult;
}
}
final JSAny? decoder = allowMalformed ? _decoderNonFatal : _decoder;
if (decoder != null) {
final arrayRef = codeUnits.toJSArrayExternRef(start, length);
return _useTextDecoder(externRefForJSAny(decoder), arrayRef);
}
return null;
}

// Always fall back to the Dart implementation for strings shorter than this
// threshold, as there is a large, constant overhead for using `TextDecoder`.
// TODO(omersa): This is copied from dart2js runtime, make sure the value is
// right for dart2wasm.
const int _shortInputThreshold = 15;

JSStringImpl? _useTextDecoder(
WasmExternRef? decoder,
WasmExternRef? codeUnits,
Expand Down

0 comments on commit d2ce8e7

Please sign in to comment.