Skip to content

Commit

Permalink
[browser][MT] faster memory view refresh + cleanup (#101249)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara authored Apr 19, 2024
1 parent 4f889f6 commit 9c237f0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 24 deletions.
4 changes: 1 addition & 3 deletions src/mono/browser/runtime/cancelable-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ export class PromiseHolder extends ManagedObject {
try {
mono_assert(!this.isPosted, "Promise is already posted to managed.");
this.isPosted = true;
if (WasmEnableThreads) {
forceThreadMemoryViewRefresh();
}
forceThreadMemoryViewRefresh();

// we can unregister the GC handle just on JS side
teardown_managed_proxy(this, this.gc_handle, /*skipManaged: */ true);
Expand Down
4 changes: 1 addition & 3 deletions src/mono/browser/runtime/marshal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ const enum JSBindingHeaderOffsets {
}

export function alloc_stack_frame (size: number): JSMarshalerArguments {
if (WasmEnableThreads) {
forceThreadMemoryViewRefresh();
}
forceThreadMemoryViewRefresh();
const bytes = JavaScriptMarshalerArgSize * size;
const args = Module.stackAlloc(bytes) as any;
_zero_region(args, bytes);
Expand Down
8 changes: 5 additions & 3 deletions src/mono/browser/runtime/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ export function copyBytes (srcPtr: VoidPtr, dstPtr: VoidPtr, bytes: number): voi
// on non-MT build, this will be a no-op trimmed by rollup
export function receiveWorkerHeapViews () {
if (!WasmEnableThreads) return;
const memory = runtimeHelpers.getMemory();
if (memory.buffer !== Module.HEAPU8.buffer) {
const wasmMemory = runtimeHelpers.getMemory();
if (wasmMemory.buffer !== Module.HEAPU8.buffer) {
runtimeHelpers.updateMemoryViews();
}
}
Expand Down Expand Up @@ -484,5 +484,7 @@ export function forceThreadMemoryViewRefresh () {
This only works because their implementation does not skip doing work even when you ask to grow by 0 pages.
*/
wasmMemory.grow(0);
runtimeHelpers.updateMemoryViews();
if (wasmMemory.buffer !== Module.HEAPU8.buffer) {
runtimeHelpers.updateMemoryViews();
}
}
4 changes: 1 addition & 3 deletions src/mono/browser/runtime/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ export function mono_wasm_schedule_timer (shortestDueTimeMs: number): void {
function mono_wasm_schedule_timer_tick () {
if (WasmEnableThreads) return;
Module.maybeExit();
if (WasmEnableThreads) {
forceThreadMemoryViewRefresh();
}
forceThreadMemoryViewRefresh();
if (!loaderHelpers.is_runtime_running()) {
return;
}
Expand Down
16 changes: 4 additions & 12 deletions src/mono/browser/runtime/web-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ export function ws_wasm_create (uri: string, sub_protocols: string[] | null, rec
try {
if (ws[wasm_ws_is_aborted]) return;
if (!loaderHelpers.is_runtime_running()) return;
if (WasmEnableThreads) {
forceThreadMemoryViewRefresh();
}
forceThreadMemoryViewRefresh();
open_promise_control.resolve(ws);
prevent_timer_throttling();
} catch (error: any) {
Expand All @@ -91,9 +89,7 @@ export function ws_wasm_create (uri: string, sub_protocols: string[] | null, rec
try {
if (ws[wasm_ws_is_aborted]) return;
if (!loaderHelpers.is_runtime_running()) return;
if (WasmEnableThreads) {
forceThreadMemoryViewRefresh();
}
forceThreadMemoryViewRefresh();
web_socket_on_message(ws, ev);
prevent_timer_throttling();
} catch (error: any) {
Expand All @@ -105,9 +101,7 @@ export function ws_wasm_create (uri: string, sub_protocols: string[] | null, rec
ws.removeEventListener("message", local_on_message);
if (ws[wasm_ws_is_aborted]) return;
if (!loaderHelpers.is_runtime_running()) return;
if (WasmEnableThreads) {
forceThreadMemoryViewRefresh();
}
forceThreadMemoryViewRefresh();

ws[wasm_ws_close_received] = true;
ws["close_status"] = ev.code;
Expand Down Expand Up @@ -137,9 +131,7 @@ export function ws_wasm_create (uri: string, sub_protocols: string[] | null, rec
try {
if (ws[wasm_ws_is_aborted]) return;
if (!loaderHelpers.is_runtime_running()) return;
if (WasmEnableThreads) {
forceThreadMemoryViewRefresh();
}
forceThreadMemoryViewRefresh();
ws.removeEventListener("message", local_on_message);
const message = ev.message
? "WebSocket error: " + ev.message
Expand Down

0 comments on commit 9c237f0

Please sign in to comment.