From d2ce8e71c8a6ae065896efee20b6385c4204d1de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Fri, 17 Jan 2025 05:10:40 -0800 Subject: [PATCH] [dart2wasm] Remove short input threshold to fall back to native UTF-8 decoder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Commit-Queue: Ömer Ağacan --- .../_internal/wasm/lib/js_string_convert.dart | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/sdk/lib/_internal/wasm/lib/js_string_convert.dart b/sdk/lib/_internal/wasm/lib/js_string_convert.dart index cb199c61d166..8ab8ef79c1fd 100644 --- a/sdk/lib/_internal/wasm/lib/js_string_convert.dart +++ b/sdk/lib/_internal/wasm/lib/js_string_convert.dart @@ -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,