diff options
| author | Cherry Mui <cherryyz@google.com> | 2025-06-22 15:40:02 -0400 |
|---|---|---|
| committer | Cherry Mui <cherryyz@google.com> | 2025-09-26 17:13:31 -0700 |
| commit | 53009b26dd2e8b75fba8b44849e1d323ddb2a31f (patch) | |
| tree | 09416fd14429b1519dbce83fcd79034cd60596c6 /src/runtime/malloc.go | |
| parent | 3a5df9d2b20c0f059c463b5d51e89be17a2c685b (diff) | |
| download | go-53009b26dd2e8b75fba8b44849e1d323ddb2a31f.tar.xz | |
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 <drchase@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/runtime/malloc.go')
| -rw-r--r-- | src/runtime/malloc.go | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go index db91e89359..fc4f21b532 100644 --- a/src/runtime/malloc.go +++ b/src/runtime/malloc.go @@ -233,19 +233,22 @@ const ( // ios/arm64 40 4MB 1 256K (2MB) // */32-bit 32 4MB 1 1024 (4KB) // */mips(le) 31 4MB 1 512 (2KB) + // wasm 32 512KB 1 8192 (64KB) // heapArenaBytes is the size of a heap arena. The heap // consists of mappings of size heapArenaBytes, aligned to // heapArenaBytes. The initial heap mapping is one arena. // - // This is currently 64MB on 64-bit non-Windows and 4MB on - // 32-bit and on Windows. We use smaller arenas on Windows - // because all committed memory is charged to the process, - // even if it's not touched. Hence, for processes with small - // heaps, the mapped arena space needs to be commensurate. - // This is particularly important with the race detector, - // since it significantly amplifies the cost of committed - // memory. + // This is currently 64MB on 64-bit non-Windows, 4MB on + // 32-bit and on Windows, and 512KB on Wasm. We use smaller + // arenas on Windows because all committed memory is charged + // to the process, even if it's not touched. Hence, for + // processes with small heaps, the mapped arena space needs + // to be commensurate. This is particularly important with + // the race detector, since it significantly amplifies the + // cost of committed memory. We use smaller arenas on Wasm + // because some Wasm programs have very small heap, and + // everything in the Wasm linear memory is charged. heapArenaBytes = 1 << logHeapArenaBytes heapArenaWords = heapArenaBytes / goarch.PtrSize @@ -253,7 +256,7 @@ const ( // logHeapArenaBytes is log_2 of heapArenaBytes. For clarity, // prefer using heapArenaBytes where possible (we need the // constant to compute some other constants). - logHeapArenaBytes = (6+20)*(_64bit*(1-goos.IsWindows)*(1-goarch.IsWasm)*(1-goos.IsIos*goarch.IsArm64)) + (2+20)*(_64bit*goos.IsWindows) + (2+20)*(1-_64bit) + (2+20)*goarch.IsWasm + (2+20)*goos.IsIos*goarch.IsArm64 + logHeapArenaBytes = (6+20)*(_64bit*(1-goos.IsWindows)*(1-goarch.IsWasm)*(1-goos.IsIos*goarch.IsArm64)) + (2+20)*(_64bit*goos.IsWindows) + (2+20)*(1-_64bit) + (9+10)*goarch.IsWasm + (2+20)*goos.IsIos*goarch.IsArm64 // heapArenaBitmapWords is the size of each heap arena's bitmap in uintptrs. heapArenaBitmapWords = heapArenaWords / (8 * goarch.PtrSize) |
