mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime, cmd/link: optimize memory allocation on wasm
WebAssembly's memory is contiguous. Allocating memory at a high address also allocates all memory up to that address. This change reduces the initial memory allocated on wasm from 1GB to 16MB by using multiple heap arenas and reducing the size of a heap arena. Fixes #27462. Change-Id: Ic941e6edcadd411e65a14cb2f9fd6c8eae02fc7a Reviewed-on: https://go-review.googlesource.com/c/go/+/170950 Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
e47090ab40
commit
460f9c6068
2 changed files with 11 additions and 11 deletions
|
|
@ -297,18 +297,18 @@ func writeTableSec(ctxt *ld.Link, fns []*wasmFunc) {
|
|||
}
|
||||
|
||||
// writeMemorySec writes the section that declares linear memories. Currently one linear memory is being used.
|
||||
// Linear memory always starts at address zero. More memory can be requested with the GrowMemory instruction.
|
||||
func writeMemorySec(ctxt *ld.Link) {
|
||||
sizeOffset := writeSecHeader(ctxt, sectionMemory)
|
||||
|
||||
// Linear memory always starts at address zero.
|
||||
// The unit of the sizes is "WebAssembly page size", which is 64Ki.
|
||||
// The minimum is currently set to 1GB, which is a lot.
|
||||
// More memory can be requested with the grow_memory instruction,
|
||||
// but this operation currently is rather slow, so we avoid it for now.
|
||||
// TODO(neelance): Use lower initial memory size.
|
||||
writeUleb128(ctxt.Out, 1) // number of memories
|
||||
ctxt.Out.WriteByte(0x00) // no maximum memory size
|
||||
writeUleb128(ctxt.Out, 1024*16) // minimum (initial) memory size
|
||||
const (
|
||||
initialSize = 16 << 20 // 16MB, enough for runtime init without growing
|
||||
wasmPageSize = 64 << 10 // 64KB
|
||||
)
|
||||
|
||||
writeUleb128(ctxt.Out, 1) // number of memories
|
||||
ctxt.Out.WriteByte(0x00) // no maximum memory size
|
||||
writeUleb128(ctxt.Out, initialSize/wasmPageSize) // minimum (initial) memory size
|
||||
|
||||
writeSecSize(ctxt, sizeOffset)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue