Review: a stack buffer for append
Reviewer: verify (lifetime soundness, rule 11). Note under review: frontend-design-appendstack.md.
1. Capacity: rule 4 keeps marmot's capacities, which already differ from Go
Measured with the host's Go 1.27.4 and marmot at 14d3417b97 (release and dev), on non-escaping appends from nil:
| Function | Go 1.27 cap | marmot cap today |
|---|---|---|
| one append of an int | 4 | 1 |
| three appends of an int in a loop | 3 | 4 |
| one append of a byte | 32 | 8 |
Go already uses a 32-byte stack buffer here, and its loop result looks like a heap move that keeps cap equal to len. Both behaviors are legal, because the spec only guarantees cap >= len. But a program that prints cap after a small local append already prints different output under marmot than under Go. Rule 4 neither fixes this nor makes it worse. Decide which goal the switch has: keep marmot's capacities (rule 4 as written), or match Go's. Matching Go is an output change and needs its own approval and gate.
2. The loop condition alone is not enough
"The operand is nil or this site's result on every incoming path" also admits a loop that resets the slice. It resets it each iteration while an older slice is still live:
var s, prev []int
for i := 0; i < 2; i++ {
prev = s
s = nil
s = append(s, i) // the operand is nil on every path, so the site qualifies
}
println(prev[0]) // Go prints 0; with one buffer per site this prints 1The note relies on the escape analysis treating prev, a value that survives into the next iteration, as escaping. That is the right rule, but it must hold for every derived value: phis, reslices, element addresses, and values copied into locals or struct fields. It must also use the CFG's cycles, not syntactic for loops (goto loops). Please add this program and a goto variant as tests that must keep heap allocation.
3. Other append sites write into the buffer
The buffer can hold more than len, for example 1 byte with heap cap 8 under rule 4. Then a later append at a different site, t := append(s, y), writes into the frame buffer without calling growslice. t, and anything derived from t, must be in the derived set and must not escape. Treat unsafe.SliceData, unsafe.Pointer and uintptr conversions of any derived value as an escape, and closures that capture the slice (including range-over-func bodies) as well.
4. Runtime assumptions to confirm with the runtime
- growslice with an old backing array on the stack must read only the pointer, the length, the capacity and the element type. It must not look up a heap span for the old pointer, its size class or its noscan bit, and it must not free it.
- Pointer stores into the frame buffer: the write barrier, or barrier elision, must handle a stack destination the same way stackMakeSlice's buffers do. Use the same lowering.
- Stack copying: if a goroutine stack can move, the slice header's pointer into the buffer must be adjusted. This is the same requirement stackMakeSlice already has, so state that the buffer goes through the same path.
- Dev builds (tier 0): apply the rule identically or not at all, so dev and release do not give different capacities.
5. What to measure
- The allocation report before and after: heap allocations removed per bench program and on tuios. Also the number of sites that qualify and the frame bytes added.
- Deep recursion with a qualifying site per frame: peak RSS and stack growth events, because 32 bytes per site per frame adds up.
- MARMOT_POISON=1 with the frame buffer filled on exit, under the conformance matrix and a fuzz round with the switch forced on.
- verify can add adversarial programs for this rule to verify/testdata/progs (the alias loop, the goto loop, an other-site append within capacity, a deferred read, range-over-func). These run under dev, release and the go dialect, and must match Go.