Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
ea02d94
wip
pavelsavara Mar 3, 2023
c1aa6c3
wip
pavelsavara Mar 3, 2023
bf9ef4d
wip
pavelsavara Mar 3, 2023
f883a72
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 3, 2023
3fe9aba
wip
pavelsavara Mar 3, 2023
f4225aa
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 3, 2023
d2e5b49
fix
pavelsavara Mar 3, 2023
c409f51
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 6, 2023
e695c42
feedback
pavelsavara Mar 6, 2023
1189015
lint
pavelsavara Mar 6, 2023
b92c82e
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
7ccc8e6
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
ec762ac
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
91c0211
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
20d548b
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
8e6865c
more linear memory usage and recovery from failure of snapshot use
pavelsavara Mar 6, 2023
7bf7881
feedback
pavelsavara Mar 6, 2023
aec9b53
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 8, 2023
5651f37
start runtime twice for unit tests, so that the actual tests is runni…
pavelsavara Mar 8, 2023
a16f667
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 8, 2023
31b46ff
wip
pavelsavara Mar 8, 2023
43a73e1
continue loading non-snapshot assets
pavelsavara Mar 8, 2023
f49354a
disable adding methods to __indirect_function_table before we take me…
pavelsavara Mar 8, 2023
53781ab
rename startupMemoryCache
pavelsavara Mar 8, 2023
60b2c5a
try to fix threads
pavelsavara Mar 8, 2023
33b6e3e
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 8, 2023
977b46c
whitespace
pavelsavara Mar 8, 2023
933594b
fix
pavelsavara Mar 9, 2023
c68c9e8
fix storing memory
pavelsavara Mar 9, 2023
fa3e331
assert
pavelsavara Mar 9, 2023
c7f913b
- make snapshot hash more resilient
pavelsavara Mar 9, 2023
9cb1b72
fix
pavelsavara Mar 9, 2023
d1bbb9c
- renamed memorySnapshotSkippedOrDone
pavelsavara Mar 9, 2023
ee4ba76
- rename loadedMemorySnapshot
pavelsavara Mar 9, 2023
83f0032
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 9, 2023
85ac81d
fix
pavelsavara Mar 9, 2023
b7790f2
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 10, 2023
3168c42
- internal config exitAfterSnapshot
pavelsavara Mar 10, 2023
4a25447
measure
pavelsavara Mar 10, 2023
5067cf7
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 11, 2023
1761786
fix
pavelsavara Mar 11, 2023
94e931a
fix
pavelsavara Mar 13, 2023
2a7508e
optimize timezone detection
pavelsavara Mar 13, 2023
9759ad8
feedback
pavelsavara Mar 13, 2023
1f4e4c6
fix
pavelsavara Mar 13, 2023
e2a2cd4
- enable snapshot in blazor
pavelsavara Mar 13, 2023
eaaeaa5
store cache key
pavelsavara Mar 13, 2023
bc86ffe
feedback and cleanup
pavelsavara Mar 13, 2023
59a7a56
fix debugging
pavelsavara Mar 13, 2023
6d9f7ee
fix
pavelsavara Mar 14, 2023
3e961d1
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 14, 2023
f617787
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 14, 2023
cfc5acf
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 14, 2023
c78ef57
Update src/mono/wasm/runtime/assets.ts
pavelsavara Mar 14, 2023
7309e43
Update src/mono/wasm/runtime/assets.ts
pavelsavara Mar 14, 2023
5f8aeef
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 14, 2023
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
Prev Previous commit
Next Next commit
feedback
  • Loading branch information
pavelsavara committed Mar 6, 2023
commit e695c421f4ff4950697b9434bd5025830f10d858
27 changes: 27 additions & 0 deletions src/mono/wasm/memory-snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Memory snapshot of the Mono runtime #

We take snapshot of WASM memory after first cold start at run time​.
We store it on the client side in the browser cache.
For subsequent start of the application with the same configuration and same assets, we could use the snapshot.
Instead of downloading all the assets and doing the runtime startup again.
Such subsequent start is significantly faster.

### Implementation details

- the consistency of inputs (configuration and assets) with the snapshot is done by calculating SHA256 of the inputs.
- the DLLs and other downloaded assets each have SHA256 which is used to validate 'integrity' by the browser.
- the mono-config has field `assetsHash` which is summary SHA256 of all the assets.
- the configuration could be changed programmatically and se we calculate the hash at the runtime, just before taking snapshot.
- the snapshot is taken just after we initialize the Mono runtime
- cwraps initialized (they would be initialized again on subsequent start)
- after loading all the DLLs into memory.
- after loading ICU and timezone data
- after applying environment variables and other runtime options.
- before any worker threads init
- before any JavaScript interop initialization
- before any Managed code initialization
- therefore we do not expect to store any application state in the snapshot.

### How to opt out
- you can use new API `withMemoryCache(false)` to opt out from the feature.

23 changes: 4 additions & 19 deletions src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ export function abort_startup(reason: any, should_exit: boolean): void {
}
}

// runs in both blazor and non-blazor
function mono_wasm_pre_init_essential(isWorker: boolean): void {
if (!isWorker)
Module.addRunDependency("mono_wasm_pre_init_essential");
Expand All @@ -321,22 +320,16 @@ function mono_wasm_pre_init_essential(isWorker: boolean): void {
Module.removeRunDependency("mono_wasm_pre_init_essential");
}

// runs in both blazor and non-blazor
async function mono_wasm_pre_init_essential_async(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");

await init_polyfills_async();
await mono_wasm_load_config(Module.configSrc);

if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, config);
}

Module.removeRunDependency("mono_wasm_pre_init_essential_async");
}

// runs just in non-blazor
async function mono_wasm_pre_init_full(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_pre_init_full");
Module.addRunDependency("mono_wasm_pre_init_full");
Expand All @@ -347,7 +340,6 @@ async function mono_wasm_pre_init_full(): Promise<void> {
Module.removeRunDependency("mono_wasm_pre_init_full");
}

// runs just in non-blazor
async function mono_wasm_before_user_runtime_initialized(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_before_user_runtime_initialized");

Expand All @@ -359,6 +351,10 @@ async function mono_wasm_before_user_runtime_initialized(): Promise<void> {
if (config.cacheMemory && !runtimeHelpers.memoryIsLoaded) {
await storeMemory(Module.HEAP8.buffer);
}
if (MonoWasmThreads) {
preAllocatePThreadWorkerPool(MONO_PTHREAD_POOL_SIZE, config);
await mono_wasm_init_diagnostics();
}
bindings_init();
if (!runtimeHelpers.mono_wasm_runtime_is_ready) mono_wasm_runtime_ready();
if (!runtimeHelpers.mono_wasm_symbols_are_ready) readSymbolMapFile("dotnet.js.symbols");
Expand All @@ -373,7 +369,6 @@ async function mono_wasm_before_user_runtime_initialized(): Promise<void> {
}
}

// runs in both blazor and non-blazor
async function mono_wasm_after_user_runtime_initialized(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_after_user_runtime_initialized");
try {
Expand All @@ -394,10 +389,6 @@ async function mono_wasm_after_user_runtime_initialized(): Promise<void> {
}
}
}
// for Blazor, init diagnostics after their "onRuntimeInitalized" sets env variables, but before their postRun callback (which calls mono_wasm_load_runtime)
if (MonoWasmThreads) {
await mono_wasm_init_diagnostics();
}

if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: Initializing mono runtime");

Expand Down Expand Up @@ -509,7 +500,6 @@ async function instantiate_wasm_module(
Module.removeRunDependency("instantiate_wasm_module");
}

// runs just in non-blazor
async function _apply_configuration_from_args() {
// create /usr/share folder which is SpecialFolder.CommonApplicationData
Module["FS_createPath"]("/", "usr", true, true);
Expand All @@ -531,11 +521,6 @@ async function _apply_configuration_from_args() {

if (config.browserProfilerOptions)
mono_wasm_init_browser_profiler(config.browserProfilerOptions);

// for non-Blazor, init diagnostics after environment variables are set
if (MonoWasmThreads) {
await mono_wasm_init_diagnostics();
}
}

export function mono_wasm_load_runtime(unused?: string, debugLevel?: number): void {
Expand Down
7 changes: 5 additions & 2 deletions src/mono/wasm/runtime/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ProductVersion from "consts:productVersion";
import GitHash from "consts:gitHash";
import { runtimeHelpers } from "./imports";

const memoryPrefix = "https://dotnet.generated.invalid/wasm-memory";

// adapted from Blazor's WebAssemblyResourceLoader.ts
async function open(): Promise<Cache | null> {
// caches will be undefined if we're running on an insecure origin (secure means https or localhost)
Expand Down Expand Up @@ -36,10 +38,11 @@ async function open(): Promise<Cache | null> {
} catch {
// There's no known scenario where we should get an exception here, but considering the
// Chromium bug above, let's tolerate it and treat as "proceed without caching".
console.warn("MONO_WASM: Failed to open cache");
return null;
}
}
const memoryPrefix = "https://dotnet.generated.invalid/wasm-memory";

export async function getMemory(): Promise<ArrayBuffer | undefined> {
try {
const inputsHash = await getInputsHash();
Expand Down Expand Up @@ -123,4 +126,4 @@ export async function getInputsHash(): Promise<string | null> {
const uint8ViewOfHash = new Uint8Array(sha256Buffer);
const hashAsString = Array.from(uint8ViewOfHash).map((b) => b.toString(16).padStart(2, "0")).join("");
return hashAsString;
}
}
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,4 @@ export function js_string_to_mono_string_new(string: string): MonoString {
} finally {
temp.release();
}
}
}