# Release speed losers (frontend) (/docs/experiments/frontend/losers)

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.

| Program | Speed | go ms | release ms |
| --- | --- | --- | --- |
| deferloop | 0.77 | 1120 | 1454 |
| sha256 | 0.88 | 8246 | 9400 |
| mapstr | 0.90 | 581 | 647 |
| jsonenc | 0.91 | 260 | 285 |
| jsondec | 0.92 | 487 | 526 |
| fmtlog | 0.98 | 713 | 729 |
| dot | 0.98 | 5283 | 5395 |
| transpose | 0.99 | 4968 | 5008 |
| hello | 1.00 | 5.4 | 5.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 [#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):

| Switches | Time | Instructions |
| --- | --- | --- |
| none | 5.55 s | 41.3 G |
| escape set (XPKG_ESCAPE, ESCAPE_SUMMARY, ESCAPE_EXT) | 2.26 s | 18.0 G |
| MARMOT_DEFERELIDE=1 | 3.10 s | 24.3 G |
| escape set + MARMOT_DEFERELIDE=1 | 0.28 s | 2.3 G |
| MARMOT_LIFETIME=all | 0.29 s | 2.3 G |
| go | 1.73 s | 12.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 [#linux-profiles-of-the-other-programs]

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

| Program | go s | release s | go G instr | release G instr | Linux speed |
| --- | --- | --- | --- | --- | --- |
| sha256 | 74.65 | 55.92 | 774 | 782 | 1.33 |
| mapstr | 1.80 | 1.37 | 4.13 | 3.65 | 1.32 |
| jsonenc | 0.81 | 0.67 | 5.58 | 7.09 | 1.21 |
| jsondec | 2.09 | 2.24 | 12.08 | 12.96 | 0.94 |
| fmtlog | 2.05 | 1.57 | 12.52 | 13.95 | 1.30 |
| dot | 11.30 | 17.24 | 137.6 | 142.6 | 0.66 |
| transpose | 10.64 | 11.52 | 14.15 | 3.04 | 0.92 |
| hello | 0.002 | 0.001 | | | |

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

### dot (backend: vectorizer cost) [#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) [#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) [#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%) [#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) [#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.

| Program | go | release | + escape set | + DEFERELIDE, APPENDMAKE |
| --- | --- | --- | --- | --- |
| jsondec | 4.41 M | 10.06 M | 9.91 M | 9.91 M |
| jsonenc | 2.12 M | 12.94 M | 10.58 M | 10.58 M |
| fmtlog | 9.00 M | 18.00 M | 13.50 M | 13.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) [#ranking-gain-per-cost]

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

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 [#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)...) [#finding-go-1274-drops-a-side-effect-in-appendx-maket-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.

```go
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.