wasm (asyncify): every goroutine and every JS callback allocates a new stack that is rarely freed; reusing finished stacks cut allocation per request 45x
What happens
With the asyncify scheduler (wasm with syscall/js), every goroutine gets a stack of -stack-size from runtime_alloc, and so does every call from JavaScript into Go (resume in runtime_wasm_js_scheduler.go starts a goroutine for each event). When the goroutine finishes, the stack is cleared and left to the collector. task_exit.go and scheduler_cooperative.go both carry a TODO about this ("explicitly free the stack after switching back to the scheduler", "we could cache the allocated stack").
For a program that talks to JavaScript a lot, stacks are then nearly everything it allocates, and in our measurements the collector got little of it back.
Measurements
An HTTP API on Cloudflare Workers (Huma on syumai/workers-go, TinyGo 0.42.0, -target wasm -no-debug -opt=z), where one Go program serves many requests. runtime.ReadMemStats after each request, under workerd on macOS:
TotalAlloc per hello request |
per request with a D1 write | |
|---|---|---|
-stack-size=256kb |
3.4 MB (about 13 stacks) | 8 MB (about 30 stacks) |
-stack-size=128kb |
1.7 MB | 4 MB |
-stack-size=128kb, stacks reused (patch below) |
38 KB | about 100 KB |
Without reuse, HeapInuse only went up: with TinyGo as it is, 9 hello requests in one program left 23 MB in use although NumGC was 14. On Cloudflare the collections in that program cost 65, 148, 175 and 293 ms of CPU, and it then ran out of memory after about 40 requests.
With reuse, CPU time per request on Cloudflare (Workers Logs, median of 20) went from 3 ms to 1 ms for the hello and from 10 to 13 ms to 4 ms for the write, and one program serves about 80 requests in an 8 MB heap without a collection.
The patch we run with
Against 0.42.0, src/internal/task/task_asyncify.go. A finished goroutine's stack, which Resume has just cleared, goes on a list threaded through its first word, and initialize takes one from the list when the size matches:
@@ func (s *state) initialize(fn uintptr, args unsafe.Pointer, stackSize uintptr) {
- // Create a stack.
- stack := runtime_alloc(stackSize, nil)
+ // Take the stack of a finished goroutine (Resume keeps them), or create one.
+ stack := freeStack
+ if stack != nil && freeStackSize == stackSize {
+ freeStack = *(*unsafe.Pointer)(stack)
+ *(*unsafe.Pointer)(stack) = nil
+ } else {
+ stack = runtime_alloc(stackSize, nil)
+ }
@@
+// freeStack is a list of the stacks of finished goroutines, all of freeStackSize bytes.
+var (
+ freeStack unsafe.Pointer
+ freeStackSize uintptr
+)
@@ func (t *Task) Resume() {
t.state.finishing = false
t.clearStack()
t.state.args = nil
+ // Keep the cleared stack for the next goroutine: a list through the first word of each.
+ base := unsafe.Pointer(t.state.canaryPtr)
+ if size := uintptr(t.state.top) - uintptr(base); freeStack == nil || size == freeStackSize {
+ *(*unsafe.Pointer)(base) = freeStack
+ freeStack, freeStackSize = base, size
+ t.state.stackState = stackState{}
+ }What it has been through: our test suite under workerd, 700 requests at 8 at a time with cancelled streams locally, and on Cloudflare 1,000 requests, long-lived SSE and WebSocket streams and a redeploy. No failures.
What I am not sure of, and would like your view on:
- It only reuses on the path where
finishingis set, whichexitGoroutineandgoexitonly do whenfinalizerIdleGC != nil. A program with no finalizers gets no reuse. Should the list be independent of that? - The list never shrinks. It holds as many stacks as goroutines were ever alive at once.
- One size per list. Stacks of another size are left to the collector as today.
- Why the collector does not get the cleared stacks back today, I did not find out. A 128 KB block in a small heap is an easy target for a conservative scan, but that is a guess.
Related: #5800 (the finalizer-pressure collection at every scheduler idle), found in the same program.
If this direction is acceptable I can open a PR.
Source: tinygo-org/tinygo