From 53009b26dd2e8b75fba8b44849e1d323ddb2a31f Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Sun, 22 Jun 2025 15:40:02 -0400 Subject: runtime: use a smaller arena size on Wasm On Wasm, some programs have very small heap. Currently, we use 4 MB arena size (like all other 32-bit platforms). For a very small program, it needs to allocate one heap arena, 4 MB size at a 4 MB aligned address. So we'll need 8 MB of linear memory, whereas only a smaller portion is actually used by the program. On Wasm, samll programs are not uncommon (e.g. WASI plugins), and users are concerned about the memory usage. This CL switches to a smaller arena size, as well as a smaller page allocator chunk size (both are now 512 KB). So the heap will be grown in 512 KB granularity. For a helloworld program, it now uses less than 3 MB of linear memory, instead of 8 MB. Change-Id: Ibd66c1fa6e794a12c00906cbacc8f2e410f196c4 Reviewed-on: https://go-review.googlesource.com/c/go/+/683296 Reviewed-by: David Chase Reviewed-by: Michael Knyszek LUCI-TryBot-Result: Go LUCI --- src/runtime/mpagealloc.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/runtime/mpagealloc.go') diff --git a/src/runtime/mpagealloc.go b/src/runtime/mpagealloc.go index 83db005051..a30434a534 100644 --- a/src/runtime/mpagealloc.go +++ b/src/runtime/mpagealloc.go @@ -48,6 +48,7 @@ package runtime import ( + "internal/goarch" "internal/runtime/atomic" "internal/runtime/gc" "unsafe" @@ -55,10 +56,12 @@ import ( const ( // The size of a bitmap chunk, i.e. the amount of bits (that is, pages) to consider - // in the bitmap at once. + // in the bitmap at once. It is 4MB on most platforms, except on Wasm it is 512KB. + // We use a smaller chuck size on Wasm for the same reason as the smaller arena + // size (see heapArenaBytes). pallocChunkPages = 1 << logPallocChunkPages pallocChunkBytes = pallocChunkPages * pageSize - logPallocChunkPages = 9 + logPallocChunkPages = 9*(1-goarch.IsWasm) + 6*goarch.IsWasm logPallocChunkBytes = logPallocChunkPages + gc.PageShift // The number of radix bits for each level. @@ -220,6 +223,7 @@ type pageAlloc struct { // heapAddrBits | L1 Bits | L2 Bits | L2 Entry Size // ------------------------------------------------ // 32 | 0 | 10 | 128 KiB + // 32 (wasm) | 0 | 13 | 128 KiB // 33 (iOS) | 0 | 11 | 256 KiB // 48 | 13 | 13 | 1 MiB // -- cgit v1.3