]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: reduce Wasm initial memory size
authorCherry Mui <cherryyz@google.com>
Mon, 21 Oct 2024 17:09:58 +0000 (13:09 -0400)
committerCherry Mui <cherryyz@google.com>
Mon, 21 Oct 2024 21:53:15 +0000 (21:53 +0000)
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>

src/cmd/link/internal/wasm/asm.go

index 2a4c1ee7ea78724f58daf2f969f7e63ff2369383..3728cc6dc3a3484bd1958705f3187c7a5e52aa91 100644 (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