Skip to content

Commit f2929bf

Browse files
mkustermannCommit Queue
authored andcommitted
[dartwasm] Do not preserve details for errors in --minify, move throwing code to (not inlined) slow path
* We outline throwing code to never-inlined function * We inline index/range checking code using fast unsigned compares * We omit details in `--minify` mode The support for omitting details in `--minify` means that the signature shaking pass will remove all parameters of the slow paths in `--minify` mode, making them functions with no arguments that just throw a constant as an exception. Together these changes result in smaller code for common data structures that do index/range checks. Which can have positive effects downstream (e.g. less to inline for wasm runtimes). This leads to around this (in -O2) FluteComplex.Compile.Size.wasm.opt -0.514 % FluteComplex.Compile.Size.wasm.opt.gz -1.664 % Hello.Compile.Size.wasm -2.298 % Hello.Compile.Size.wasm.opt.gz -4.864 % It also leads to (in -O2) around 10% perf improvement on `TypedDataPoly.*` benchmarks. Issue #54395 Change-Id: Icd1cdf28a061f7763bd4bac7cfd5eaeea150c469 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404301 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Ömer Ağacan <[email protected]>
1 parent a565406 commit f2929bf

24 files changed

+602
-479
lines changed

pkg/dart2wasm/lib/constant_evaluator.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import 'target.dart';
1616
class ConstantEvaluator extends kernel.ConstantEvaluator
1717
implements VMConstantEvaluator {
1818
final bool _checkBounds;
19+
final bool _minify;
1920

2021
final Procedure _dartInternalCheckBoundsGetter;
22+
final Procedure _dartInternalMinifyGetter;
2123

2224
ConstantEvaluator(
2325
WasmCompilerOptions options,
@@ -27,8 +29,11 @@ class ConstantEvaluator extends kernel.ConstantEvaluator
2729
ClassHierarchy classHierarchy,
2830
LibraryIndex libraryIndex)
2931
: _checkBounds = !options.translatorOptions.omitBoundsChecks,
32+
_minify = options.translatorOptions.minify,
3033
_dartInternalCheckBoundsGetter = libraryIndex.getTopLevelProcedure(
31-
"dart:_internal", "get:_checkBounds"),
34+
"dart:_internal", "get:checkBounds"),
35+
_dartInternalMinifyGetter =
36+
libraryIndex.getTopLevelProcedure("dart:_internal", "get:minify"),
3237
super(
3338
target.dartLibrarySupport,
3439
target.constantsBackend,
@@ -48,6 +53,9 @@ class ConstantEvaluator extends kernel.ConstantEvaluator
4853
if (target == _dartInternalCheckBoundsGetter) {
4954
return canonicalize(BoolConstant(_checkBounds));
5055
}
56+
if (target == _dartInternalMinifyGetter) {
57+
return canonicalize(BoolConstant(_minify));
58+
}
5159

5260
return super.visitStaticGet(node);
5361
}
@@ -58,5 +66,6 @@ class ConstantEvaluator extends kernel.ConstantEvaluator
5866
// error if they are not).
5967
@override
6068
bool shouldEvaluateMember(Member node) =>
61-
node == _dartInternalCheckBoundsGetter;
69+
node == _dartInternalCheckBoundsGetter ||
70+
node == _dartInternalMinifyGetter;
6271
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import "dart:_error_utils";
56
import 'dart:_boxed_int' show intHashCode;
67
import 'dart:_internal' show doubleToIntBits, intBitsToDouble;
78
import 'dart:_js_helper' show JS, jsStringToDartString;
@@ -362,10 +363,12 @@ final class BoxedDouble implements double {
362363
// See ECMAScript-262, 15.7.4.5 for details.
363364

364365
// Step 2.
365-
// fractionDigits < 0 || fractionDigits > 20
366-
if (fractionDigits.gtU(20)) {
367-
throw new RangeError.range(fractionDigits, 0, 20, "fractionDigits");
368-
}
366+
// 0 <= fractionDigits <= 20
367+
RangeErrorUtils.checkValueBetweenZeroAndPositiveMax(
368+
fractionDigits,
369+
20,
370+
"fractionDigits",
371+
);
369372

370373
// Step 3.
371374
double x = this;
@@ -406,10 +409,12 @@ final class BoxedDouble implements double {
406409

407410
// Step 7.
408411
if (fractionDigits != null) {
409-
// fractionDigits < 0 || fractionDigits > 20
410-
if (fractionDigits.gtU(20)) {
411-
throw new RangeError.range(fractionDigits, 0, 20, "fractionDigits");
412-
}
412+
// 0 <= fractionDigits <= 20
413+
RangeErrorUtils.checkValueBetweenZeroAndPositiveMax(
414+
fractionDigits,
415+
20,
416+
"fractionDigits",
417+
);
413418
}
414419

415420
if (isNaN) return "NaN";
@@ -442,9 +447,7 @@ final class BoxedDouble implements double {
442447
// look at the fractionDigits first.
443448

444449
// Step 8.
445-
if (precision < 1 || precision > 21) {
446-
throw new RangeError.range(precision, 1, 21, "precision");
447-
}
450+
RangeErrorUtils.checkValueInInterval(precision, 1, 21, "precision");
448451

449452
if (isNaN) return "NaN";
450453
if (this == double.infinity) return "Infinity";

sdk/lib/_internal/wasm/lib/boxed_int.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import "dart:_error_utils";
56
import 'dart:_boxed_double';
67
import 'dart:_internal';
78
import 'dart:_wasm';
@@ -306,8 +307,8 @@ final class BoxedInt implements int {
306307

307308
// Returns pow(this, e) % m.
308309
int modPow(int e, int m) {
309-
if (e < 0) throw new RangeError.range(e, 0, null, "exponent");
310-
if (m <= 0) throw new RangeError.range(m, 1, null, "modulus");
310+
RangeErrorUtils.checkNotNegative(e, "exponent");
311+
RangeErrorUtils.checkPositive(m, "modulus");
311312
if (e == 0) return 1;
312313

313314
// This is floor(sqrt(2^63)).
@@ -407,7 +408,7 @@ final class BoxedInt implements int {
407408

408409
// Returns 1/this % m, with m > 0.
409410
int modInverse(int m) {
410-
if (m <= 0) throw new RangeError.range(m, 1, null, "modulus");
411+
RangeErrorUtils.checkPositive(m, "modulus");
411412
if (m == 1) return 0;
412413
int t = this;
413414
// t < 0 || t >= m, m is positive (checked above)

sdk/lib/_internal/wasm/lib/boxed_int_to_string.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:_error_utils';
56
import 'dart:_internal';
67
import 'dart:_string';
78

@@ -17,9 +18,7 @@ class BoxedInt {
1718
const _digits = "0123456789abcdefghijklmnopqrstuvwxyz";
1819

1920
String _intToRadixString(int value, int radix) {
20-
if (radix < 2 || 36 < radix) {
21-
throw new RangeError.range(radix, 2, 36, "radix");
22-
}
21+
RangeErrorUtils.checkValueInInterval(radix, 2, 36, "radix");
2322
if (radix & (radix - 1) == 0) {
2423
return _toPow2String(value, radix);
2524
}

sdk/lib/_internal/wasm/lib/compact_hash.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ class _CompactIterator<E> implements Iterator<E> {
763763
E? _current;
764764

765765
_CompactIterator(this._table, this._data, this._len, this._offset, this._step)
766-
: _checkSum = _table._checkSum;
766+
: _checkSum = _table._checkSum;
767767

768768
bool moveNext() {
769769
if (_table._isModifiedSince(_data, _checkSum)) {
@@ -812,7 +812,7 @@ class _CompactEntriesIterator<K, V> implements Iterator<MapEntry<K, V>> {
812812
MapEntry<K, V>? _current;
813813

814814
_CompactEntriesIterator(this._table, this._data, this._len)
815-
: _checkSum = _table._checkSum;
815+
: _checkSum = _table._checkSum;
816816

817817
bool moveNext() {
818818
if (_table._isModifiedSince(_data, _checkSum)) {
@@ -1248,13 +1248,13 @@ base class CompactLinkedCustomHashSet<E> extends _HashFieldBase
12481248
) : _validKey = validKey ?? TypeTest<E>().test;
12491249

12501250
Set<R> cast<R>() => Set.castFrom<E, R>(this);
1251-
Set<E> toSet() => CompactLinkedCustomHashSet<E>(_equality, _hasher, _validKey)
1252-
..addAll(this);
1251+
Set<E> toSet() =>
1252+
CompactLinkedCustomHashSet<E>(_equality, _hasher, _validKey)
1253+
..addAll(this);
12531254
}
12541255

12551256
@pragma('wasm:prefer-inline')
12561257
Map<K, V> createMapFromKeyValueListUnsafe<K, V>(
12571258
WasmArray<Object?> keyValuePairData,
12581259
int usedData,
1259-
) =>
1260-
DefaultMap<K, V>().._populateUnsafe(keyValuePairData, usedData);
1260+
) => DefaultMap<K, V>().._populateUnsafe(keyValuePairData, usedData);

sdk/lib/_internal/wasm/lib/convert_patch.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import "dart:_compact_hash" show createMapFromKeyValueListUnsafe;
6+
import "dart:_error_utils";
67
import "dart:_internal"
78
show patch, POWERS_OF_TEN, unsafeCast, pushWasmArray, popWasmArray;
89
import "dart:_js_string_convert";
@@ -2006,7 +2007,11 @@ class _Utf8Decoder {
20062007

20072008
@patch
20082009
String convertSingle(List<int> codeUnits, int start, int? maybeEnd) {
2009-
int end = RangeError.checkValidRange(start, maybeEnd, codeUnits.length);
2010+
int end = RangeErrorUtils.checkValidRange(
2011+
start,
2012+
maybeEnd,
2013+
codeUnits.length,
2014+
);
20102015
if (start == end) return "";
20112016

20122017
if (codeUnits is JSUint8ArrayImpl) {
@@ -2103,7 +2108,12 @@ class _Utf8Decoder {
21032108

21042109
@patch
21052110
String convertChunked(List<int> codeUnits, int start, int? maybeEnd) {
2106-
int end = RangeError.checkValidRange(start, maybeEnd, codeUnits.length);
2111+
int end = RangeErrorUtils.checkValidRange(
2112+
start,
2113+
maybeEnd,
2114+
codeUnits.length,
2115+
);
2116+
if (start == end) return "";
21072117

21082118
final U8List bytes;
21092119
int errorOffset;

sdk/lib/_internal/wasm/lib/core_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import "dart:_internal"
99
doubleToIntBits,
1010
EfficientLengthIterable,
1111
FixedLengthListMixin,
12-
indexCheckWithName,
1312
intBitsToDouble,
1413
IterableElementError,
1514
jsonEncode,
@@ -25,6 +24,7 @@ import "dart:_internal"
2524
WasmStringBase,
2625
WasmTypedDataBase;
2726

27+
import 'dart:_error_utils';
2828
import "dart:_internal" as _internal;
2929

3030
import 'dart:_js_helper'

0 commit comments

Comments
 (0)