Design: a stack buffer for append
Status: proposal, revised after the runtime and verify critiques (16 November), not implemented. Reviews: runtime (allocation, stack scanning) and verify (lifetime soundness, rule 11).
Goal
Remove the first heap allocations of short local slices built by append, as Go 1.26 does. Many library paths build a slice of a few elements per item (fmt argument lists, token lists, fields). They drop it at the end of the function.
Rule
The rule applies to an append site with two properties. On every incoming path, its slice operand is the nil slice or the result of this same site. In a loop, that is a phi that starts at nil. Its results do not escape the function:
- The function gets one zeroed 32-byte frame buffer for the site, aligned to the element.
- The operand's capacity can be 0 and the new length times the element size can fit in 32 bytes. Then the site uses the buffer as the backing store instead of calling growslice.
- Every other growth calls growslice as now. growslice copies the old elements, also when the old backing store is the frame buffer, and allocates on the heap.
- For observable results, the site keeps the capacity sequence that append would give on the heap. The cap after the first append is what growslice returns for that length and element size. The buffer is used only when that capacity also fits in 32 bytes. Otherwise the heap path runs. So cap(s) is identical to the default build.
"Do not escape" is the existing SSA escape analysis (escapes(v, ..., inLoop)), as for stackMakeSlice. It applies to every value derived from the site's results: phis, slices of it and element addresses. In a loop, the analysis already treats a value that survives into the next iteration as escaping. So a buffer is never reused while an older slice of it is live.
Not in this version: Go 1.26's move to the heap at escape points (runtime.move2heap). Sites whose results escape keep the heap path. A second step can add the move once the allocation report shows how many sites escape only through a return or one store.
Revisions from the critiques
- growslice may take a frame buffer as its old store because it only copies from it. Rule: any later in-place growth or free of an old store must first test that the old store is in the heap arena (runtime).
- Stack size: native goroutine stacks do not grow, and wasm stacks are 64 KiB (16 KiB in the playground). Version 1 is off on wasm and in functions in a recursive cycle of the call graph. It gives a function at most 64 bytes of such buffers (two sites).
- Zeroing: the buffer is zeroed on the path that takes it (capacity 0), not at function entry.
- Derived values: every value derived from a qualifying site counts. That is phis, reslices, element addresses, copies into locals and fields, and results of other append sites that grow within the spare capacity. unsafe.SliceData, unsafe.Pointer and uintptr conversions and closure captures, range-over-func bodies included, are escapes. Loops are cycles of the CFG, goto loops included.
- Capacities: marmot's capacities after small local appends already differ from Go 1.27 (one int append: Go 4, marmot 1, three appends in a loop: Go 3, marmot 4, one byte: Go 32, marmot 8). Version 1 keeps marmot's capacities, so it changes no output. Matching Go is a separate decision (an output change with its own gate).
- Dev and release: the rule is applied in codegen, so tier0 and LLVM builds get the same code and the same capacities.
- Tests: verify adds adversarial programs, which must match Go in dev, release and the go dialect. They are the alias loop prev = s, s = nil, s = append(s, i), and a goto loop. Others are an other-site append within capacity, a deferred read and range-over-func.
Exclusions (same as stackMakeSlice)
- Functions with a frame region (f.region) and functions with in-place append reuse (reuseAppend). Reuse frees an old backing array on growth, so it must never see a frame buffer.
- Element types larger than 32 bytes, zero-size element types.
- Goroutine and defer closures that capture the slice: the capture is an escape.
GC
The frame buffer can hold pointers when the element type has them. Stacks are scanned conservatively today, so the buffer is covered. With precise stack maps (MARMOT_GCPTR=1), the buffer must be in the frame's map as a pointer-holding slot. The first version is off when MARMOT_GCPTR=1.
Checks before a default flip (rule 11)
- The rule above written in docs/frontend-passes.md.
- The conformance matrix and a fuzz round with the switch forced on. MARMOT_POISON=1 tests fill a frame buffer on function exit, so a use after the frame is caught.
- verify: the rule re-checked, the checker run on compiler output.
- perfcmp in dev, O2 and release, exit 0. The allocation report before and after on the bench programs and tuios (count of heap allocations removed).
Expected effect
One heap allocation fewer per short local slice. Programs: fmt-heavy and parse-heavy class B rows. Frame size grows by 32 bytes per site that qualifies. The allocation report gives the site count.