compiler, runtime: optimize zero-sized allocations

Instead of referring to an unused global, use a constant value. This is
safe even when using `-gc=none` (since no actual memory gets allocated)
which wasn't the case before. It should also reduce binary size by a few
bytes for most programs.
This commit is contained in:
Ayke van Laethem
2026-05-25 18:55:36 +02:00
committed by Ron Evans
parent 221ea6a54c
commit 134de98ba5
18 changed files with 63 additions and 12 deletions
+1 -1
View File
@@ -299,7 +299,7 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
// means that monotonic time in the time package is counted from
// time.Time{}.Sub(1), which should be fine.
locals[inst.localIndex] = literalValue{uint64(0)}
case callFn.name == "runtime.alloc" || callFn.name == "runtime.alloc_noheap":
case callFn.name == "runtime.alloc" || callFn.name == "runtime.alloc_noheap" || callFn.name == "runtime.alloc_zero":
// Allocate heap memory. At compile time, this is instead done
// by creating a global variable.