Skip to content

Commit d150bad

Browse files
authored
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 <[email protected]>
1 parent 2d85fc6 commit d150bad

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Diff for: src/runtime/gc_leaking.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
4444
runtimePanic("out of memory")
4545
}
4646
pointer := unsafe.Pointer(addr)
47-
memzero(pointer, size)
47+
zero_new_alloc(pointer, size)
4848
return pointer
4949
}
5050

Diff for: src/runtime/zero_new_alloc.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build gc.leaking && (baremetal || nintendoswitch)
2+
3+
package runtime
4+
5+
import (
6+
"unsafe"
7+
)
8+
9+
//go:inline
10+
func zero_new_alloc(ptr unsafe.Pointer, size uintptr) {
11+
memzero(ptr, size)
12+
}

Diff for: src/runtime/zero_new_alloc_noop.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build gc.leaking && !baremetal && !nintendoswitch
2+
3+
package runtime
4+
5+
import (
6+
"unsafe"
7+
)
8+
9+
//go:inline
10+
func zero_new_alloc(ptr unsafe.Pointer, size uintptr) {
11+
// Wasm linear memory is initialized to zero by default, so
12+
// there's no need to do anything. This is also the case for
13+
// fresh-allocated memory from an mmap() system call.
14+
}

0 commit comments

Comments
 (0)