From d150badf33d6087bdad8da5eb6a680e13dafe6ff Mon Sep 17 00:00:00 2001 From: "L. Pereira" Date: Mon, 8 Jul 2024 16:34:40 -0700 Subject: [PATCH] gc_leaking: Don't zero out new allocations in some targets (#4302) Wasm linear memory is always initialized to zero by definition, so there's no need to waste time zeroing out this allocation. This is the case for freshly-obtained memory from mmap(). Signed-off-by: L. Pereira --- src/runtime/gc_leaking.go | 2 +- src/runtime/zero_new_alloc.go | 12 ++++++++++++ src/runtime/zero_new_alloc_noop.go | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/runtime/zero_new_alloc.go create mode 100644 src/runtime/zero_new_alloc_noop.go diff --git a/src/runtime/gc_leaking.go b/src/runtime/gc_leaking.go index 26b012bdb..5c9594277 100644 --- a/src/runtime/gc_leaking.go +++ b/src/runtime/gc_leaking.go @@ -44,7 +44,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer { runtimePanic("out of memory") } pointer := unsafe.Pointer(addr) - memzero(pointer, size) + zero_new_alloc(pointer, size) return pointer } diff --git a/src/runtime/zero_new_alloc.go b/src/runtime/zero_new_alloc.go new file mode 100644 index 000000000..b94190ae0 --- /dev/null +++ b/src/runtime/zero_new_alloc.go @@ -0,0 +1,12 @@ +//go:build gc.leaking && (baremetal || nintendoswitch) + +package runtime + +import ( + "unsafe" +) + +//go:inline +func zero_new_alloc(ptr unsafe.Pointer, size uintptr) { + memzero(ptr, size) +} diff --git a/src/runtime/zero_new_alloc_noop.go b/src/runtime/zero_new_alloc_noop.go new file mode 100644 index 000000000..79fefc199 --- /dev/null +++ b/src/runtime/zero_new_alloc_noop.go @@ -0,0 +1,14 @@ +//go:build gc.leaking && !baremetal && !nintendoswitch + +package runtime + +import ( + "unsafe" +) + +//go:inline +func zero_new_alloc(ptr unsafe.Pointer, size uintptr) { + // Wasm linear memory is initialized to zero by default, so + // there's no need to do anything. This is also the case for + // fresh-allocated memory from an mmap() system call. +}