Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser][MT] faster memory view refresh + cleanup #101249

Merged
merged 1 commit into from
Apr 19, 2024
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason of using updateMemoryViewes here, not forceThreadMemoryViewRefresh that differ with calling wasmMemory.grow(0);? We don't need to propagate the knowledge about the update between threads?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that grow(0) is relatively expensive.
But the receiveWorkerHeapViews is called from too many places.

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
Loading