I optimized libghostty’s WebAssembly runtime memory usage by replacing Zig’s standard memory pool with a pool designed for Wasm. This work builds on my broader attention to terminal memory layout and extends my webassembly terminal support work.
I replaced std.heap.MemoryPool with a custom Wasm-specific pool that lowered libghostty’s required terminal memory by 75%.
The custom pool reduced required terminal memory without any measurable throughput impact.
Zig’s std.heap.MemoryPool grows by 1.5x whenever it has no free space.
On Wasm, the general-purpose backing allocator is the BrkAllocator, which grows by power-of-two for big allocation slots; that was the behavior we were hitting.
Unlike native virtual-memory growth, every Wasm linear-memory growth is immediately measured, zeroed, and resident.
The standard pool’s growth behavior caused really big linear-memory growth in WebAssembly and saved absolutely nothing on future IO.
My custom pool grows the linear memory by exactly 1 item size when it needs it.
The custom pool maintains a module-global free list so same-item pools are shared globally.
Sharing same-item pools through the module-global free list assumes a single-threaded Wasm module.
I described the replacement pool as “~20 lines of effective code.”
I shared the pull request at https://t.co/1Uh2yPk9fn.
Related: webassembly terminal support, handles and memory pools, terminal memory layout