[dart2wasm] Introduce a --minify flag to dart2wasm that uses "minified:Class<cid>" for class names

The `minified:...` encoding of class names mirrors the one used by
dart2js. So the added test will work in both modes.

This reduces optimized dart2wasm output

* hello world by 20%
* flute complex by 8.5%

Change-Id: I080de40919ee3f25f0f0d8c9b82aa662f7e734aa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/347741
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Martin Kustermann 2024-01-24 09:10:15 +00:00 committed by Commit Queue
parent 1c8a39e07c
commit 7f668b63c3
5 changed files with 46 additions and 7 deletions

View file

@ -27,6 +27,8 @@ final List<Option> options = [
defaultsTo: _d.translatorOptions.inlining),
Flag("name-section", (o, value) => o.translatorOptions.nameSection = value,
defaultsTo: _d.translatorOptions.nameSection),
Flag("minify", (o, value) => o.translatorOptions.minify = value,
defaultsTo: _d.translatorOptions.minify),
Flag("polymorphic-specialization",
(o, value) => o.translatorOptions.polymorphicSpecialization = value,
defaultsTo: _d.translatorOptions.polymorphicSpecialization),

View file

@ -38,6 +38,7 @@ class TranslatorOptions {
bool polymorphicSpecialization = false;
bool printKernel = false;
bool printWasm = false;
bool minify = false;
bool verifyTypeChecks = false;
int inliningLimit = 0;
int? sharedMemoryMaxPages;

View file

@ -290,13 +290,16 @@ class Types {
final arrayOfStringType = InterfaceType(
translator.wasmArrayClass, Nullability.nonNullable, [stringType]);
final arrayOfStrings = translator.constants.makeArrayOf(
stringType, [for (final name in typeNames) StringConstant(name)]);
final typeNamesType =
translator.translateStorageType(arrayOfStringType).unpacked;
translator.constants
.instantiateConstant(null, b, arrayOfStrings, typeNamesType);
if (translator.options.minify) {
b.ref_null((typeNamesType as w.RefType).heapType);
} else {
final arrayOfStrings = translator.constants.makeArrayOf(
stringType, [for (final name in typeNames) StringConstant(name)]);
translator.constants
.instantiateConstant(null, b, arrayOfStrings, typeNamesType);
}
return typeNamesType;
}

View file

@ -303,7 +303,7 @@ class _InterfaceType extends _Type {
@override
String toString() {
StringBuffer s = StringBuffer();
s.write(_getTypeNames()[classId]);
s.write(_getTypeNames()?[classId] ?? 'minified:Class${classId}');
if (typeArguments.isNotEmpty) {
s.write("<");
for (int i = 0; i < typeArguments.length; i++) {
@ -602,7 +602,7 @@ class _RecordType extends _Type {
external WasmArray<WasmArray<WasmI32>> _getTypeRulesSupers();
external WasmArray<WasmArray<WasmArray<_Type>>> _getTypeRulesSubstitutions();
external WasmArray<String> _getTypeNames();
external WasmArray<String>? _getTypeNames();
/// Type parameter environment used while comparing function types.
///

View file

@ -0,0 +1,33 @@
// 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.
// dart2jsOptions=--minify
// dart2wasmOptions=--minify
import 'package:expect/expect.dart';
import 'package:expect/config.dart';
void main() {
// This test is specific to testing dart2wasm & dart2js.
if (!isDart2jsConfiguration && !isDart2WasmConfiguration) return;
final obj = int.parse('1') == 1 ? Foo<Bar>() : Foo<Baz>();
final runtimeType = obj.runtimeType.toString();
final match = RegExp(r'^minified:[A-Za-z0-9]+<minified:[A-Za-z0-9]+>$')
.matchAsPrefix(runtimeType);
Expect.isNotNull(
match,
'Foo<Bar>().runtimeType should have format '
'minified:XXX<minified:YYY> but was $runtimeType');
Expect.isTrue(obj.isT(Bar()));
Expect.isTrue(!obj.isT(Baz()));
}
class Foo<T> {
bool isT(Object obj) => obj is T;
}
class Bar {}
class Baz {}