Skip to content

Commit 45a32ec

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Make use of ImmutableWasmArray in core libraries
This can allow tools such as binaryen or V8 optimize loads from the arrays better. We'd also want to make some other uses of wasm arrays immutable (e.g. `WasmArray<_Type>` used in RTT implementation) but it's currently not easily possible to create immutable wasm arrays of unknown length from another mutable array (see [0]). [0] WebAssembly/gc#570 Change-Id: Ia5270bad238c3dc6ea1086677395091d441d9398 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392904 Reviewed-by: Ömer Ağacan <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent a96ad1b commit 45a32ec

File tree

8 files changed

+32
-21
lines changed

8 files changed

+32
-21
lines changed

pkg/dart2wasm/lib/constants.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ class Constants {
243243
...type.named.map((named) => named.type),
244244
]);
245245
final names = makeArrayOf(coreTypes.stringNonNullableRawType,
246-
type.named.map((t) => StringConstant(t.name)).toList());
246+
type.named.map((t) => StringConstant(t.name)).toList(),
247+
mutable: false);
247248
return _makeTypeConstant(translator.recordTypeClass, type.nullability, {
248249
translator.recordTypeFieldTypesField.fieldReference: fieldTypes,
249250
translator.recordTypeNamesField.fieldReference: names,

pkg/dart2wasm/lib/record_class_generator.dart

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import 'records.dart';
3030
/// Record_2_a(this.$1, this.$2, this.a);
3131
///
3232
/// @pragma('wasm:entry-point')
33-
/// bool _checkRecordType(WasmArray<_Type> types, WasmArray<String> names) {
33+
/// bool _checkRecordType(WasmArray<_Type> types, ImmutableWasmArray<String> names) {
3434
/// if (types.length != 3) return false;
35-
/// if (!identical(names, const WasmArray(["a"]))) return false;
35+
/// if (!identical(names, const ImmutableWasmArray(["a"]))) return false;
3636
///
3737
/// if (!_isSubtype($1, types[0])) return false;
3838
/// if (!_isSubtype($2, types[1])) return false;
@@ -44,7 +44,7 @@ import 'records.dart';
4444
/// @pragma('wasm:entry-point')
4545
/// _Type get _masqueradedRecordRuntimeType =>
4646
/// _RecordType(
47-
/// const WasmArray(["a"]),
47+
/// const ImmutableWasmArray(["a"]),
4848
/// WasmArray.literal([
4949
/// _getMasqueradedRuntimeTypeNullable($1),
5050
/// _getMasqueradedRuntimeTypeNullable($2),
@@ -54,7 +54,7 @@ import 'records.dart';
5454
/// @pragma('wasm:entry-point')
5555
/// _Type get _recordRuntimeType =>
5656
/// _RecordType(
57-
/// const WasmArray(["a"]),
57+
/// const ImmutableWasmArray(["a"]),
5858
/// WasmArray.literal([
5959
/// _getActualRuntimeTypeNullable($1),
6060
/// _getActualRuntimeTypeNullable($2),
@@ -126,6 +126,9 @@ class _RecordClassGenerator {
126126
late final Class wasmArrayClass =
127127
coreTypes.index.getClass('dart:_wasm', 'WasmArray');
128128

129+
late final Class immutableWasmArrayClass =
130+
coreTypes.index.getClass('dart:_wasm', 'ImmutableWasmArray');
131+
129132
late final Procedure wasmArrayRefLength =
130133
coreTypes.index.getProcedure('dart:_wasm', 'WasmArrayRef', 'get:length');
131134

@@ -144,11 +147,16 @@ class _RecordClassGenerator {
144147
late final Field wasmArrayValueField =
145148
coreTypes.index.getField("dart:_wasm", "WasmArray", "_value");
146149

150+
late final Field immutableWasmArrayValueField =
151+
coreTypes.index.getField("dart:_wasm", "ImmutableWasmArray", "_value");
152+
147153
late final InterfaceType wasmArrayOfType = InterfaceType(
148154
wasmArrayClass, Nullability.nonNullable, [nonNullableTypeType]);
149155

150-
late final InterfaceType wasmArrayOfString = InterfaceType(
151-
wasmArrayClass, Nullability.nonNullable, [nonNullableStringType]);
156+
late final InterfaceType immutableWasmArrayOfString = InterfaceType(
157+
immutableWasmArrayClass,
158+
Nullability.nonNullable,
159+
[nonNullableStringType]);
152160

153161
late final InterfaceType runtimeTypeType =
154162
InterfaceType(typeRuntimetypeTypeClass, Nullability.nonNullable);
@@ -414,7 +422,7 @@ class _RecordClassGenerator {
414422
Procedure _generateCheckRecordType(RecordShape shape, List<Field> fields) {
415423
final typesParameter = VariableDeclaration('types', type: wasmArrayOfType);
416424
final namesParameter =
417-
VariableDeclaration('names', type: wasmArrayOfString);
425+
VariableDeclaration('names', type: immutableWasmArrayOfString);
418426

419427
final List<Statement> statements = [];
420428

@@ -502,7 +510,7 @@ class _RecordClassGenerator {
502510
String name, Procedure target, RecordShape shape, List<Field> fields) {
503511
final List<Statement> statements = [];
504512

505-
// const WasmArray(["name1", "name2", ...])
513+
// const ImmutableWasmArray(["name1", "name2", ...])
506514
final fieldNamesList = ConstantExpression(_fieldNamesConstant(shape));
507515

508516
Expression fieldRuntimeTypeExpr(Field field) => StaticInvocation(
@@ -548,10 +556,11 @@ class _RecordClassGenerator {
548556
}
549557

550558
Constant _fieldNamesConstant(RecordShape shape) {
551-
return InstanceConstant(wasmArrayClass.reference, [
559+
return InstanceConstant(immutableWasmArrayClass.reference, [
552560
nonNullableStringType
553561
], {
554-
wasmArrayValueField.fieldReference: ListConstant(nonNullableStringType,
562+
immutableWasmArrayValueField.fieldReference: ListConstant(
563+
nonNullableStringType,
555564
shape.names.map((name) => StringConstant(name)).toList())
556565
});
557566
}

pkg/dart2wasm/lib/types.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ class Types {
202202

203203
final names = translator.constants.makeArrayOf(
204204
translator.coreTypes.stringNonNullableRawType,
205-
type.named.map((t) => StringConstant(t.name)).toList());
205+
type.named.map((t) => StringConstant(t.name)).toList(),
206+
mutable: false);
206207

207208
translator.constants.instantiateConstant(
208209
codeGen.b, names, recordTypeNamesFieldExpectedType);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,7 @@ mixin _ChunkedJsonParser<T> on _JsonParserWithListener {
10671067
* }
10681068
* ```
10691069
*/
1070-
static const WasmArray<WasmI8> _characterAttributes =
1071-
WasmArray<WasmI8>.literal([
1070+
static const _characterAttributes = ImmutableWasmArray<WasmI8>.literal([
10721071
33, 33, 33, 33, 33, 33, 33, 33, 33, 35, 35, 33, 33, 35, 33, 33, 33, 33, //
10731072
33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 32, 33, 32, //
10741073
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, //
@@ -1776,7 +1775,7 @@ class _Utf8Decoder {
17761775
// Non-BMP 'R' = 64 + (2 | flagNonLatin1);
17771776
// Illegal 'a' = 64 + (1 | flagIllegal);
17781777
// Illegal 'b' = 64 + (2 | flagIllegal);
1779-
static const WasmArray<WasmI8> scanTable = WasmArray<WasmI8>.literal([
1778+
static const scanTable = ImmutableWasmArray<WasmI8>.literal([
17801779
// 00-1F
17811780
65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
17821781
65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65,

sdk/lib/_internal/wasm/lib/int_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class int {
210210
}
211211

212212
// For each radix, 2-36, how many digits are guaranteed to fit in an `int`.
213-
static const _PARSE_LIMITS = const WasmArray<WasmI64>.literal([
213+
static const _PARSE_LIMITS = const ImmutableWasmArray<WasmI64>.literal([
214214
0, // unused
215215
0, // unused
216216
63, // radix: 2

sdk/lib/_internal/wasm/lib/record_patch.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ abstract class Record {
1212
_RecordType get _masqueradedRecordRuntimeType;
1313
_RecordType get _recordRuntimeType;
1414

15-
bool _checkRecordType(WasmArray<_Type> types, WasmArray<String> names);
15+
bool _checkRecordType(
16+
WasmArray<_Type> types, ImmutableWasmArray<String> names);
1617

1718
@pragma("wasm:prefer-inline")
1819
static _RecordType _getRecordRuntimeType(Record record) =>

sdk/lib/_internal/wasm/lib/string.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ final class OneByteString extends StringBase {
14521452
// Lower-case conversion table for Latin-1 as string.
14531453
// Upper-case ranges: 0x41-0x5a ('A' - 'Z'), 0xc0-0xd6, 0xd8-0xde.
14541454
// Conversion to lower case performed by adding 0x20.
1455-
static const WasmArray<WasmI8> _LC_TABLE = WasmArray<WasmI8>.literal([
1455+
static const _LC_TABLE = ImmutableWasmArray<WasmI8>.literal([
14561456
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, //
14571457
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, //
14581458
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, //
@@ -1484,7 +1484,7 @@ final class OneByteString extends StringBase {
14841484
// The German "sharp s" \xdf (ß) should be converted into two characters (SS),
14851485
// and is also marked with 0x00.
14861486
// Conversion to lower case performed by subtracting 0x20.
1487-
static const WasmArray<WasmI8> _UC_TABLE = WasmArray<WasmI8>.literal([
1487+
static const _UC_TABLE = ImmutableWasmArray<WasmI8>.literal([
14881488
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, //
14891489
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, //
14901490
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, //

sdk/lib/_internal/wasm/lib/type.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ extension on WasmArray<_NamedParameter> {
5757
}
5858
}
5959

60-
extension on WasmArray<String> {
60+
extension on ImmutableWasmArray<String> {
6161
@pragma("wasm:prefer-inline")
6262
bool get isNotEmpty => length != 0;
6363
}
@@ -544,7 +544,7 @@ class _AbstractRecordType extends _Type {
544544

545545
@pragma("wasm:entry-point")
546546
class _RecordType extends _Type {
547-
final WasmArray<String> names;
547+
final ImmutableWasmArray<String> names;
548548
final WasmArray<_Type> fieldTypes;
549549

550550
@pragma("wasm:entry-point")

0 commit comments

Comments
 (0)