1
0
mirror of https://github.com/golang/go synced 2024-11-13 12:40:26 -07:00

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 <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Cherry Mui 2024-10-21 13:09:58 -04:00
parent 4510586f93
commit 24bc473173

View File

@ -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