From 24bc4731739d55826a8adfcdb2aa1217f9db3bc8 Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Mon, 21 Oct 2024 13:09:58 -0400 Subject: [PATCH] cmd/link: reduce Wasm initial memory size Currently, for Wasm, the linker sets the initial memory size to the size of global data plus 16 MB. The intention is that it covers the global data and runtime initialization without growing the linear memory. However, the code accounts only the data "section", not the bss "section", therefore the extra 16 MB is actually used to cover bss variables. Also, as seen on the previous CL, the runtime actually didn't use the extra space, which means the program can start without that space. This CL corrects the global data size calculation, and reduces the extra to 1 MB. Currently the runtime's allocation pattern at startup is that it allocates a few pages for the page allocator's metadata, the an 8 MB reservation for the first 4 MB size, 4 MB aligned heap arena (it may be possible to reduce that, but we'll leave that for later). Here we use 1 MB extra space to cover the small allocations, but let the runtime allocate the heap arena, so the linker code and the runtime's allocator are not tightly coupled. For #69018. Change-Id: I39fe1172382ecc03f4b537e43ec710af8075eab3 Reviewed-on: https://go-review.googlesource.com/c/go/+/621636 Reviewed-by: Michael Knyszek LUCI-TryBot-Result: Go LUCI --- src/cmd/link/internal/wasm/asm.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cmd/link/internal/wasm/asm.go b/src/cmd/link/internal/wasm/asm.go index 2a4c1ee7ea..3728cc6dc3 100644 --- a/src/cmd/link/internal/wasm/asm.go +++ b/src/cmd/link/internal/wasm/asm.go @@ -365,9 +365,8 @@ func writeTableSec(ctxt *ld.Link, fns []*wasmFunc) { func writeMemorySec(ctxt *ld.Link, ldr *loader.Loader) { sizeOffset := writeSecHeader(ctxt, sectionMemory) - dataSection := ldr.SymSect(ldr.Lookup("runtime.data", 0)) - dataEnd := dataSection.Vaddr + dataSection.Length - var initialSize = dataEnd + 16<<20 // 16MB, enough for runtime init without growing + dataEnd := uint64(ldr.SymValue(ldr.Lookup("runtime.end", 0))) + var initialSize = dataEnd + 1<<20 // 1 MB, for runtime init allocating a few pages const wasmPageSize = 64 << 10 // 64KB -- 2.48.1