Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,7 @@
<link rel="preload" href="./blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./System.Private.CoreLib.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./advanced-sample.lib.module.js" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withConfig({
startupMemoryCache: true,
resources: {
modulesAfterConfigLoaded: {
"advanced-sample.lib.module.js": ""
Expand Down
3 changes: 0 additions & 3 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,9 +12,6 @@
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./_framework/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
5 changes: 4 additions & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,14 +31,17 @@ try {
}

const runtime = await dotnet
.withConfig({
maxParallelDownloads: 10000,
Comment thread
kg marked this conversation as resolved.
// diagnosticTracing:true,
})
.withModuleConfig({
printErr: () => undefined,
print: () => undefined,
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
})
.create();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/features.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -193,12 +193,13 @@ See also [fetch integrity on MDN](https://developer.mozilla.org/en-US/docs/Web/A

### Pre-fetching
In order to start downloading application resources as soon as possible you can add HTML elements to `<head>` of your page similar to:
Adding too many files into prefetch could be counterproductive.
Please benchmark your startup performance on real target devices and with realistic network conditions.

```html
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
```

See also [link rel prefetch on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import type { RuntimeAPI } from "./types";

import { Module, linkerDisableLegacyJsInterop, exportedRuntimeAPI, passEmscriptenInternals, runtimeHelpers, setRuntimeGlobals, } from "./globals";
import { GlobalObjects, is_nullish } from "./types/internal";
import { configureEmscriptenStartup, configureWorkerStartup } from "./startup";
import { configureEmscriptenStartup, configureRuntimeStartup, configureWorkerStartup } from "./startup";

import { create_weak_ref } from "./weak-ref";
import { export_internal } from "./exports-internal";
Expand DownExpand Up@@ -143,5 +143,5 @@ class RuntimeList {

// export external API
export {
passEmscriptenInternals, initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
passEmscriptenInternals, initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
};
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,6 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
gitHash,
allAssetsInMemory: createPromiseController<void>(),
dotnetReady: createPromiseController<any>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),
afterInstantiateWasm: createPromiseController<void>(),
beforePreInit: createPromiseController<void>(),
afterPreInit: createPromiseController<void>(),
Expand Down
18 changes: 7 additions & 11 deletions src/mono/wasm/runtime/loader/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,8 +78,6 @@ const containedInSnapshotByAssetTypes: {
"pdb": true,
"heap": true,
"icu": true,
...jsModulesAssetTypes,
"dotnetwasm": true,
};

// these assets are instantiated differently than the main flow
Expand All@@ -95,7 +93,7 @@ export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != loaderHelpers.preferredIcuAsset);
}

function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
function convert_single_asset(assetsCollection: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
const keys = Object.keys(resource || {});
mono_assert(keys.length == 1, `Expect to have one ${behavior} asset in resources`);

Expand All@@ -110,7 +108,7 @@ function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: Res
set_single_asset(asset);

// so that we can use it on the worker too
modulesAssets.push(asset);
assetsCollection.push(asset);
return asset;
}

Expand DownExpand Up@@ -168,15 +166,12 @@ export async function mono_download_assets(): Promise<void> {
countAndStartDownload(asset);
}

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

// continue after we know if memory snapshot is available or not
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
await loaderHelpers.memorySnapshotSkippedOrDone.promise;

// start fetching assets in parallel, only if memory snapshot is not available.
for (const asset of containedInSnapshotAssets) {
if (!runtimeHelpers.loadedMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
countAndStartDownload(asset);
} else {
// Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download.
Expand All@@ -193,6 +188,8 @@ export async function mono_download_assets(): Promise<void> {
}

loaderHelpers.allDownloadsQueued.promise_control.resolve();

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All@@ -211,7 +208,6 @@ export async function mono_download_assets(): Promise<void> {
// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped

await runtimeHelpers.beforeOnRuntimeInitialized.promise;
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
runtimeHelpers.instantiate_asset(asset, url, data);
}
} else {
Expand DownExpand Up@@ -284,7 +280,7 @@ export function prepareAssets() {
mono_assert(resources.jsModuleNative, "resources.jsModuleNative must be defined");
mono_assert(resources.jsModuleRuntime, "resources.jsModuleRuntime must be defined");
mono_assert(!MonoWasmThreads || resources.jsModuleWorker, "resources.jsModuleWorker must be defined");
convert_single_asset(modulesAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(alwaysLoadedAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(modulesAssets, resources.jsModuleNative, "js-module-native");
convert_single_asset(modulesAssets, resources.jsModuleRuntime, "js-module-runtime");
if (MonoWasmThreads) {
Expand Down
19 changes: 11 additions & 8 deletions src/mono/wasm/runtime/loader/config.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { mono_assert } from "./globals";

export function deep_merge_config(target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand DownExpand Up@@ -220,15 +221,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi
await loaderHelpers.afterConfigLoaded.promise;
return;
}
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
return;
}
mono_log_debug("mono_wasm_load_config");
try {
await loadBootConfig(module);
configLoaded = true;
if (configFilePath) {
mono_log_debug("mono_wasm_load_config");
await loadBootConfig(module);
}

normalizeConfig();

Expand All@@ -249,7 +247,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi

normalizeConfig();

mono_assert(!loaderHelpers.config.startupMemoryCache || !module.instantiateWasm, "startupMemoryCache is not supported with Module.instantiateWasm");

loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
if (!loaderHelpers.config.startupMemoryCache) {
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err} ${(err as Error)?.stack}`;
loaderHelpers.config = module.config = Object.assign(loaderHelpers.config, { message: errMessage, error: err, isError: true });
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/loader/exit.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,9 +122,9 @@ function abort_promises(reason: any) {
loaderHelpers.afterConfigLoaded.promise_control.reject(reason);
loaderHelpers.wasmDownloadPromise.promise_control.reject(reason);
loaderHelpers.runtimeModuleLoaded.promise_control.reject(reason);
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
if (runtimeHelpers.dotnetReady) {
runtimeHelpers.dotnetReady.promise_control.reject(reason);
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
runtimeHelpers.afterInstantiateWasm.promise_control.reject(reason);
runtimeHelpers.beforePreInit.promise_control.reject(reason);
runtimeHelpers.afterPreInit.promise_control.reject(reason);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/loader/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,7 @@ export function setLoaderGlobals(
allDownloadsQueued: createPromiseController<void>(),
wasmDownloadPromise: createPromiseController<AssetEntryInternal>(),
runtimeModuleLoaded: createPromiseController<void>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),

is_exited,
is_runtime_running,
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -454,10 +454,11 @@ function importModules() {
}

async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) {
const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { default: emscriptenFactory } = es6Modules[1];
setRuntimeGlobals(globalObjectsRoot);
initializeExports(globalObjectsRoot);
await configureRuntimeStartup();
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
Expand DownExpand Up@@ -494,9 +495,8 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
mono_exit(1, err);
});

init_globalization();

setTimeout(() => {
init_globalization();
mono_download_assets(); // intentionally not awaited
}, 0);

Expand Down
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/snapshot.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,22 +44,35 @@ async function openCache(): Promise<Cache | null> {
}
}

export async function getMemorySnapshotSize(): Promise<number | undefined> {
export async function checkMemorySnapshotSize(): Promise<void> {
try {
if (!runtimeHelpers.config.startupMemoryCache) {
// we could start downloading DLLs because snapshot is disabled
return;
}

const cacheKey = await getCacheKey();
if (!cacheKey) {
return undefined;
return;
}
const cache = await openCache();
if (!cache) {
return undefined;
return;
}
const res = await cache.match(cacheKey);
const contentLength = res?.headers.get("content-length");
return contentLength ? parseInt(contentLength) : undefined;
const memorySize = contentLength ? parseInt(contentLength) : undefined;

runtimeHelpers.loadedMemorySnapshotSize = memorySize;
runtimeHelpers.storeMemorySnapshotPending = !memorySize;
} catch (ex) {
mono_log_warn("Failed find memory snapshot in the cache", ex);
return undefined;
}
finally {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
// we could start downloading DLLs because there is no snapshot yet
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
}
}

Expand Down
46 changes: 19 additions & 27 deletions src/mono/wasm/runtime/startup.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ import { instantiate_wasm_asset, wait_for_all_assets } from "./assets";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { replace_linker_placeholders } from "./exports-binding";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { getMemorySnapshot, storeMemorySnapshot, getMemorySnapshotSize } from "./snapshot";
import { checkMemorySnapshotSize, getMemorySnapshot, storeMemorySnapshot } from "./snapshot";
import { mono_log_debug, mono_log_error, mono_log_warn, mono_set_thread_id } from "./logging";

// threads
Expand All@@ -40,6 +40,19 @@ import { assertNoProxies } from "./gc-handles";
// default size if MonoConfig.pthreadPoolSize is undefined
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

// we are making emscripten startup async friendly
// emscripten is executing the events without awaiting it and so we need to block progress via PromiseControllers above
export function configureEmscriptenStartup(module: DotnetModuleInternal): void {
Expand DownExpand Up@@ -118,8 +131,6 @@ function instantiateWasm(

const mark = startMeasure();
if (userInstantiateWasm) {
// user wasm instantiation doesn't support memory snapshots
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
const exports = userInstantiateWasm(imports, (instance: WebAssembly.Instance, module: WebAssembly.Module | undefined) => {
endMeasure(mark, MeasuredBlock.instantiateWasm);
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
Expand DownExpand Up@@ -377,14 +388,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
Expand DownExpand Up@@ -459,20 +462,9 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
let memorySize: number | undefined = undefined;
await loaderHelpers.afterConfigLoaded;
mono_log_debug("instantiate_wasm_module");

if (runtimeHelpers.config.startupMemoryCache) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}

await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

Expand All@@ -486,19 +478,19 @@ async function instantiate_wasm_module(

mono_log_debug("instantiate_wasm_module done");

if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
try {
const wasmMemory = Module.getMemory();

// .grow() takes a delta compared to the previous size
wasmMemory.grow((memorySize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
wasmMemory.grow((runtimeHelpers.loadedMemorySnapshotSize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
Comment thread
kg marked this conversation as resolved.
runtimeHelpers.updateMemoryViews();
} catch (err) {
mono_log_warn("failed to resize memory for the snapshot", err);
runtimeHelpers.loadedMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshotSize = undefined;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
} catch (err) {
Expand All@@ -511,7 +503,7 @@ async function instantiate_wasm_module(

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
// get the bytes after we re-sized the memory, so that we don't have too much memory in use at the same time
const memoryBytes = await getMemorySnapshot();
const heapU8 = localHeapViewU8();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,7 @@
<link rel="preload" href="./blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./System.Private.CoreLib.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./advanced-sample.lib.module.js" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withConfig({
startupMemoryCache: true,
resources: {
modulesAfterConfigLoaded: {
"advanced-sample.lib.module.js": ""
Expand Down
3 changes: 0 additions & 3 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,9 +12,6 @@
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./_framework/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
5 changes: 4 additions & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,14 +31,17 @@ try {
}

const runtime = await dotnet
.withConfig({
maxParallelDownloads: 10000,
Comment thread
kg marked this conversation as resolved.
// diagnosticTracing:true,
})
.withModuleConfig({
printErr: () => undefined,
print: () => undefined,
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
})
.create();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/features.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -193,12 +193,13 @@ See also [fetch integrity on MDN](https://developer.mozilla.org/en-US/docs/Web/A

### Pre-fetching
In order to start downloading application resources as soon as possible you can add HTML elements to `<head>` of your page similar to:
Adding too many files into prefetch could be counterproductive.
Please benchmark your startup performance on real target devices and with realistic network conditions.

```html
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
```

See also [link rel prefetch on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import type { RuntimeAPI } from "./types";

import { Module, linkerDisableLegacyJsInterop, exportedRuntimeAPI, passEmscriptenInternals, runtimeHelpers, setRuntimeGlobals, } from "./globals";
import { GlobalObjects, is_nullish } from "./types/internal";
import { configureEmscriptenStartup, configureWorkerStartup } from "./startup";
import { configureEmscriptenStartup, configureRuntimeStartup, configureWorkerStartup } from "./startup";

import { create_weak_ref } from "./weak-ref";
import { export_internal } from "./exports-internal";
Expand DownExpand Up@@ -143,5 +143,5 @@ class RuntimeList {

// export external API
export {
passEmscriptenInternals, initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
passEmscriptenInternals, initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
};
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,6 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
gitHash,
allAssetsInMemory: createPromiseController<void>(),
dotnetReady: createPromiseController<any>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),
afterInstantiateWasm: createPromiseController<void>(),
beforePreInit: createPromiseController<void>(),
afterPreInit: createPromiseController<void>(),
Expand Down
18 changes: 7 additions & 11 deletions src/mono/wasm/runtime/loader/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,8 +78,6 @@ const containedInSnapshotByAssetTypes: {
"pdb": true,
"heap": true,
"icu": true,
...jsModulesAssetTypes,
"dotnetwasm": true,
};

// these assets are instantiated differently than the main flow
Expand All@@ -95,7 +93,7 @@ export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != loaderHelpers.preferredIcuAsset);
}

function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
function convert_single_asset(assetsCollection: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
const keys = Object.keys(resource || {});
mono_assert(keys.length == 1, `Expect to have one ${behavior} asset in resources`);

Expand All@@ -110,7 +108,7 @@ function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: Res
set_single_asset(asset);

// so that we can use it on the worker too
modulesAssets.push(asset);
assetsCollection.push(asset);
return asset;
}

Expand DownExpand Up@@ -168,15 +166,12 @@ export async function mono_download_assets(): Promise<void> {
countAndStartDownload(asset);
}

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

// continue after we know if memory snapshot is available or not
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
await loaderHelpers.memorySnapshotSkippedOrDone.promise;

// start fetching assets in parallel, only if memory snapshot is not available.
for (const asset of containedInSnapshotAssets) {
if (!runtimeHelpers.loadedMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
countAndStartDownload(asset);
} else {
// Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download.
Expand All@@ -193,6 +188,8 @@ export async function mono_download_assets(): Promise<void> {
}

loaderHelpers.allDownloadsQueued.promise_control.resolve();

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All@@ -211,7 +208,6 @@ export async function mono_download_assets(): Promise<void> {
// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped

await runtimeHelpers.beforeOnRuntimeInitialized.promise;
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
runtimeHelpers.instantiate_asset(asset, url, data);
}
} else {
Expand DownExpand Up@@ -284,7 +280,7 @@ export function prepareAssets() {
mono_assert(resources.jsModuleNative, "resources.jsModuleNative must be defined");
mono_assert(resources.jsModuleRuntime, "resources.jsModuleRuntime must be defined");
mono_assert(!MonoWasmThreads || resources.jsModuleWorker, "resources.jsModuleWorker must be defined");
convert_single_asset(modulesAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(alwaysLoadedAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(modulesAssets, resources.jsModuleNative, "js-module-native");
convert_single_asset(modulesAssets, resources.jsModuleRuntime, "js-module-runtime");
if (MonoWasmThreads) {
Expand Down
19 changes: 11 additions & 8 deletions src/mono/wasm/runtime/loader/config.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { mono_assert } from "./globals";

export function deep_merge_config(target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand DownExpand Up@@ -220,15 +221,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi
await loaderHelpers.afterConfigLoaded.promise;
return;
}
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
return;
}
mono_log_debug("mono_wasm_load_config");
try {
await loadBootConfig(module);
configLoaded = true;
if (configFilePath) {
mono_log_debug("mono_wasm_load_config");
await loadBootConfig(module);
}

normalizeConfig();

Expand All@@ -249,7 +247,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi

normalizeConfig();

mono_assert(!loaderHelpers.config.startupMemoryCache || !module.instantiateWasm, "startupMemoryCache is not supported with Module.instantiateWasm");

loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
if (!loaderHelpers.config.startupMemoryCache) {
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err} ${(err as Error)?.stack}`;
loaderHelpers.config = module.config = Object.assign(loaderHelpers.config, { message: errMessage, error: err, isError: true });
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/loader/exit.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,9 +122,9 @@ function abort_promises(reason: any) {
loaderHelpers.afterConfigLoaded.promise_control.reject(reason);
loaderHelpers.wasmDownloadPromise.promise_control.reject(reason);
loaderHelpers.runtimeModuleLoaded.promise_control.reject(reason);
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
if (runtimeHelpers.dotnetReady) {
runtimeHelpers.dotnetReady.promise_control.reject(reason);
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
runtimeHelpers.afterInstantiateWasm.promise_control.reject(reason);
runtimeHelpers.beforePreInit.promise_control.reject(reason);
runtimeHelpers.afterPreInit.promise_control.reject(reason);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/loader/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,7 @@ export function setLoaderGlobals(
allDownloadsQueued: createPromiseController<void>(),
wasmDownloadPromise: createPromiseController<AssetEntryInternal>(),
runtimeModuleLoaded: createPromiseController<void>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),

is_exited,
is_runtime_running,
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -454,10 +454,11 @@ function importModules() {
}

async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) {
const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { default: emscriptenFactory } = es6Modules[1];
setRuntimeGlobals(globalObjectsRoot);
initializeExports(globalObjectsRoot);
await configureRuntimeStartup();
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
Expand DownExpand Up@@ -494,9 +495,8 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
mono_exit(1, err);
});

init_globalization();

setTimeout(() => {
init_globalization();
mono_download_assets(); // intentionally not awaited
}, 0);

Expand Down
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/snapshot.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,22 +44,35 @@ async function openCache(): Promise<Cache | null> {
}
}

export async function getMemorySnapshotSize(): Promise<number | undefined> {
export async function checkMemorySnapshotSize(): Promise<void> {
try {
if (!runtimeHelpers.config.startupMemoryCache) {
// we could start downloading DLLs because snapshot is disabled
return;
}

const cacheKey = await getCacheKey();
if (!cacheKey) {
return undefined;
return;
}
const cache = await openCache();
if (!cache) {
return undefined;
return;
}
const res = await cache.match(cacheKey);
const contentLength = res?.headers.get("content-length");
return contentLength ? parseInt(contentLength) : undefined;
const memorySize = contentLength ? parseInt(contentLength) : undefined;

runtimeHelpers.loadedMemorySnapshotSize = memorySize;
runtimeHelpers.storeMemorySnapshotPending = !memorySize;
} catch (ex) {
mono_log_warn("Failed find memory snapshot in the cache", ex);
return undefined;
}
finally {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
// we could start downloading DLLs because there is no snapshot yet
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
}
}

Expand Down
46 changes: 19 additions & 27 deletions src/mono/wasm/runtime/startup.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ import { instantiate_wasm_asset, wait_for_all_assets } from "./assets";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { replace_linker_placeholders } from "./exports-binding";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { getMemorySnapshot, storeMemorySnapshot, getMemorySnapshotSize } from "./snapshot";
import { checkMemorySnapshotSize, getMemorySnapshot, storeMemorySnapshot } from "./snapshot";
import { mono_log_debug, mono_log_error, mono_log_warn, mono_set_thread_id } from "./logging";

// threads
Expand All@@ -40,6 +40,19 @@ import { assertNoProxies } from "./gc-handles";
// default size if MonoConfig.pthreadPoolSize is undefined
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

// we are making emscripten startup async friendly
// emscripten is executing the events without awaiting it and so we need to block progress via PromiseControllers above
export function configureEmscriptenStartup(module: DotnetModuleInternal): void {
Expand DownExpand Up@@ -118,8 +131,6 @@ function instantiateWasm(

const mark = startMeasure();
if (userInstantiateWasm) {
// user wasm instantiation doesn't support memory snapshots
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
const exports = userInstantiateWasm(imports, (instance: WebAssembly.Instance, module: WebAssembly.Module | undefined) => {
endMeasure(mark, MeasuredBlock.instantiateWasm);
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
Expand DownExpand Up@@ -377,14 +388,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
Expand DownExpand Up@@ -459,20 +462,9 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
let memorySize: number | undefined = undefined;
await loaderHelpers.afterConfigLoaded;
mono_log_debug("instantiate_wasm_module");

if (runtimeHelpers.config.startupMemoryCache) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}

await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

Expand All@@ -486,19 +478,19 @@ async function instantiate_wasm_module(

mono_log_debug("instantiate_wasm_module done");

if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
try {
const wasmMemory = Module.getMemory();

// .grow() takes a delta compared to the previous size
wasmMemory.grow((memorySize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
wasmMemory.grow((runtimeHelpers.loadedMemorySnapshotSize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
Comment thread
kg marked this conversation as resolved.
runtimeHelpers.updateMemoryViews();
} catch (err) {
mono_log_warn("failed to resize memory for the snapshot", err);
runtimeHelpers.loadedMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshotSize = undefined;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
} catch (err) {
Expand All@@ -511,7 +503,7 @@ async function instantiate_wasm_module(

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
// get the bytes after we re-sized the memory, so that we don't have too much memory in use at the same time
const memoryBytes = await getMemorySnapshot();
const heapU8 = localHeapViewU8();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,7 @@
<link rel="preload" href="./blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./System.Private.CoreLib.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./advanced-sample.lib.module.js" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withConfig({
startupMemoryCache: true,
resources: {
modulesAfterConfigLoaded: {
"advanced-sample.lib.module.js": ""
Expand Down
3 changes: 0 additions & 3 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,9 +12,6 @@
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./_framework/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
5 changes: 4 additions & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,14 +31,17 @@ try {
}

const runtime = await dotnet
.withConfig({
maxParallelDownloads: 10000,
Comment thread
kg marked this conversation as resolved.
// diagnosticTracing:true,
})
.withModuleConfig({
printErr: () => undefined,
print: () => undefined,
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
})
.create();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/features.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -193,12 +193,13 @@ See also [fetch integrity on MDN](https://developer.mozilla.org/en-US/docs/Web/A

### Pre-fetching
In order to start downloading application resources as soon as possible you can add HTML elements to `<head>` of your page similar to:
Adding too many files into prefetch could be counterproductive.
Please benchmark your startup performance on real target devices and with realistic network conditions.

```html
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
```

See also [link rel prefetch on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import type { RuntimeAPI } from "./types";

import { Module, linkerDisableLegacyJsInterop, exportedRuntimeAPI, passEmscriptenInternals, runtimeHelpers, setRuntimeGlobals, } from "./globals";
import { GlobalObjects, is_nullish } from "./types/internal";
import { configureEmscriptenStartup, configureWorkerStartup } from "./startup";
import { configureEmscriptenStartup, configureRuntimeStartup, configureWorkerStartup } from "./startup";

import { create_weak_ref } from "./weak-ref";
import { export_internal } from "./exports-internal";
Expand DownExpand Up@@ -143,5 +143,5 @@ class RuntimeList {

// export external API
export {
passEmscriptenInternals, initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
passEmscriptenInternals, initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
};
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,6 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
gitHash,
allAssetsInMemory: createPromiseController<void>(),
dotnetReady: createPromiseController<any>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),
afterInstantiateWasm: createPromiseController<void>(),
beforePreInit: createPromiseController<void>(),
afterPreInit: createPromiseController<void>(),
Expand Down
18 changes: 7 additions & 11 deletions src/mono/wasm/runtime/loader/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,8 +78,6 @@ const containedInSnapshotByAssetTypes: {
"pdb": true,
"heap": true,
"icu": true,
...jsModulesAssetTypes,
"dotnetwasm": true,
};

// these assets are instantiated differently than the main flow
Expand All@@ -95,7 +93,7 @@ export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != loaderHelpers.preferredIcuAsset);
}

function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
function convert_single_asset(assetsCollection: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
const keys = Object.keys(resource || {});
mono_assert(keys.length == 1, `Expect to have one ${behavior} asset in resources`);

Expand All@@ -110,7 +108,7 @@ function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: Res
set_single_asset(asset);

// so that we can use it on the worker too
modulesAssets.push(asset);
assetsCollection.push(asset);
return asset;
}

Expand DownExpand Up@@ -168,15 +166,12 @@ export async function mono_download_assets(): Promise<void> {
countAndStartDownload(asset);
}

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

// continue after we know if memory snapshot is available or not
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
await loaderHelpers.memorySnapshotSkippedOrDone.promise;

// start fetching assets in parallel, only if memory snapshot is not available.
for (const asset of containedInSnapshotAssets) {
if (!runtimeHelpers.loadedMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
countAndStartDownload(asset);
} else {
// Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download.
Expand All@@ -193,6 +188,8 @@ export async function mono_download_assets(): Promise<void> {
}

loaderHelpers.allDownloadsQueued.promise_control.resolve();

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All@@ -211,7 +208,6 @@ export async function mono_download_assets(): Promise<void> {
// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped

await runtimeHelpers.beforeOnRuntimeInitialized.promise;
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
runtimeHelpers.instantiate_asset(asset, url, data);
}
} else {
Expand DownExpand Up@@ -284,7 +280,7 @@ export function prepareAssets() {
mono_assert(resources.jsModuleNative, "resources.jsModuleNative must be defined");
mono_assert(resources.jsModuleRuntime, "resources.jsModuleRuntime must be defined");
mono_assert(!MonoWasmThreads || resources.jsModuleWorker, "resources.jsModuleWorker must be defined");
convert_single_asset(modulesAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(alwaysLoadedAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(modulesAssets, resources.jsModuleNative, "js-module-native");
convert_single_asset(modulesAssets, resources.jsModuleRuntime, "js-module-runtime");
if (MonoWasmThreads) {
Expand Down
19 changes: 11 additions & 8 deletions src/mono/wasm/runtime/loader/config.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { mono_assert } from "./globals";

export function deep_merge_config(target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand DownExpand Up@@ -220,15 +221,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi
await loaderHelpers.afterConfigLoaded.promise;
return;
}
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
return;
}
mono_log_debug("mono_wasm_load_config");
try {
await loadBootConfig(module);
configLoaded = true;
if (configFilePath) {
mono_log_debug("mono_wasm_load_config");
await loadBootConfig(module);
}

normalizeConfig();

Expand All@@ -249,7 +247,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi

normalizeConfig();

mono_assert(!loaderHelpers.config.startupMemoryCache || !module.instantiateWasm, "startupMemoryCache is not supported with Module.instantiateWasm");

loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
if (!loaderHelpers.config.startupMemoryCache) {
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err} ${(err as Error)?.stack}`;
loaderHelpers.config = module.config = Object.assign(loaderHelpers.config, { message: errMessage, error: err, isError: true });
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/loader/exit.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,9 +122,9 @@ function abort_promises(reason: any) {
loaderHelpers.afterConfigLoaded.promise_control.reject(reason);
loaderHelpers.wasmDownloadPromise.promise_control.reject(reason);
loaderHelpers.runtimeModuleLoaded.promise_control.reject(reason);
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
if (runtimeHelpers.dotnetReady) {
runtimeHelpers.dotnetReady.promise_control.reject(reason);
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
runtimeHelpers.afterInstantiateWasm.promise_control.reject(reason);
runtimeHelpers.beforePreInit.promise_control.reject(reason);
runtimeHelpers.afterPreInit.promise_control.reject(reason);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/loader/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,7 @@ export function setLoaderGlobals(
allDownloadsQueued: createPromiseController<void>(),
wasmDownloadPromise: createPromiseController<AssetEntryInternal>(),
runtimeModuleLoaded: createPromiseController<void>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),

is_exited,
is_runtime_running,
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -454,10 +454,11 @@ function importModules() {
}

async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) {
const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { default: emscriptenFactory } = es6Modules[1];
setRuntimeGlobals(globalObjectsRoot);
initializeExports(globalObjectsRoot);
await configureRuntimeStartup();
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
Expand DownExpand Up@@ -494,9 +495,8 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
mono_exit(1, err);
});

init_globalization();

setTimeout(() => {
init_globalization();
mono_download_assets(); // intentionally not awaited
}, 0);

Expand Down
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/snapshot.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,22 +44,35 @@ async function openCache(): Promise<Cache | null> {
}
}

export async function getMemorySnapshotSize(): Promise<number | undefined> {
export async function checkMemorySnapshotSize(): Promise<void> {
try {
if (!runtimeHelpers.config.startupMemoryCache) {
// we could start downloading DLLs because snapshot is disabled
return;
}

const cacheKey = await getCacheKey();
if (!cacheKey) {
return undefined;
return;
}
const cache = await openCache();
if (!cache) {
return undefined;
return;
}
const res = await cache.match(cacheKey);
const contentLength = res?.headers.get("content-length");
return contentLength ? parseInt(contentLength) : undefined;
const memorySize = contentLength ? parseInt(contentLength) : undefined;

runtimeHelpers.loadedMemorySnapshotSize = memorySize;
runtimeHelpers.storeMemorySnapshotPending = !memorySize;
} catch (ex) {
mono_log_warn("Failed find memory snapshot in the cache", ex);
return undefined;
}
finally {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
// we could start downloading DLLs because there is no snapshot yet
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
}
}

Expand Down
46 changes: 19 additions & 27 deletions src/mono/wasm/runtime/startup.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ import { instantiate_wasm_asset, wait_for_all_assets } from "./assets";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { replace_linker_placeholders } from "./exports-binding";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { getMemorySnapshot, storeMemorySnapshot, getMemorySnapshotSize } from "./snapshot";
import { checkMemorySnapshotSize, getMemorySnapshot, storeMemorySnapshot } from "./snapshot";
import { mono_log_debug, mono_log_error, mono_log_warn, mono_set_thread_id } from "./logging";

// threads
Expand All@@ -40,6 +40,19 @@ import { assertNoProxies } from "./gc-handles";
// default size if MonoConfig.pthreadPoolSize is undefined
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

// we are making emscripten startup async friendly
// emscripten is executing the events without awaiting it and so we need to block progress via PromiseControllers above
export function configureEmscriptenStartup(module: DotnetModuleInternal): void {
Expand DownExpand Up@@ -118,8 +131,6 @@ function instantiateWasm(

const mark = startMeasure();
if (userInstantiateWasm) {
// user wasm instantiation doesn't support memory snapshots
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
const exports = userInstantiateWasm(imports, (instance: WebAssembly.Instance, module: WebAssembly.Module | undefined) => {
endMeasure(mark, MeasuredBlock.instantiateWasm);
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
Expand DownExpand Up@@ -377,14 +388,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
Expand DownExpand Up@@ -459,20 +462,9 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
let memorySize: number | undefined = undefined;
await loaderHelpers.afterConfigLoaded;
mono_log_debug("instantiate_wasm_module");

if (runtimeHelpers.config.startupMemoryCache) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}

await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

Expand All@@ -486,19 +478,19 @@ async function instantiate_wasm_module(

mono_log_debug("instantiate_wasm_module done");

if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
try {
const wasmMemory = Module.getMemory();

// .grow() takes a delta compared to the previous size
wasmMemory.grow((memorySize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
wasmMemory.grow((runtimeHelpers.loadedMemorySnapshotSize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
Comment thread
kg marked this conversation as resolved.
runtimeHelpers.updateMemoryViews();
} catch (err) {
mono_log_warn("failed to resize memory for the snapshot", err);
runtimeHelpers.loadedMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshotSize = undefined;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
} catch (err) {
Expand All@@ -511,7 +503,7 @@ async function instantiate_wasm_module(

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
// get the bytes after we re-sized the memory, so that we don't have too much memory in use at the same time
const memoryBytes = await getMemorySnapshot();
const heapU8 = localHeapViewU8();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,7 @@
<link rel="preload" href="./blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./System.Private.CoreLib.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./advanced-sample.lib.module.js" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withConfig({
startupMemoryCache: true,
resources: {
modulesAfterConfigLoaded: {
"advanced-sample.lib.module.js": ""
Expand Down
3 changes: 0 additions & 3 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,9 +12,6 @@
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./_framework/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
5 changes: 4 additions & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,14 +31,17 @@ try {
}

const runtime = await dotnet
.withConfig({
maxParallelDownloads: 10000,
Comment thread
kg marked this conversation as resolved.
// diagnosticTracing:true,
})
.withModuleConfig({
printErr: () => undefined,
print: () => undefined,
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
})
.create();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/features.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -193,12 +193,13 @@ See also [fetch integrity on MDN](https://developer.mozilla.org/en-US/docs/Web/A

### Pre-fetching
In order to start downloading application resources as soon as possible you can add HTML elements to `<head>` of your page similar to:
Adding too many files into prefetch could be counterproductive.
Please benchmark your startup performance on real target devices and with realistic network conditions.

```html
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
```

See also [link rel prefetch on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import type { RuntimeAPI } from "./types";

import { Module, linkerDisableLegacyJsInterop, exportedRuntimeAPI, passEmscriptenInternals, runtimeHelpers, setRuntimeGlobals, } from "./globals";
import { GlobalObjects, is_nullish } from "./types/internal";
import { configureEmscriptenStartup, configureWorkerStartup } from "./startup";
import { configureEmscriptenStartup, configureRuntimeStartup, configureWorkerStartup } from "./startup";

import { create_weak_ref } from "./weak-ref";
import { export_internal } from "./exports-internal";
Expand DownExpand Up@@ -143,5 +143,5 @@ class RuntimeList {

// export external API
export {
passEmscriptenInternals, initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
passEmscriptenInternals, initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
};
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,6 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
gitHash,
allAssetsInMemory: createPromiseController<void>(),
dotnetReady: createPromiseController<any>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),
afterInstantiateWasm: createPromiseController<void>(),
beforePreInit: createPromiseController<void>(),
afterPreInit: createPromiseController<void>(),
Expand Down
18 changes: 7 additions & 11 deletions src/mono/wasm/runtime/loader/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,8 +78,6 @@ const containedInSnapshotByAssetTypes: {
"pdb": true,
"heap": true,
"icu": true,
...jsModulesAssetTypes,
"dotnetwasm": true,
};

// these assets are instantiated differently than the main flow
Expand All@@ -95,7 +93,7 @@ export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != loaderHelpers.preferredIcuAsset);
}

function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
function convert_single_asset(assetsCollection: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
const keys = Object.keys(resource || {});
mono_assert(keys.length == 1, `Expect to have one ${behavior} asset in resources`);

Expand All@@ -110,7 +108,7 @@ function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: Res
set_single_asset(asset);

// so that we can use it on the worker too
modulesAssets.push(asset);
assetsCollection.push(asset);
return asset;
}

Expand DownExpand Up@@ -168,15 +166,12 @@ export async function mono_download_assets(): Promise<void> {
countAndStartDownload(asset);
}

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

// continue after we know if memory snapshot is available or not
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
await loaderHelpers.memorySnapshotSkippedOrDone.promise;

// start fetching assets in parallel, only if memory snapshot is not available.
for (const asset of containedInSnapshotAssets) {
if (!runtimeHelpers.loadedMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
countAndStartDownload(asset);
} else {
// Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download.
Expand All@@ -193,6 +188,8 @@ export async function mono_download_assets(): Promise<void> {
}

loaderHelpers.allDownloadsQueued.promise_control.resolve();

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All@@ -211,7 +208,6 @@ export async function mono_download_assets(): Promise<void> {
// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped

await runtimeHelpers.beforeOnRuntimeInitialized.promise;
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
runtimeHelpers.instantiate_asset(asset, url, data);
}
} else {
Expand DownExpand Up@@ -284,7 +280,7 @@ export function prepareAssets() {
mono_assert(resources.jsModuleNative, "resources.jsModuleNative must be defined");
mono_assert(resources.jsModuleRuntime, "resources.jsModuleRuntime must be defined");
mono_assert(!MonoWasmThreads || resources.jsModuleWorker, "resources.jsModuleWorker must be defined");
convert_single_asset(modulesAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(alwaysLoadedAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(modulesAssets, resources.jsModuleNative, "js-module-native");
convert_single_asset(modulesAssets, resources.jsModuleRuntime, "js-module-runtime");
if (MonoWasmThreads) {
Expand Down
19 changes: 11 additions & 8 deletions src/mono/wasm/runtime/loader/config.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { mono_assert } from "./globals";

export function deep_merge_config(target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand DownExpand Up@@ -220,15 +221,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi
await loaderHelpers.afterConfigLoaded.promise;
return;
}
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
return;
}
mono_log_debug("mono_wasm_load_config");
try {
await loadBootConfig(module);
configLoaded = true;
if (configFilePath) {
mono_log_debug("mono_wasm_load_config");
await loadBootConfig(module);
}

normalizeConfig();

Expand All@@ -249,7 +247,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi

normalizeConfig();

mono_assert(!loaderHelpers.config.startupMemoryCache || !module.instantiateWasm, "startupMemoryCache is not supported with Module.instantiateWasm");

loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
if (!loaderHelpers.config.startupMemoryCache) {
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err} ${(err as Error)?.stack}`;
loaderHelpers.config = module.config = Object.assign(loaderHelpers.config, { message: errMessage, error: err, isError: true });
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/loader/exit.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,9 +122,9 @@ function abort_promises(reason: any) {
loaderHelpers.afterConfigLoaded.promise_control.reject(reason);
loaderHelpers.wasmDownloadPromise.promise_control.reject(reason);
loaderHelpers.runtimeModuleLoaded.promise_control.reject(reason);
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
if (runtimeHelpers.dotnetReady) {
runtimeHelpers.dotnetReady.promise_control.reject(reason);
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
runtimeHelpers.afterInstantiateWasm.promise_control.reject(reason);
runtimeHelpers.beforePreInit.promise_control.reject(reason);
runtimeHelpers.afterPreInit.promise_control.reject(reason);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/loader/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,7 @@ export function setLoaderGlobals(
allDownloadsQueued: createPromiseController<void>(),
wasmDownloadPromise: createPromiseController<AssetEntryInternal>(),
runtimeModuleLoaded: createPromiseController<void>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),

is_exited,
is_runtime_running,
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -454,10 +454,11 @@ function importModules() {
}

async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) {
const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { default: emscriptenFactory } = es6Modules[1];
setRuntimeGlobals(globalObjectsRoot);
initializeExports(globalObjectsRoot);
await configureRuntimeStartup();
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
Expand DownExpand Up@@ -494,9 +495,8 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
mono_exit(1, err);
});

init_globalization();

setTimeout(() => {
init_globalization();
mono_download_assets(); // intentionally not awaited
}, 0);

Expand Down
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/snapshot.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,22 +44,35 @@ async function openCache(): Promise<Cache | null> {
}
}

export async function getMemorySnapshotSize(): Promise<number | undefined> {
export async function checkMemorySnapshotSize(): Promise<void> {
try {
if (!runtimeHelpers.config.startupMemoryCache) {
// we could start downloading DLLs because snapshot is disabled
return;
}

const cacheKey = await getCacheKey();
if (!cacheKey) {
return undefined;
return;
}
const cache = await openCache();
if (!cache) {
return undefined;
return;
}
const res = await cache.match(cacheKey);
const contentLength = res?.headers.get("content-length");
return contentLength ? parseInt(contentLength) : undefined;
const memorySize = contentLength ? parseInt(contentLength) : undefined;

runtimeHelpers.loadedMemorySnapshotSize = memorySize;
runtimeHelpers.storeMemorySnapshotPending = !memorySize;
} catch (ex) {
mono_log_warn("Failed find memory snapshot in the cache", ex);
return undefined;
}
finally {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
// we could start downloading DLLs because there is no snapshot yet
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
}
}

Expand Down
46 changes: 19 additions & 27 deletions src/mono/wasm/runtime/startup.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ import { instantiate_wasm_asset, wait_for_all_assets } from "./assets";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { replace_linker_placeholders } from "./exports-binding";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { getMemorySnapshot, storeMemorySnapshot, getMemorySnapshotSize } from "./snapshot";
import { checkMemorySnapshotSize, getMemorySnapshot, storeMemorySnapshot } from "./snapshot";
import { mono_log_debug, mono_log_error, mono_log_warn, mono_set_thread_id } from "./logging";

// threads
Expand All@@ -40,6 +40,19 @@ import { assertNoProxies } from "./gc-handles";
// default size if MonoConfig.pthreadPoolSize is undefined
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

// we are making emscripten startup async friendly
// emscripten is executing the events without awaiting it and so we need to block progress via PromiseControllers above
export function configureEmscriptenStartup(module: DotnetModuleInternal): void {
Expand DownExpand Up@@ -118,8 +131,6 @@ function instantiateWasm(

const mark = startMeasure();
if (userInstantiateWasm) {
// user wasm instantiation doesn't support memory snapshots
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
const exports = userInstantiateWasm(imports, (instance: WebAssembly.Instance, module: WebAssembly.Module | undefined) => {
endMeasure(mark, MeasuredBlock.instantiateWasm);
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
Expand DownExpand Up@@ -377,14 +388,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
Expand DownExpand Up@@ -459,20 +462,9 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
let memorySize: number | undefined = undefined;
await loaderHelpers.afterConfigLoaded;
mono_log_debug("instantiate_wasm_module");

if (runtimeHelpers.config.startupMemoryCache) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}

await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

Expand All@@ -486,19 +478,19 @@ async function instantiate_wasm_module(

mono_log_debug("instantiate_wasm_module done");

if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
try {
const wasmMemory = Module.getMemory();

// .grow() takes a delta compared to the previous size
wasmMemory.grow((memorySize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
wasmMemory.grow((runtimeHelpers.loadedMemorySnapshotSize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
Comment thread
kg marked this conversation as resolved.
runtimeHelpers.updateMemoryViews();
} catch (err) {
mono_log_warn("failed to resize memory for the snapshot", err);
runtimeHelpers.loadedMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshotSize = undefined;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
} catch (err) {
Expand All@@ -511,7 +503,7 @@ async function instantiate_wasm_module(

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
// get the bytes after we re-sized the memory, so that we don't have too much memory in use at the same time
const memoryBytes = await getMemorySnapshot();
const heapU8 = localHeapViewU8();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,7 @@
<link rel="preload" href="./blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./System.Private.CoreLib.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./advanced-sample.lib.module.js" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withConfig({
startupMemoryCache: true,
resources: {
modulesAfterConfigLoaded: {
"advanced-sample.lib.module.js": ""
Expand Down
3 changes: 0 additions & 3 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,9 +12,6 @@
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./_framework/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
5 changes: 4 additions & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,14 +31,17 @@ try {
}

const runtime = await dotnet
.withConfig({
maxParallelDownloads: 10000,
Comment thread
kg marked this conversation as resolved.
// diagnosticTracing:true,
})
.withModuleConfig({
printErr: () => undefined,
print: () => undefined,
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
})
.create();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/features.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -193,12 +193,13 @@ See also [fetch integrity on MDN](https://developer.mozilla.org/en-US/docs/Web/A

### Pre-fetching
In order to start downloading application resources as soon as possible you can add HTML elements to `<head>` of your page similar to:
Adding too many files into prefetch could be counterproductive.
Please benchmark your startup performance on real target devices and with realistic network conditions.

```html
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
```

See also [link rel prefetch on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import type { RuntimeAPI } from "./types";

import { Module, linkerDisableLegacyJsInterop, exportedRuntimeAPI, passEmscriptenInternals, runtimeHelpers, setRuntimeGlobals, } from "./globals";
import { GlobalObjects, is_nullish } from "./types/internal";
import { configureEmscriptenStartup, configureWorkerStartup } from "./startup";
import { configureEmscriptenStartup, configureRuntimeStartup, configureWorkerStartup } from "./startup";

import { create_weak_ref } from "./weak-ref";
import { export_internal } from "./exports-internal";
Expand DownExpand Up@@ -143,5 +143,5 @@ class RuntimeList {

// export external API
export {
passEmscriptenInternals, initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
passEmscriptenInternals, initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
};
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,6 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
gitHash,
allAssetsInMemory: createPromiseController<void>(),
dotnetReady: createPromiseController<any>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),
afterInstantiateWasm: createPromiseController<void>(),
beforePreInit: createPromiseController<void>(),
afterPreInit: createPromiseController<void>(),
Expand Down
18 changes: 7 additions & 11 deletions src/mono/wasm/runtime/loader/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,8 +78,6 @@ const containedInSnapshotByAssetTypes: {
"pdb": true,
"heap": true,
"icu": true,
...jsModulesAssetTypes,
"dotnetwasm": true,
};

// these assets are instantiated differently than the main flow
Expand All@@ -95,7 +93,7 @@ export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != loaderHelpers.preferredIcuAsset);
}

function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
function convert_single_asset(assetsCollection: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
const keys = Object.keys(resource || {});
mono_assert(keys.length == 1, `Expect to have one ${behavior} asset in resources`);

Expand All@@ -110,7 +108,7 @@ function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: Res
set_single_asset(asset);

// so that we can use it on the worker too
modulesAssets.push(asset);
assetsCollection.push(asset);
return asset;
}

Expand DownExpand Up@@ -168,15 +166,12 @@ export async function mono_download_assets(): Promise<void> {
countAndStartDownload(asset);
}

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

// continue after we know if memory snapshot is available or not
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
await loaderHelpers.memorySnapshotSkippedOrDone.promise;

// start fetching assets in parallel, only if memory snapshot is not available.
for (const asset of containedInSnapshotAssets) {
if (!runtimeHelpers.loadedMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
countAndStartDownload(asset);
} else {
// Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download.
Expand All@@ -193,6 +188,8 @@ export async function mono_download_assets(): Promise<void> {
}

loaderHelpers.allDownloadsQueued.promise_control.resolve();

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All@@ -211,7 +208,6 @@ export async function mono_download_assets(): Promise<void> {
// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped

await runtimeHelpers.beforeOnRuntimeInitialized.promise;
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
runtimeHelpers.instantiate_asset(asset, url, data);
}
} else {
Expand DownExpand Up@@ -284,7 +280,7 @@ export function prepareAssets() {
mono_assert(resources.jsModuleNative, "resources.jsModuleNative must be defined");
mono_assert(resources.jsModuleRuntime, "resources.jsModuleRuntime must be defined");
mono_assert(!MonoWasmThreads || resources.jsModuleWorker, "resources.jsModuleWorker must be defined");
convert_single_asset(modulesAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(alwaysLoadedAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(modulesAssets, resources.jsModuleNative, "js-module-native");
convert_single_asset(modulesAssets, resources.jsModuleRuntime, "js-module-runtime");
if (MonoWasmThreads) {
Expand Down
19 changes: 11 additions & 8 deletions src/mono/wasm/runtime/loader/config.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { mono_assert } from "./globals";

export function deep_merge_config(target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand DownExpand Up@@ -220,15 +221,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi
await loaderHelpers.afterConfigLoaded.promise;
return;
}
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
return;
}
mono_log_debug("mono_wasm_load_config");
try {
await loadBootConfig(module);
configLoaded = true;
if (configFilePath) {
mono_log_debug("mono_wasm_load_config");
await loadBootConfig(module);
}

normalizeConfig();

Expand All@@ -249,7 +247,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi

normalizeConfig();

mono_assert(!loaderHelpers.config.startupMemoryCache || !module.instantiateWasm, "startupMemoryCache is not supported with Module.instantiateWasm");

loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
if (!loaderHelpers.config.startupMemoryCache) {
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err} ${(err as Error)?.stack}`;
loaderHelpers.config = module.config = Object.assign(loaderHelpers.config, { message: errMessage, error: err, isError: true });
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/loader/exit.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,9 +122,9 @@ function abort_promises(reason: any) {
loaderHelpers.afterConfigLoaded.promise_control.reject(reason);
loaderHelpers.wasmDownloadPromise.promise_control.reject(reason);
loaderHelpers.runtimeModuleLoaded.promise_control.reject(reason);
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
if (runtimeHelpers.dotnetReady) {
runtimeHelpers.dotnetReady.promise_control.reject(reason);
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
runtimeHelpers.afterInstantiateWasm.promise_control.reject(reason);
runtimeHelpers.beforePreInit.promise_control.reject(reason);
runtimeHelpers.afterPreInit.promise_control.reject(reason);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/loader/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,7 @@ export function setLoaderGlobals(
allDownloadsQueued: createPromiseController<void>(),
wasmDownloadPromise: createPromiseController<AssetEntryInternal>(),
runtimeModuleLoaded: createPromiseController<void>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),

is_exited,
is_runtime_running,
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -454,10 +454,11 @@ function importModules() {
}

async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) {
const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { default: emscriptenFactory } = es6Modules[1];
setRuntimeGlobals(globalObjectsRoot);
initializeExports(globalObjectsRoot);
await configureRuntimeStartup();
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
Expand DownExpand Up@@ -494,9 +495,8 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
mono_exit(1, err);
});

init_globalization();

setTimeout(() => {
init_globalization();
mono_download_assets(); // intentionally not awaited
}, 0);

Expand Down
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/snapshot.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,22 +44,35 @@ async function openCache(): Promise<Cache | null> {
}
}

export async function getMemorySnapshotSize(): Promise<number | undefined> {
export async function checkMemorySnapshotSize(): Promise<void> {
try {
if (!runtimeHelpers.config.startupMemoryCache) {
// we could start downloading DLLs because snapshot is disabled
return;
}

const cacheKey = await getCacheKey();
if (!cacheKey) {
return undefined;
return;
}
const cache = await openCache();
if (!cache) {
return undefined;
return;
}
const res = await cache.match(cacheKey);
const contentLength = res?.headers.get("content-length");
return contentLength ? parseInt(contentLength) : undefined;
const memorySize = contentLength ? parseInt(contentLength) : undefined;

runtimeHelpers.loadedMemorySnapshotSize = memorySize;
runtimeHelpers.storeMemorySnapshotPending = !memorySize;
} catch (ex) {
mono_log_warn("Failed find memory snapshot in the cache", ex);
return undefined;
}
finally {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
// we could start downloading DLLs because there is no snapshot yet
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
}
}

Expand Down
46 changes: 19 additions & 27 deletions src/mono/wasm/runtime/startup.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ import { instantiate_wasm_asset, wait_for_all_assets } from "./assets";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { replace_linker_placeholders } from "./exports-binding";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { getMemorySnapshot, storeMemorySnapshot, getMemorySnapshotSize } from "./snapshot";
import { checkMemorySnapshotSize, getMemorySnapshot, storeMemorySnapshot } from "./snapshot";
import { mono_log_debug, mono_log_error, mono_log_warn, mono_set_thread_id } from "./logging";

// threads
Expand All@@ -40,6 +40,19 @@ import { assertNoProxies } from "./gc-handles";
// default size if MonoConfig.pthreadPoolSize is undefined
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

// we are making emscripten startup async friendly
// emscripten is executing the events without awaiting it and so we need to block progress via PromiseControllers above
export function configureEmscriptenStartup(module: DotnetModuleInternal): void {
Expand DownExpand Up@@ -118,8 +131,6 @@ function instantiateWasm(

const mark = startMeasure();
if (userInstantiateWasm) {
// user wasm instantiation doesn't support memory snapshots
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
const exports = userInstantiateWasm(imports, (instance: WebAssembly.Instance, module: WebAssembly.Module | undefined) => {
endMeasure(mark, MeasuredBlock.instantiateWasm);
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
Expand DownExpand Up@@ -377,14 +388,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
Expand DownExpand Up@@ -459,20 +462,9 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
let memorySize: number | undefined = undefined;
await loaderHelpers.afterConfigLoaded;
mono_log_debug("instantiate_wasm_module");

if (runtimeHelpers.config.startupMemoryCache) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}

await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

Expand All@@ -486,19 +478,19 @@ async function instantiate_wasm_module(

mono_log_debug("instantiate_wasm_module done");

if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
try {
const wasmMemory = Module.getMemory();

// .grow() takes a delta compared to the previous size
wasmMemory.grow((memorySize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
wasmMemory.grow((runtimeHelpers.loadedMemorySnapshotSize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
Comment thread
kg marked this conversation as resolved.
runtimeHelpers.updateMemoryViews();
} catch (err) {
mono_log_warn("failed to resize memory for the snapshot", err);
runtimeHelpers.loadedMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshotSize = undefined;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
} catch (err) {
Expand All@@ -511,7 +503,7 @@ async function instantiate_wasm_module(

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
// get the bytes after we re-sized the memory, so that we don't have too much memory in use at the same time
const memoryBytes = await getMemorySnapshot();
const heapU8 = localHeapViewU8();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,7 @@
<link rel="preload" href="./blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./System.Private.CoreLib.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./advanced-sample.lib.module.js" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withConfig({
startupMemoryCache: true,
resources: {
modulesAfterConfigLoaded: {
"advanced-sample.lib.module.js": ""
Expand Down
3 changes: 0 additions & 3 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,9 +12,6 @@
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./_framework/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
5 changes: 4 additions & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,14 +31,17 @@ try {
}

const runtime = await dotnet
.withConfig({
maxParallelDownloads: 10000,
Comment thread
kg marked this conversation as resolved.
// diagnosticTracing:true,
})
.withModuleConfig({
printErr: () => undefined,
print: () => undefined,
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
})
.create();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/features.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -193,12 +193,13 @@ See also [fetch integrity on MDN](https://developer.mozilla.org/en-US/docs/Web/A

### Pre-fetching
In order to start downloading application resources as soon as possible you can add HTML elements to `<head>` of your page similar to:
Adding too many files into prefetch could be counterproductive.
Please benchmark your startup performance on real target devices and with realistic network conditions.

```html
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
```

See also [link rel prefetch on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import type { RuntimeAPI } from "./types";

import { Module, linkerDisableLegacyJsInterop, exportedRuntimeAPI, passEmscriptenInternals, runtimeHelpers, setRuntimeGlobals, } from "./globals";
import { GlobalObjects, is_nullish } from "./types/internal";
import { configureEmscriptenStartup, configureWorkerStartup } from "./startup";
import { configureEmscriptenStartup, configureRuntimeStartup, configureWorkerStartup } from "./startup";

import { create_weak_ref } from "./weak-ref";
import { export_internal } from "./exports-internal";
Expand DownExpand Up@@ -143,5 +143,5 @@ class RuntimeList {

// export external API
export {
passEmscriptenInternals, initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
passEmscriptenInternals, initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
};
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,6 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
gitHash,
allAssetsInMemory: createPromiseController<void>(),
dotnetReady: createPromiseController<any>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),
afterInstantiateWasm: createPromiseController<void>(),
beforePreInit: createPromiseController<void>(),
afterPreInit: createPromiseController<void>(),
Expand Down
18 changes: 7 additions & 11 deletions src/mono/wasm/runtime/loader/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,8 +78,6 @@ const containedInSnapshotByAssetTypes: {
"pdb": true,
"heap": true,
"icu": true,
...jsModulesAssetTypes,
"dotnetwasm": true,
};

// these assets are instantiated differently than the main flow
Expand All@@ -95,7 +93,7 @@ export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != loaderHelpers.preferredIcuAsset);
}

function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
function convert_single_asset(assetsCollection: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
const keys = Object.keys(resource || {});
mono_assert(keys.length == 1, `Expect to have one ${behavior} asset in resources`);

Expand All@@ -110,7 +108,7 @@ function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: Res
set_single_asset(asset);

// so that we can use it on the worker too
modulesAssets.push(asset);
assetsCollection.push(asset);
return asset;
}

Expand DownExpand Up@@ -168,15 +166,12 @@ export async function mono_download_assets(): Promise<void> {
countAndStartDownload(asset);
}

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

// continue after we know if memory snapshot is available or not
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
await loaderHelpers.memorySnapshotSkippedOrDone.promise;

// start fetching assets in parallel, only if memory snapshot is not available.
for (const asset of containedInSnapshotAssets) {
if (!runtimeHelpers.loadedMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
countAndStartDownload(asset);
} else {
// Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download.
Expand All@@ -193,6 +188,8 @@ export async function mono_download_assets(): Promise<void> {
}

loaderHelpers.allDownloadsQueued.promise_control.resolve();

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All@@ -211,7 +208,6 @@ export async function mono_download_assets(): Promise<void> {
// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped

await runtimeHelpers.beforeOnRuntimeInitialized.promise;
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
runtimeHelpers.instantiate_asset(asset, url, data);
}
} else {
Expand DownExpand Up@@ -284,7 +280,7 @@ export function prepareAssets() {
mono_assert(resources.jsModuleNative, "resources.jsModuleNative must be defined");
mono_assert(resources.jsModuleRuntime, "resources.jsModuleRuntime must be defined");
mono_assert(!MonoWasmThreads || resources.jsModuleWorker, "resources.jsModuleWorker must be defined");
convert_single_asset(modulesAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(alwaysLoadedAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(modulesAssets, resources.jsModuleNative, "js-module-native");
convert_single_asset(modulesAssets, resources.jsModuleRuntime, "js-module-runtime");
if (MonoWasmThreads) {
Expand Down
19 changes: 11 additions & 8 deletions src/mono/wasm/runtime/loader/config.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { mono_assert } from "./globals";

export function deep_merge_config(target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand DownExpand Up@@ -220,15 +221,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi
await loaderHelpers.afterConfigLoaded.promise;
return;
}
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
return;
}
mono_log_debug("mono_wasm_load_config");
try {
await loadBootConfig(module);
configLoaded = true;
if (configFilePath) {
mono_log_debug("mono_wasm_load_config");
await loadBootConfig(module);
}

normalizeConfig();

Expand All@@ -249,7 +247,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi

normalizeConfig();

mono_assert(!loaderHelpers.config.startupMemoryCache || !module.instantiateWasm, "startupMemoryCache is not supported with Module.instantiateWasm");

loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
if (!loaderHelpers.config.startupMemoryCache) {
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err} ${(err as Error)?.stack}`;
loaderHelpers.config = module.config = Object.assign(loaderHelpers.config, { message: errMessage, error: err, isError: true });
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/loader/exit.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,9 +122,9 @@ function abort_promises(reason: any) {
loaderHelpers.afterConfigLoaded.promise_control.reject(reason);
loaderHelpers.wasmDownloadPromise.promise_control.reject(reason);
loaderHelpers.runtimeModuleLoaded.promise_control.reject(reason);
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
if (runtimeHelpers.dotnetReady) {
runtimeHelpers.dotnetReady.promise_control.reject(reason);
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
runtimeHelpers.afterInstantiateWasm.promise_control.reject(reason);
runtimeHelpers.beforePreInit.promise_control.reject(reason);
runtimeHelpers.afterPreInit.promise_control.reject(reason);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/loader/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,7 @@ export function setLoaderGlobals(
allDownloadsQueued: createPromiseController<void>(),
wasmDownloadPromise: createPromiseController<AssetEntryInternal>(),
runtimeModuleLoaded: createPromiseController<void>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),

is_exited,
is_runtime_running,
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -454,10 +454,11 @@ function importModules() {
}

async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) {
const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { default: emscriptenFactory } = es6Modules[1];
setRuntimeGlobals(globalObjectsRoot);
initializeExports(globalObjectsRoot);
await configureRuntimeStartup();
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
Expand DownExpand Up@@ -494,9 +495,8 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
mono_exit(1, err);
});

init_globalization();

setTimeout(() => {
init_globalization();
mono_download_assets(); // intentionally not awaited
}, 0);

Expand Down
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/snapshot.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,22 +44,35 @@ async function openCache(): Promise<Cache | null> {
}
}

export async function getMemorySnapshotSize(): Promise<number | undefined> {
export async function checkMemorySnapshotSize(): Promise<void> {
try {
if (!runtimeHelpers.config.startupMemoryCache) {
// we could start downloading DLLs because snapshot is disabled
return;
}

const cacheKey = await getCacheKey();
if (!cacheKey) {
return undefined;
return;
}
const cache = await openCache();
if (!cache) {
return undefined;
return;
}
const res = await cache.match(cacheKey);
const contentLength = res?.headers.get("content-length");
return contentLength ? parseInt(contentLength) : undefined;
const memorySize = contentLength ? parseInt(contentLength) : undefined;

runtimeHelpers.loadedMemorySnapshotSize = memorySize;
runtimeHelpers.storeMemorySnapshotPending = !memorySize;
} catch (ex) {
mono_log_warn("Failed find memory snapshot in the cache", ex);
return undefined;
}
finally {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
// we could start downloading DLLs because there is no snapshot yet
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
}
}

Expand Down
46 changes: 19 additions & 27 deletions src/mono/wasm/runtime/startup.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ import { instantiate_wasm_asset, wait_for_all_assets } from "./assets";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { replace_linker_placeholders } from "./exports-binding";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { getMemorySnapshot, storeMemorySnapshot, getMemorySnapshotSize } from "./snapshot";
import { checkMemorySnapshotSize, getMemorySnapshot, storeMemorySnapshot } from "./snapshot";
import { mono_log_debug, mono_log_error, mono_log_warn, mono_set_thread_id } from "./logging";

// threads
Expand All@@ -40,6 +40,19 @@ import { assertNoProxies } from "./gc-handles";
// default size if MonoConfig.pthreadPoolSize is undefined
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

// we are making emscripten startup async friendly
// emscripten is executing the events without awaiting it and so we need to block progress via PromiseControllers above
export function configureEmscriptenStartup(module: DotnetModuleInternal): void {
Expand DownExpand Up@@ -118,8 +131,6 @@ function instantiateWasm(

const mark = startMeasure();
if (userInstantiateWasm) {
// user wasm instantiation doesn't support memory snapshots
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
const exports = userInstantiateWasm(imports, (instance: WebAssembly.Instance, module: WebAssembly.Module | undefined) => {
endMeasure(mark, MeasuredBlock.instantiateWasm);
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
Expand DownExpand Up@@ -377,14 +388,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
Expand DownExpand Up@@ -459,20 +462,9 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
let memorySize: number | undefined = undefined;
await loaderHelpers.afterConfigLoaded;
mono_log_debug("instantiate_wasm_module");

if (runtimeHelpers.config.startupMemoryCache) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}

await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

Expand All@@ -486,19 +478,19 @@ async function instantiate_wasm_module(

mono_log_debug("instantiate_wasm_module done");

if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
try {
const wasmMemory = Module.getMemory();

// .grow() takes a delta compared to the previous size
wasmMemory.grow((memorySize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
wasmMemory.grow((runtimeHelpers.loadedMemorySnapshotSize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
Comment thread
kg marked this conversation as resolved.
runtimeHelpers.updateMemoryViews();
} catch (err) {
mono_log_warn("failed to resize memory for the snapshot", err);
runtimeHelpers.loadedMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshotSize = undefined;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
} catch (err) {
Expand All@@ -511,7 +503,7 @@ async function instantiate_wasm_module(

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
// get the bytes after we re-sized the memory, so that we don't have too much memory in use at the same time
const memoryBytes = await getMemorySnapshot();
const heapU8 = localHeapViewU8();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,7 @@
<link rel="preload" href="./blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./System.Private.CoreLib.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./advanced-sample.lib.module.js" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withConfig({
startupMemoryCache: true,
resources: {
modulesAfterConfigLoaded: {
"advanced-sample.lib.module.js": ""
Expand Down
3 changes: 0 additions & 3 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,9 +12,6 @@
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./_framework/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
5 changes: 4 additions & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,14 +31,17 @@ try {
}

const runtime = await dotnet
.withConfig({
maxParallelDownloads: 10000,
Comment thread
kg marked this conversation as resolved.
// diagnosticTracing:true,
})
.withModuleConfig({
printErr: () => undefined,
print: () => undefined,
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
})
.create();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/features.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -193,12 +193,13 @@ See also [fetch integrity on MDN](https://developer.mozilla.org/en-US/docs/Web/A

### Pre-fetching
In order to start downloading application resources as soon as possible you can add HTML elements to `<head>` of your page similar to:
Adding too many files into prefetch could be counterproductive.
Please benchmark your startup performance on real target devices and with realistic network conditions.

```html
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
```

See also [link rel prefetch on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import type { RuntimeAPI } from "./types";

import { Module, linkerDisableLegacyJsInterop, exportedRuntimeAPI, passEmscriptenInternals, runtimeHelpers, setRuntimeGlobals, } from "./globals";
import { GlobalObjects, is_nullish } from "./types/internal";
import { configureEmscriptenStartup, configureWorkerStartup } from "./startup";
import { configureEmscriptenStartup, configureRuntimeStartup, configureWorkerStartup } from "./startup";

import { create_weak_ref } from "./weak-ref";
import { export_internal } from "./exports-internal";
Expand DownExpand Up@@ -143,5 +143,5 @@ class RuntimeList {

// export external API
export {
passEmscriptenInternals, initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
passEmscriptenInternals, initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
};
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,6 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
gitHash,
allAssetsInMemory: createPromiseController<void>(),
dotnetReady: createPromiseController<any>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),
afterInstantiateWasm: createPromiseController<void>(),
beforePreInit: createPromiseController<void>(),
afterPreInit: createPromiseController<void>(),
Expand Down
18 changes: 7 additions & 11 deletions src/mono/wasm/runtime/loader/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,8 +78,6 @@ const containedInSnapshotByAssetTypes: {
"pdb": true,
"heap": true,
"icu": true,
...jsModulesAssetTypes,
"dotnetwasm": true,
};

// these assets are instantiated differently than the main flow
Expand All@@ -95,7 +93,7 @@ export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != loaderHelpers.preferredIcuAsset);
}

function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
function convert_single_asset(assetsCollection: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
const keys = Object.keys(resource || {});
mono_assert(keys.length == 1, `Expect to have one ${behavior} asset in resources`);

Expand All@@ -110,7 +108,7 @@ function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: Res
set_single_asset(asset);

// so that we can use it on the worker too
modulesAssets.push(asset);
assetsCollection.push(asset);
return asset;
}

Expand DownExpand Up@@ -168,15 +166,12 @@ export async function mono_download_assets(): Promise<void> {
countAndStartDownload(asset);
}

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

// continue after we know if memory snapshot is available or not
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
await loaderHelpers.memorySnapshotSkippedOrDone.promise;

// start fetching assets in parallel, only if memory snapshot is not available.
for (const asset of containedInSnapshotAssets) {
if (!runtimeHelpers.loadedMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
countAndStartDownload(asset);
} else {
// Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download.
Expand All@@ -193,6 +188,8 @@ export async function mono_download_assets(): Promise<void> {
}

loaderHelpers.allDownloadsQueued.promise_control.resolve();

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All@@ -211,7 +208,6 @@ export async function mono_download_assets(): Promise<void> {
// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped

await runtimeHelpers.beforeOnRuntimeInitialized.promise;
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
runtimeHelpers.instantiate_asset(asset, url, data);
}
} else {
Expand DownExpand Up@@ -284,7 +280,7 @@ export function prepareAssets() {
mono_assert(resources.jsModuleNative, "resources.jsModuleNative must be defined");
mono_assert(resources.jsModuleRuntime, "resources.jsModuleRuntime must be defined");
mono_assert(!MonoWasmThreads || resources.jsModuleWorker, "resources.jsModuleWorker must be defined");
convert_single_asset(modulesAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(alwaysLoadedAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(modulesAssets, resources.jsModuleNative, "js-module-native");
convert_single_asset(modulesAssets, resources.jsModuleRuntime, "js-module-runtime");
if (MonoWasmThreads) {
Expand Down
19 changes: 11 additions & 8 deletions src/mono/wasm/runtime/loader/config.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { mono_assert } from "./globals";

export function deep_merge_config(target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand DownExpand Up@@ -220,15 +221,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi
await loaderHelpers.afterConfigLoaded.promise;
return;
}
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
return;
}
mono_log_debug("mono_wasm_load_config");
try {
await loadBootConfig(module);
configLoaded = true;
if (configFilePath) {
mono_log_debug("mono_wasm_load_config");
await loadBootConfig(module);
}

normalizeConfig();

Expand All@@ -249,7 +247,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi

normalizeConfig();

mono_assert(!loaderHelpers.config.startupMemoryCache || !module.instantiateWasm, "startupMemoryCache is not supported with Module.instantiateWasm");

loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
if (!loaderHelpers.config.startupMemoryCache) {
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err} ${(err as Error)?.stack}`;
loaderHelpers.config = module.config = Object.assign(loaderHelpers.config, { message: errMessage, error: err, isError: true });
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/loader/exit.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,9 +122,9 @@ function abort_promises(reason: any) {
loaderHelpers.afterConfigLoaded.promise_control.reject(reason);
loaderHelpers.wasmDownloadPromise.promise_control.reject(reason);
loaderHelpers.runtimeModuleLoaded.promise_control.reject(reason);
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
if (runtimeHelpers.dotnetReady) {
runtimeHelpers.dotnetReady.promise_control.reject(reason);
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
runtimeHelpers.afterInstantiateWasm.promise_control.reject(reason);
runtimeHelpers.beforePreInit.promise_control.reject(reason);
runtimeHelpers.afterPreInit.promise_control.reject(reason);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/loader/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,7 @@ export function setLoaderGlobals(
allDownloadsQueued: createPromiseController<void>(),
wasmDownloadPromise: createPromiseController<AssetEntryInternal>(),
runtimeModuleLoaded: createPromiseController<void>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),

is_exited,
is_runtime_running,
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -454,10 +454,11 @@ function importModules() {
}

async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) {
const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { default: emscriptenFactory } = es6Modules[1];
setRuntimeGlobals(globalObjectsRoot);
initializeExports(globalObjectsRoot);
await configureRuntimeStartup();
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
Expand DownExpand Up@@ -494,9 +495,8 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
mono_exit(1, err);
});

init_globalization();

setTimeout(() => {
init_globalization();
mono_download_assets(); // intentionally not awaited
}, 0);

Expand Down
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/snapshot.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,22 +44,35 @@ async function openCache(): Promise<Cache | null> {
}
}

export async function getMemorySnapshotSize(): Promise<number | undefined> {
export async function checkMemorySnapshotSize(): Promise<void> {
try {
if (!runtimeHelpers.config.startupMemoryCache) {
// we could start downloading DLLs because snapshot is disabled
return;
}

const cacheKey = await getCacheKey();
if (!cacheKey) {
return undefined;
return;
}
const cache = await openCache();
if (!cache) {
return undefined;
return;
}
const res = await cache.match(cacheKey);
const contentLength = res?.headers.get("content-length");
return contentLength ? parseInt(contentLength) : undefined;
const memorySize = contentLength ? parseInt(contentLength) : undefined;

runtimeHelpers.loadedMemorySnapshotSize = memorySize;
runtimeHelpers.storeMemorySnapshotPending = !memorySize;
} catch (ex) {
mono_log_warn("Failed find memory snapshot in the cache", ex);
return undefined;
}
finally {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
// we could start downloading DLLs because there is no snapshot yet
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
}
}

Expand Down
46 changes: 19 additions & 27 deletions src/mono/wasm/runtime/startup.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ import { instantiate_wasm_asset, wait_for_all_assets } from "./assets";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { replace_linker_placeholders } from "./exports-binding";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { getMemorySnapshot, storeMemorySnapshot, getMemorySnapshotSize } from "./snapshot";
import { checkMemorySnapshotSize, getMemorySnapshot, storeMemorySnapshot } from "./snapshot";
import { mono_log_debug, mono_log_error, mono_log_warn, mono_set_thread_id } from "./logging";

// threads
Expand All@@ -40,6 +40,19 @@ import { assertNoProxies } from "./gc-handles";
// default size if MonoConfig.pthreadPoolSize is undefined
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

// we are making emscripten startup async friendly
// emscripten is executing the events without awaiting it and so we need to block progress via PromiseControllers above
export function configureEmscriptenStartup(module: DotnetModuleInternal): void {
Expand DownExpand Up@@ -118,8 +131,6 @@ function instantiateWasm(

const mark = startMeasure();
if (userInstantiateWasm) {
// user wasm instantiation doesn't support memory snapshots
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
const exports = userInstantiateWasm(imports, (instance: WebAssembly.Instance, module: WebAssembly.Module | undefined) => {
endMeasure(mark, MeasuredBlock.instantiateWasm);
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
Expand DownExpand Up@@ -377,14 +388,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
Expand DownExpand Up@@ -459,20 +462,9 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
let memorySize: number | undefined = undefined;
await loaderHelpers.afterConfigLoaded;
mono_log_debug("instantiate_wasm_module");

if (runtimeHelpers.config.startupMemoryCache) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}

await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

Expand All@@ -486,19 +478,19 @@ async function instantiate_wasm_module(

mono_log_debug("instantiate_wasm_module done");

if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
try {
const wasmMemory = Module.getMemory();

// .grow() takes a delta compared to the previous size
wasmMemory.grow((memorySize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
wasmMemory.grow((runtimeHelpers.loadedMemorySnapshotSize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
Comment thread
kg marked this conversation as resolved.
runtimeHelpers.updateMemoryViews();
} catch (err) {
mono_log_warn("failed to resize memory for the snapshot", err);
runtimeHelpers.loadedMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshotSize = undefined;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
} catch (err) {
Expand All@@ -511,7 +503,7 @@ async function instantiate_wasm_module(

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
// get the bytes after we re-sized the memory, so that we don't have too much memory in use at the same time
const memoryBytes = await getMemorySnapshot();
const heapU8 = localHeapViewU8();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,7 @@
<link rel="preload" href="./blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./System.Private.CoreLib.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./advanced-sample.lib.module.js" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,6 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withConfig({
startupMemoryCache: true,
resources: {
modulesAfterConfigLoaded: {
"advanced-sample.lib.module.js": ""
Expand Down
3 changes: 0 additions & 3 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,9 +12,6 @@
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./_framework/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
5 changes: 4 additions & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,14 +31,17 @@ try {
}

const runtime = await dotnet
.withConfig({
maxParallelDownloads: 10000,
Comment thread
kg marked this conversation as resolved.
// diagnosticTracing:true,
})
.withModuleConfig({
printErr: () => undefined,
print: () => undefined,
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
})
.create();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/features.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -193,12 +193,13 @@ See also [fetch integrity on MDN](https://developer.mozilla.org/en-US/docs/Web/A

### Pre-fetching
In order to start downloading application resources as soon as possible you can add HTML elements to `<head>` of your page similar to:
Adding too many files into prefetch could be counterproductive.
Please benchmark your startup performance on real target devices and with realistic network conditions.

```html
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="use-credentials">
<link rel="prefetch" href="./_framework/dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.runtime.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./_framework/dotnet.native.wasm" as="fetch" crossorigin="anonymous">
```

See also [link rel prefetch on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch)
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import type { RuntimeAPI } from "./types";

import { Module, linkerDisableLegacyJsInterop, exportedRuntimeAPI, passEmscriptenInternals, runtimeHelpers, setRuntimeGlobals, } from "./globals";
import { GlobalObjects, is_nullish } from "./types/internal";
import { configureEmscriptenStartup, configureWorkerStartup } from "./startup";
import { configureEmscriptenStartup, configureRuntimeStartup, configureWorkerStartup } from "./startup";

import { create_weak_ref } from "./weak-ref";
import { export_internal } from "./exports-internal";
Expand DownExpand Up@@ -143,5 +143,5 @@ class RuntimeList {

// export external API
export {
passEmscriptenInternals, initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
passEmscriptenInternals, initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals
};
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -59,7 +59,6 @@ export function setRuntimeGlobals(globalObjects: GlobalObjects) {
gitHash,
allAssetsInMemory: createPromiseController<void>(),
dotnetReady: createPromiseController<any>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),
afterInstantiateWasm: createPromiseController<void>(),
beforePreInit: createPromiseController<void>(),
afterPreInit: createPromiseController<void>(),
Expand Down
18 changes: 7 additions & 11 deletions src/mono/wasm/runtime/loader/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,8 +78,6 @@ const containedInSnapshotByAssetTypes: {
"pdb": true,
"heap": true,
"icu": true,
...jsModulesAssetTypes,
"dotnetwasm": true,
};

// these assets are instantiated differently than the main flow
Expand All@@ -95,7 +93,7 @@ export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != loaderHelpers.preferredIcuAsset);
}

function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
function convert_single_asset(assetsCollection: AssetEntryInternal[], resource: ResourceList | undefined, behavior: SingleAssetBehaviors): AssetEntryInternal {
const keys = Object.keys(resource || {});
mono_assert(keys.length == 1, `Expect to have one ${behavior} asset in resources`);

Expand All@@ -110,7 +108,7 @@ function convert_single_asset(modulesAssets: AssetEntryInternal[], resource: Res
set_single_asset(asset);

// so that we can use it on the worker too
modulesAssets.push(asset);
assetsCollection.push(asset);
return asset;
}

Expand DownExpand Up@@ -168,15 +166,12 @@ export async function mono_download_assets(): Promise<void> {
countAndStartDownload(asset);
}

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

// continue after we know if memory snapshot is available or not
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
await loaderHelpers.memorySnapshotSkippedOrDone.promise;

// start fetching assets in parallel, only if memory snapshot is not available.
for (const asset of containedInSnapshotAssets) {
if (!runtimeHelpers.loadedMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
countAndStartDownload(asset);
} else {
// Otherwise cleanup in case we were given pending download. It would be even better if we could abort the download.
Expand All@@ -193,6 +188,8 @@ export async function mono_download_assets(): Promise<void> {
}

loaderHelpers.allDownloadsQueued.promise_control.resolve();

// continue after the dotnet.runtime.js was loaded
await loaderHelpers.runtimeModuleLoaded.promise;

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All@@ -211,7 +208,6 @@ export async function mono_download_assets(): Promise<void> {
// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped

await runtimeHelpers.beforeOnRuntimeInitialized.promise;
await runtimeHelpers.memorySnapshotSkippedOrDone.promise;
runtimeHelpers.instantiate_asset(asset, url, data);
}
} else {
Expand DownExpand Up@@ -284,7 +280,7 @@ export function prepareAssets() {
mono_assert(resources.jsModuleNative, "resources.jsModuleNative must be defined");
mono_assert(resources.jsModuleRuntime, "resources.jsModuleRuntime must be defined");
mono_assert(!MonoWasmThreads || resources.jsModuleWorker, "resources.jsModuleWorker must be defined");
convert_single_asset(modulesAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(alwaysLoadedAssets, resources.wasmNative, "dotnetwasm");
convert_single_asset(modulesAssets, resources.jsModuleNative, "js-module-native");
convert_single_asset(modulesAssets, resources.jsModuleRuntime, "js-module-runtime");
if (MonoWasmThreads) {
Expand Down
19 changes: 11 additions & 8 deletions src/mono/wasm/runtime/loader/config.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { mono_assert } from "./globals";

export function deep_merge_config(target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand DownExpand Up@@ -220,15 +221,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi
await loaderHelpers.afterConfigLoaded.promise;
return;
}
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
return;
}
mono_log_debug("mono_wasm_load_config");
try {
await loadBootConfig(module);
configLoaded = true;
if (configFilePath) {
mono_log_debug("mono_wasm_load_config");
await loadBootConfig(module);
}

normalizeConfig();

Expand All@@ -249,7 +247,12 @@ export async function mono_wasm_load_config(module: DotnetModuleInternal): Promi

normalizeConfig();

mono_assert(!loaderHelpers.config.startupMemoryCache || !module.instantiateWasm, "startupMemoryCache is not supported with Module.instantiateWasm");

loaderHelpers.afterConfigLoaded.promise_control.resolve(loaderHelpers.config);
if (!loaderHelpers.config.startupMemoryCache) {
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err} ${(err as Error)?.stack}`;
loaderHelpers.config = module.config = Object.assign(loaderHelpers.config, { message: errMessage, error: err, isError: true });
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/loader/exit.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,9 +122,9 @@ function abort_promises(reason: any) {
loaderHelpers.afterConfigLoaded.promise_control.reject(reason);
loaderHelpers.wasmDownloadPromise.promise_control.reject(reason);
loaderHelpers.runtimeModuleLoaded.promise_control.reject(reason);
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
if (runtimeHelpers.dotnetReady) {
runtimeHelpers.dotnetReady.promise_control.reject(reason);
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.reject(reason);
runtimeHelpers.afterInstantiateWasm.promise_control.reject(reason);
runtimeHelpers.beforePreInit.promise_control.reject(reason);
runtimeHelpers.afterPreInit.promise_control.reject(reason);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/loader/globals.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -87,6 +87,7 @@ export function setLoaderGlobals(
allDownloadsQueued: createPromiseController<void>(),
wasmDownloadPromise: createPromiseController<AssetEntryInternal>(),
runtimeModuleLoaded: createPromiseController<void>(),
memorySnapshotSkippedOrDone: createPromiseController<void>(),

is_exited,
is_runtime_running,
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasm/runtime/loader/run.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -454,10 +454,11 @@ function importModules() {
}

async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) {
const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { initializeExports, initializeReplacements, configureRuntimeStartup, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0];
const { default: emscriptenFactory } = es6Modules[1];
setRuntimeGlobals(globalObjectsRoot);
initializeExports(globalObjectsRoot);
await configureRuntimeStartup();
loaderHelpers.runtimeModuleLoaded.promise_control.resolve();

emscriptenFactory((originalModule: EmscriptenModuleInternal) => {
Expand DownExpand Up@@ -494,9 +495,8 @@ async function createEmscriptenMain(): Promise<RuntimeAPI> {
mono_exit(1, err);
});

init_globalization();

setTimeout(() => {
init_globalization();
mono_download_assets(); // intentionally not awaited
}, 0);

Expand Down
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/snapshot.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,22 +44,35 @@ async function openCache(): Promise<Cache | null> {
}
}

export async function getMemorySnapshotSize(): Promise<number | undefined> {
export async function checkMemorySnapshotSize(): Promise<void> {
try {
if (!runtimeHelpers.config.startupMemoryCache) {
// we could start downloading DLLs because snapshot is disabled
return;
}

const cacheKey = await getCacheKey();
if (!cacheKey) {
return undefined;
return;
}
const cache = await openCache();
if (!cache) {
return undefined;
return;
}
const res = await cache.match(cacheKey);
const contentLength = res?.headers.get("content-length");
return contentLength ? parseInt(contentLength) : undefined;
const memorySize = contentLength ? parseInt(contentLength) : undefined;

runtimeHelpers.loadedMemorySnapshotSize = memorySize;
runtimeHelpers.storeMemorySnapshotPending = !memorySize;
} catch (ex) {
mono_log_warn("Failed find memory snapshot in the cache", ex);
return undefined;
}
finally {
if (!runtimeHelpers.loadedMemorySnapshotSize) {
// we could start downloading DLLs because there is no snapshot yet
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
}
}

Expand Down
46 changes: 19 additions & 27 deletions src/mono/wasm/runtime/startup.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,7 +21,7 @@ import { instantiate_wasm_asset, wait_for_all_assets } from "./assets";
import { mono_wasm_init_diagnostics } from "./diagnostics";
import { replace_linker_placeholders } from "./exports-binding";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { getMemorySnapshot, storeMemorySnapshot, getMemorySnapshotSize } from "./snapshot";
import { checkMemorySnapshotSize, getMemorySnapshot, storeMemorySnapshot } from "./snapshot";
import { mono_log_debug, mono_log_error, mono_log_warn, mono_set_thread_id } from "./logging";

// threads
Expand All@@ -40,6 +40,19 @@ import { assertNoProxies } from "./gc-handles";
// default size if MonoConfig.pthreadPoolSize is undefined
const MONO_PTHREAD_POOL_SIZE = 4;

export async function configureRuntimeStartup(): Promise<void> {
if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

await checkMemorySnapshotSize();
}

// we are making emscripten startup async friendly
// emscripten is executing the events without awaiting it and so we need to block progress via PromiseControllers above
export function configureEmscriptenStartup(module: DotnetModuleInternal): void {
Expand DownExpand Up@@ -118,8 +131,6 @@ function instantiateWasm(

const mark = startMeasure();
if (userInstantiateWasm) {
// user wasm instantiation doesn't support memory snapshots
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
const exports = userInstantiateWasm(imports, (instance: WebAssembly.Instance, module: WebAssembly.Module | undefined) => {
endMeasure(mark, MeasuredBlock.instantiateWasm);
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
Expand DownExpand Up@@ -377,14 +388,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
mono_log_debug("mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

if (linkerWasmEnableSIMD) {
mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}
if (linkerWasmEnableEH) {
mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features");
}

await init_polyfills_async();

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, runtimeHelpers.config);
Expand DownExpand Up@@ -459,20 +462,9 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
let memorySize: number | undefined = undefined;
await loaderHelpers.afterConfigLoaded;
mono_log_debug("instantiate_wasm_module");

if (runtimeHelpers.config.startupMemoryCache) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}

await runtimeHelpers.beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

Expand All@@ -486,19 +478,19 @@ async function instantiate_wasm_module(

mono_log_debug("instantiate_wasm_module done");

if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
try {
const wasmMemory = Module.getMemory();

// .grow() takes a delta compared to the previous size
wasmMemory.grow((memorySize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
wasmMemory.grow((runtimeHelpers.loadedMemorySnapshotSize! - wasmMemory.buffer.byteLength + 65535) >>> 16);
Comment thread
kg marked this conversation as resolved.
runtimeHelpers.updateMemoryViews();
} catch (err) {
mono_log_warn("failed to resize memory for the snapshot", err);
runtimeHelpers.loadedMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshotSize = undefined;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
runtimeHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
loaderHelpers.memorySnapshotSkippedOrDone.promise_control.resolve();
}
runtimeHelpers.afterInstantiateWasm.promise_control.resolve();
} catch (err) {
Expand All@@ -511,7 +503,7 @@ async function instantiate_wasm_module(

async function mono_wasm_before_memory_snapshot() {
const mark = startMeasure();
if (runtimeHelpers.loadedMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshotSize) {
// get the bytes after we re-sized the memory, so that we don't have too much memory in use at the same time
const memoryBytes = await getMemorySnapshot();
const heapU8 = localHeapViewU8();
Expand Down
Loading