Skip to content

Commit 219471c

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Only include deferred/dynamic loading code in mjs if needed
This removes 1.6 KB from the default mjs file in case neither deferred loading nor dynamic loading is used. => Fixes size regression introduced by dynamic modules support ([0]) [0] https://dart-review.googlesource.com/c/sdk/+/397721 Change-Id: Id5bba9883117038b1fb4139b16f17a91478c7ded Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/414000 Reviewed-by: Ömer Ağacan <[email protected]> Reviewed-by: Nate Biggs <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent 49a93a0 commit 219471c

File tree

7 files changed

+101
-52
lines changed

7 files changed

+101
-52
lines changed

pkg/dart2wasm/lib/compile.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ Future<CompilationResult> compileToModule(
331331
translator.functions.translatedProcedures,
332332
translator.internalizedStringsForJSRuntime,
333333
translator.options.requireJsStringBuiltin,
334+
translator.options.enableDeferredLoading ||
335+
translator.options.enableMultiModuleStressTestMode ||
336+
translator.dynamicModuleSupportEnabled,
334337
mode);
335338

336339
final supportJs = _generateSupportJs(options.translatorOptions);

pkg/dart2wasm/lib/constant_evaluator.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ class ConstantEvaluator extends kernel.ConstantEvaluator
1818
final bool _checkBounds;
1919
final bool _minify;
2020
final bool _hasDynamicModuleSupport;
21+
final bool _deferredLoadingEnabled;
2122

2223
final Procedure _dartInternalCheckBoundsGetter;
2324
final Procedure _dartInternalMinifyGetter;
2425
final Procedure _dartInternalHasDynamicModuleSupportGetter;
26+
final Procedure _dartInternalDeferredLoadingEnabled;
2527

2628
ConstantEvaluator(
2729
WasmCompilerOptions options,
@@ -33,13 +35,18 @@ class ConstantEvaluator extends kernel.ConstantEvaluator
3335
: _checkBounds = !options.translatorOptions.omitBoundsChecks,
3436
_minify = options.translatorOptions.minify,
3537
_hasDynamicModuleSupport = options.dynamicModuleMainUri != null,
38+
_deferredLoadingEnabled =
39+
options.translatorOptions.enableDeferredLoading ||
40+
options.translatorOptions.enableMultiModuleStressTestMode,
3641
_dartInternalCheckBoundsGetter = libraryIndex.getTopLevelProcedure(
3742
"dart:_internal", "get:checkBounds"),
3843
_dartInternalMinifyGetter =
3944
libraryIndex.getTopLevelProcedure("dart:_internal", "get:minify"),
4045
_dartInternalHasDynamicModuleSupportGetter =
4146
libraryIndex.getTopLevelProcedure(
4247
"dart:_internal", "get:hasDynamicModuleSupport"),
48+
_dartInternalDeferredLoadingEnabled = libraryIndex.getTopLevelProcedure(
49+
"dart:_internal", "get:deferredLoadingEnabled"),
4350
super(
4451
target.dartLibrarySupport,
4552
target.constantsBackend,
@@ -64,6 +71,9 @@ class ConstantEvaluator extends kernel.ConstantEvaluator
6471
if (target == _dartInternalHasDynamicModuleSupportGetter) {
6572
return canonicalize(BoolConstant(_hasDynamicModuleSupport));
6673
}
74+
if (target == _dartInternalDeferredLoadingEnabled) {
75+
return canonicalize(BoolConstant(_deferredLoadingEnabled));
76+
}
6777

6878
return super.visitStaticGet(node);
6979
}
@@ -76,5 +86,6 @@ class ConstantEvaluator extends kernel.ConstantEvaluator
7686
bool shouldEvaluateMember(Member node) =>
7787
node == _dartInternalCheckBoundsGetter ||
7888
node == _dartInternalMinifyGetter ||
79-
node == _dartInternalHasDynamicModuleSupportGetter;
89+
node == _dartInternalHasDynamicModuleSupportGetter ||
90+
node == _dartInternalDeferredLoadingEnabled;
8091
}

pkg/dart2wasm/lib/js/runtime_blob.dart

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -111,53 +111,12 @@ class CompiledApp {
111111
112112
<<JS_STRING_POLYFILL_METHODS>>
113113
114-
const loadModuleFromBytes = async (bytes) => {
115-
const module = await WebAssembly.compile(bytes, this.builtins);
116-
return await WebAssembly.instantiate(module, {
117-
...baseImports,
118-
...additionalImports,
119-
"wasm:js-string": jsStringPolyfill,
120-
"module0": dartInstance.exports,
121-
});
122-
}
123-
124-
const loadModule = async (loader, loaderArgument) => {
125-
const source = await Promise.resolve(loader(loaderArgument));
126-
const module = await ((source instanceof Response)
127-
? WebAssembly.compileStreaming(source, this.builtins)
128-
: WebAssembly.compile(source, this.builtins));
129-
return await WebAssembly.instantiate(module, {
130-
...baseImports,
131-
...additionalImports,
132-
<<JS_POLYFILL_IMPORT>>
133-
"module0": dartInstance.exports,
134-
});
135-
}
136-
137-
const deferredLibraryHelper = {
138-
"loadModule": async (moduleName) => {
139-
if (!loadDeferredWasm) {
140-
throw "No implementation of loadDeferredWasm provided.";
141-
}
142-
return await loadModule(loadDeferredWasm, moduleName);
143-
},
144-
"loadDynamicModuleFromUri": async (uri) => {
145-
if (!loadDynamicModule) {
146-
throw "No implementation of loadDynamicModule provided.";
147-
}
148-
const loadedModule = await loadModule(loadDynamicModule, uri);
149-
return loadedModule.exports.$invokeEntryPoint;
150-
},
151-
"loadDynamicModuleFromBytes": async (bytes) => {
152-
const loadedModule = await loadModuleFromBytes(loadDynamicModule, uri);
153-
return loadedModule.exports.$invokeEntryPoint;
154-
},
155-
};
114+
<<DEFERRED_LIBRARY_HELPER_METHODS>>
156115
157116
dartInstance = await WebAssembly.instantiate(this.module, {
158117
...baseImports,
159118
...additionalImports,
160-
"deferredLibraryHelper": deferredLibraryHelper,
119+
<<MODULE_LOADING_IMPORT>>
161120
<<JS_POLYFILL_IMPORT>>
162121
});
163122
@@ -223,6 +182,51 @@ const jsStringPolyfill = {
223182
};
224183
''';
225184

185+
final moduleLoadingHelperTemplate = Template(r'''
186+
const loadModuleFromBytes = async (bytes) => {
187+
const module = await WebAssembly.compile(bytes, this.builtins);
188+
return await WebAssembly.instantiate(module, {
189+
...baseImports,
190+
...additionalImports,
191+
<<JS_POLYFILL_IMPORT>>
192+
"module0": dartInstance.exports,
193+
});
194+
}
195+
196+
const loadModule = async (loader, loaderArgument) => {
197+
const source = await Promise.resolve(loader(loaderArgument));
198+
const module = await ((source instanceof Response)
199+
? WebAssembly.compileStreaming(source, this.builtins)
200+
: WebAssembly.compile(source, this.builtins));
201+
return await WebAssembly.instantiate(module, {
202+
...baseImports,
203+
...additionalImports,
204+
<<JS_POLYFILL_IMPORT>>
205+
"module0": dartInstance.exports,
206+
});
207+
}
208+
209+
const moduleLoadingHelper = {
210+
"loadModule": async (moduleName) => {
211+
if (!loadDeferredWasm) {
212+
throw "No implementation of loadDeferredWasm provided.";
213+
}
214+
return await loadModule(loadDeferredWasm, moduleName);
215+
},
216+
"loadDynamicModuleFromUri": async (uri) => {
217+
if (!loadDynamicModule) {
218+
throw "No implementation of loadDynamicModule provided.";
219+
}
220+
const loadedModule = await loadModule(loadDynamicModule, uri);
221+
return loadedModule.exports.$invokeEntryPoint;
222+
},
223+
"loadDynamicModuleFromBytes": async (bytes) => {
224+
const loadedModule = await loadModuleFromBytes(loadDynamicModule, uri);
225+
return loadedModule.exports.$invokeEntryPoint;
226+
},
227+
};
228+
''');
229+
226230
class Template {
227231
static final _templateVariableRegExp = RegExp(r'<<(?<varname>[A-Z_]+)>>');
228232
final List<_TemplatePart> _parts = [];

pkg/dart2wasm/lib/js/runtime_generator.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class RuntimeFinalizer {
5858
Iterable<Procedure> translatedProcedures,
5959
List<String> constantStrings,
6060
bool requireJsBuiltin,
61+
bool supportsAdditionalModuleLoading,
6162
wasm_target.Mode mode) {
6263
String escape(String s) => json.encode(s);
6364

@@ -104,13 +105,30 @@ class RuntimeFinalizer {
104105
''';
105106
}
106107

108+
final jsStringBuiltinPolyfillImportVars = {
109+
'JS_POLYFILL_IMPORT':
110+
requireJsBuiltin ? '' : '"wasm:js-string": jsStringPolyfill,',
111+
};
112+
final moduleLoadingImportVars = {
113+
'MODULE_LOADING_IMPORT': supportsAdditionalModuleLoading
114+
? '"moduleLoadingHelper": moduleLoadingHelper,'
115+
: '',
116+
};
117+
118+
final moduleLoadingHelperMethods = supportsAdditionalModuleLoading
119+
? moduleLoadingHelperTemplate.instantiate({
120+
...jsStringBuiltinPolyfillImportVars,
121+
})
122+
: '';
123+
107124
return jsRuntimeBlobTemplate.instantiate({
125+
...jsStringBuiltinPolyfillImportVars,
126+
...moduleLoadingImportVars,
108127
'BUILTINS_MAP_BODY': builtins.join(', '),
109128
'JS_METHODS': jsMethods.toString(),
110129
'IMPORTED_JS_STRINGS_IN_MJS': internalizedStrings,
111130
'JS_STRING_POLYFILL_METHODS': requireJsBuiltin ? '' : jsPolyFillMethods,
112-
'JS_POLYFILL_IMPORT':
113-
requireJsBuiltin ? '' : '"wasm:js-string": jsStringPolyfill,'
131+
'DEFERRED_LIBRARY_HELPER_METHODS': moduleLoadingHelperMethods,
114132
});
115133
}
116134
}

sdk/lib/_internal/wasm/lib/deferred.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final Map<String, Set<String>> _loadedLibraries = {};
1010

1111
external Map<String, Map<String, List<String>>> get _importMapping;
1212

13-
@pragma("wasm:import", "deferredLibraryHelper.loadModule")
13+
@pragma("wasm:import", "moduleLoadingHelper.loadModule")
1414
external WasmExternRef _loadModule(WasmExternRef moduleName);
1515

1616
class DeferredNotLoadedError extends Error implements NoSuchMethodError {
@@ -49,6 +49,10 @@ Future<void> loadLibrary(String enclosingLibrary, String importPrefix) {
4949
return Future.value();
5050
}
5151

52+
if (!deferredLoadingEnabled) {
53+
throw DeferredLoadException('Compiler did not enable deferred loading.');
54+
}
55+
5256
// Start loading modules
5357
final List<Future> loadFutures = [];
5458
for (final moduleName in moduleNames) {

sdk/lib/_internal/wasm/lib/dynamic_module.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
part of "internal_patch.dart";
66

7-
@pragma("wasm:import", "deferredLibraryHelper.loadDynamicModuleFromUri")
7+
@pragma("wasm:import", "moduleLoadingHelper.loadDynamicModuleFromUri")
88
external WasmExternRef _loadModuleFromUri(WasmExternRef moduleUri);
99

10-
@pragma("wasm:import", "deferredLibraryHelper.loadDynamicModuleFromBytes")
10+
@pragma("wasm:import", "moduleLoadingHelper.loadDynamicModuleFromBytes")
1111
external WasmExternRef _loadModuleFromBytes(WasmExternRef moduleUri);
1212

1313
@patch

sdk/lib/_internal/wasm/lib/internal_patch.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ String jsonEncode(String object) => jsStringToDartString(
194194
/// Bounds checks are disabled with `--omit-bounds-checks`, which is implied by
195195
/// `-O4`.
196196
///
197-
/// Reads of this variable is evaluated before the TFA by the constant
197+
/// Reads of this variable are evaluated before the TFA by the constant
198198
/// evaluator, and its value depends on `--omit-bounds-checks`.
199199
external bool get checkBounds;
200200

@@ -203,7 +203,7 @@ external bool get checkBounds;
203203
/// If minification is on we do not retain specific error message details (e.g.
204204
/// omit the index in index errors).
205205
///
206-
/// Reads of this variable is evaluated before the TFA by the constant
206+
/// Reads of this variable are evaluated before the TFA by the constant
207207
/// evaluator, and its value depends on `--minify`.
208208
external bool get minify;
209209

@@ -212,10 +212,19 @@ external bool get minify;
212212
/// Enables shortcuts in some runtime logic if it is known that no support is
213213
/// needed for dynamic modules.
214214
///
215-
/// Reads of this variable is evaluated before the TFA by the constant
215+
/// Reads of this variable are evaluated before the TFA by the constant
216216
/// evaluator, and its value depends on `--dynamic-module-main`.
217217
external bool get hasDynamicModuleSupport;
218218

219+
/// Whether deferred loading is enabled.
220+
///
221+
/// If true, then there may be deferred modules that can be loaded at runtime.
222+
///
223+
/// Reads of this variable are evaluated before the TFA by the constant
224+
/// evaluator, and its value depends on `--enable-deferred-loading` and
225+
/// `--enable-multi-module-stress-test-mode`.
226+
external bool get deferredLoadingEnabled;
227+
219228
/// Compiler intrinsic to push an element to a Wasm array in a class field or
220229
/// variable.
221230
///

0 commit comments

Comments
 (0)