#5800·tinygo

wasm: finalizer-pressure GC at every scheduler idle makes syscall/js programs 4-6x slower (finalizerGCThreshold = 32)

Author: joeblew999Created Oct 1, 2026Updated Oct 1, 2026

On -target wasm with the asyncify scheduler, a program that uses syscall/js heavily runs a full garbage collection at almost every idle point. On Cloudflare Workers that made each request 4 to 6 times more expensive than it needs to be.

TinyGo 0.42.0, -target wasm -no-debug, default (precise) GC, running under Cloudflare's workerd through syumai/workers-go v0.36.0, which creates a fresh instance per request.

What happens

runtime/gc_finalizer.go has

go
const finalizerGCThreshold = 32

and the cooperative scheduler calls finalizerIdleGC (finalizerPressureGC) whenever it has nothing runnable. So once 32 finalizers have been registered since the last collection, the next idle point runs runGC().

In a syscall/js program every js.Value that holds a reference registers a finalizer. An HTTP handler on Workers creates hundreds per request (the request, each header, each promise, each callback), and it goes idle at every await. One request therefore runs many full collections, each marking the whole heap and every goroutine stack.

Measured

A small API (Huma on net/http, a D1 query through database/sql), 2.4 MB of Wasm. Time inside the Worker for one request, local workerd, median of 4:

Build 404 trivial GET D1 read of 20 rows D1 write + a fetch
0.42.0 as it is 8 ms 8 ms 15 ms 37 to 44 ms
finalizerGCThreshold = 0 8 ms 8 ms 9 ms
finalizerGCThreshold = 0 and --initial-memory=8MB 1 ms 1 to 2 ms 3 ms 5 ms
-gc=leaking, for comparison 1 to 3 ms 1 to 4 ms 3 to 5 ms

On Cloudflare itself (CPU time from Workers analytics, median), the same two changes took the trivial GET from 55 ms to about 13 ms, the D1 read from 71 ms to about 13 ms, and a 404 from 38 ms to 9 ms.

The starting heap matters as well: with the default of a few pages, the allocator collects each time it has to grow, which is the other half of the cost. But a larger starting heap alone does not help (an 8 MB heap with the threshold at 32 measured 11 to 19 ms), because the idle collections then mark a larger heap.

Why the threshold of 32 hurts here

The pressure heuristic assumes finalizer registrations mean external memory worth reclaiming promptly. For js.Value references the external objects are small, and in a per-request instance they all die with the instance a few milliseconds later. The count reaches 32 many times within one request.

Proposals, any of which would fix it

  1. Make the threshold tunable without patching the runtime (a -ldflags-settable variable, a build tag, or runtime/debug).
  2. Make the trigger proportional to work done, e.g. only run the pressure collection when the heap has grown by some fraction since the last collection, or rate-limit it (at most one per N ms of scheduler time).
  3. Do not count syscall/js reference finalizers toward this pressure, or weigh them far lower.

I'm happy to send a PR for whichever you prefer. For now I build against a copy of src/runtime with the constant set to 0: https://github.com/joeblew999/orpc-api/blob/main/dev/wasm.go

Related: #5798 (timers on Cloudflare) and #5621 (the sleepTicks chain), both in the same scheduler-idle path.