Skip to Content
marmot

Source: the night bench run, darwin/arm64, main bd3fa968, 2026-11-17. Speed is go time / marmot -release time (median run_ms). Below 1.00 is slower than go.

ProgramSpeedgo msrelease ms
deferloop0.7711201454
sha2560.8882469400
mapstr0.90581647
jsonenc0.91260285
jsondec0.92487526
fmtlog0.98713729
dot0.9852835395
transpose0.9949685008
hello1.005.45.4

Method: Linux host, at.sh sh job, go build and marmot build -release of each program at main 2c7cc9d8. Perf stat (cycles:u, instructions:u) and perf record -e cycles:u (flat). The release binary is profiled from the work directory link output (nopcln.exe, symbols kept). Its code is the same as the stripped binary. Outputs: ~/.cache/marmot/frontend-losers.

deferloop

linux/amd64: go 1.73 s, 12.0 G instructions. Release 5.55 s, 41.3 G instructions (3.2x slower, worse than the 1.3x on darwin).

Profile (release): rt_alloc 47%, main.main 27% (step inlined), runtime.getg 16%, main.step$1 4.6%. Go: main.step 55%, step.func1 20%, deferwrap1 18%.

Causes, from the annotated loop:

  1. V goes to the heap: one rt_alloc(8) per call. V is captured by the deferred closure (by reference: it changes after the defer) and &v is a deferred argument. gc keeps v on the stack.
  2. A _defer record is pushed and popped on every call, with a call to runtime.getg. getg is not inlined: it sets up a frame and reads the TLS offset rt_m_tlsoff. The record is needed only if something can panic while a defer is pending.
  3. The deferred closure is called through its func value (call *slot) on the return path. The callee is known (main.step$1), but it is not inlined.

Variants, release, linux/amd64, main 555e5c5c (perf stat, one run each):

SwitchesTimeInstructions
none5.55 s41.3 G
escape set (XPKG_ESCAPE, ESCAPE_SUMMARY, ESCAPE_EXT)2.26 s18.0 G
MARMOT_DEFERELIDE=13.10 s24.3 G
escape set + MARMOT_DEFERELIDE=10.28 s2.3 G
MARMOT_LIFETIME=all0.29 s2.3 G
go1.73 s12.0 G

The escape set puts v on the stack (no rt_alloc). DEFERELIDE drops the record (no getg, no push and pop) when nothing can panic while a defer is pending. For this, v must be on the stack, so that &v is known not nil. With both, step and its deferred calls inline into the loop. Output is the same in all variants.

Fix: no new code. The escape set is in its gate. DEFERELIDE needs the same audit and gate as APPENDMAKE (off since cf8bd9d7 as a class, not for a found bug). Cost: low. Gain: deferloop from 3.2x slower than go to 6x faster (linux).

Remaining, not needed for this program: runtime.getg is a real call in -release (frame setup and a TLS offset load). Records stay in functions that can panic. Runtime and backend: inline getg.

Linux profiles of the other programs

perf stat, one run, linux/amd64, main 2c7cc9d8 (host under load: times are rough, instruction counts are firm).

Programgo srelease sgo G instrrelease G instrLinux speed
sha25674.6555.927747821.33
mapstr1.801.374.133.651.32
jsonenc0.810.675.587.091.21
jsondec2.092.2412.0812.960.94
fmtlog2.051.5712.5213.951.30
dot11.3017.24137.6142.60.66
transpose10.6411.5214.153.040.92
hello0.0020.001

So sha256, mapstr, jsonenc and fmtlog lose only on darwin/arm64. Dot, transpose and jsondec lose on both.

dot (backend: vectorizer cost)

go runs the scalar loop (two loads, imul, add): 45 G cycles. Release vectorizes the int64 multiply-add with SSE2, which has no 64-bit vector multiply: per two lanes three pmuludq, two psrlq, a psllq and two paddq. Same instruction count, 65 G cycles. On arm64 NEON has no 64-bit vector multiply either. Fix: the backend's vectorizer cost for i64 mul on targets without a native 64-bit vector multiply (or keep such reductions scalar).

transpose (backend, low gain)

Memory bound (column stores, 2048 x 2048 float64). Release runs 3.0 G instructions against 14.1 G for go, at almost the same cycles. The gap is the cache behavior of the store pattern: 8% on Linux, 1% on darwin. Low priority.

sha256, mapstr (darwin only, runtime)

Linux: release beats go. sha256 spends 99.6% in the runtime's sha256_rounds/sha256_sched2 (C, AVX2 on amd64). On arm64, go uses the SHA2 instructions. The marmot runtime's arm64 block function should too. mapstr: lookup and hashing (rt_hash_bytes). On arm64 go hashes with AES instructions. A darwin profile must confirm this.

fmtlog (frontend, darwin gap 2%)

Release: rt_alloc 11.5%, runtime.getitab 10.3%. go has no getitab in the profile. Since Go 1.22, gc emits a per-site cache for assertions to non-empty interfaces. handleMethods in fmt asserts every argument to Formatter and Stringer. Marmot calls assertE2I, then getitab, which searches the global itab table every time. Candidate: a per-site assertion cache in the code generator.

Allocation counts (load-independent)

go: runtime.MemStats.Mallocs of a copy of the program. Marmot: calls to rt_alloc and rt_alloc_nozero under callgrind, release, main 555e5c5c.

Programgorelease+ escape set+ DEFERELIDE, APPENDMAKE
jsondec4.41 M10.06 M9.91 M9.91 M
jsonenc2.12 M12.94 M10.58 M10.58 M
fmtlog9.00 M18.00 M13.50 M13.50 M

Top allocating callers with the escape set (callgrind):

  • jsonenc: runtime.rawbyteslice 4.10 M, from []byte(s) conversions that go keeps on the stack. Also reflect.copyVal 2.0 M, makeStructArshaler closure 1.8 M, reflect.New 1.0 M, reflect.Value.MapIndex 1.0 M and MapRange 0.5 M.
  • jsondec: rawstring 2.77 M, reflect.Value.SetMapIndex 2.0 M, growslice 1.5 M, reflect.New 1.14 M, MapIndex 1.04 M, makemap 0.52 M.
  • fmtlog: per line 9 allocations against go's 6. The allocation report names them: 5 boxings (as go), the Sprintf result (as go), and two that go avoids: []byte(msg) and []byte{'\n'} passed to h.Write. H is a hash.Hash64 interface whose dynamic type is *fnv.sum64a (fnv.New64a returns it), so gc devirtualizes the call and sees that Write keeps nothing. Go also converts []byte(msg) without a copy, because the slice is not written and does not escape.

Ranking (gain per cost)

#FixOwnerGainCost
1Gate and flip MARMOT_DEFERELIDE (with the escape set)frontenddeferloop 3.2x slower than go to 6x faster (linux), records gone in every frame that cannot paniclow: audit done, one bug fixed (append of zero-size elements), gate
2Flip the escape set (gate running)frontendjsonenc -18% allocations, fmtlog -25%, deferloop with 1in progress
3Devirtualize interface calls whose receiver has one known dynamic type (returned by a function that always returns one concrete type), before escape analysisfrontendfmtlog -2 allocations per line, any code that builds a hash, writer or reader through a constructormedium
4[]byte(s) without a copy when the slice is never written and does not escape (gc's rule)frontendjsonenc up to 4.1 M allocations, fmtlog 1 per linemedium: needs a write analysis of the slice
5Per-site cache for assertions to non-empty interfacesfrontendfmtlog getitab 10% of cycles, fmt-heavy codemedium
6reflect allocations (copyVal, New, MapIndex, SetMapIndex)frontend/rtcore (lib/src/reflect)json: 4 to 5 M allocationsmedium to high
7Vectorizer cost of i64 multiply without a vector multiply (dot)backenddot 1.5x on linux, 2% darwinlow to medium
8sha256 and map hash with arm64 instructionsrtcoresha256 12%, mapstr 10% on darwinmedium
9Inline getg (TLS read) in -releasertcore/backendevery frame that keeps a recordlow
10transpose store patternbackend8% linux, 1% darwinunknown

Top fix: 1 needs no new design (existing code, audit and gate). The top fix that needs a design is 3 together with 4 (one design: both feed the escape analysis).

Progress

  • APPENDMAKE: audited (no behavior bug, adjacency rule added, 555e5c5c), gate running. Wordfreq release max RSS 535 -> 400 MB (go 401).
  • DEFERELIDE: audit found one bug (append of zero-size elements may panic, fixed 689713c8), gate running with the escape set.
  • ROCONV (design Part A): landed opt-in 8288e8ca. Jsonenc release allocations 10.58 M -> 6.48 M (go 2.12 M). In separate compilation the one-level escape tags limit it. Gate running.
  • DEVIRT1 (design Part B): landed opt-in 2338d372. Fmtlog release allocations 13.50 M -> 10.50 M (go 9.00 M), with the escape set and ROCONV. Gate running. Direct-call code is a follow-up.

Finding: go 1.27.4 drops a side effect in append(x, make([]T, n)...)

When both operands call inlined functions that append to the same global, gc keeps only one of the appends. -gcflags=-l or -N gives the spec result. Marmot gives the spec result.

package main

var trace []string

func src(s []int) []int { trace = append(trace, "src"); return s }
func size(n int) int    { trace = append(trace, "size"); return n }

func main() {
	_ = append(src([]int{9}), make([]int, size(2))...)
	println(len(trace), trace[0]) // the spec: 2 src (both calls ran, src first)
	trace = nil
	_ = append(src([]int{9}), []int{size(2)}...)
	println(len(trace)) // control without make: 2
}

go1.27.4 linux/amd64: prints "1 src" then "2". With -gcflags=-l: "2 src" then "2". The make form is gc's extendslice rewrite. tests/run/opt/append_make marks src and size noinline so the test checks the spec order.

On this page

Was this page helpful?

Report an issueOpen issues