|
| 1 | +// SPDX-License-Identifier: EUPL-1.2 |
| 2 | + |
| 3 | +// Allocation regression gates. Unlike a Benchmark (which only prints a |
| 4 | +// number nobody compares), each case here asserts a hard ceiling on |
| 5 | +// allocations-per-call via testing.AllocsPerRun — so the suite FAILS if a |
| 6 | +// hot primitive starts allocating where it didn't before. ns/op is |
| 7 | +// deliberately not gated (machine-dependent); allocs are stable. |
| 8 | +// |
| 9 | +// Ceilings are the measured floor as of this commit: |
| 10 | +// 0 — pure / zero-copy primitive (no heap touch) |
| 11 | +// 1 — one unavoidable alloc: Result{Value:<non-pointer>} boxes the value |
| 12 | +// into `any` (string/int → 1 alloc; pointer values box for free) |
| 13 | +// |
| 14 | +// Tightening a ceiling that's been beaten is welcome; raising one is a |
| 15 | +// regression that needs justifying in review. |
| 16 | +// |
| 17 | +// Run: go test -run 'TestAllocs_' . |
| 18 | + |
| 19 | +package core_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + |
| 24 | + . "dappco.re/go" |
| 25 | +) |
| 26 | + |
| 27 | +// gate sinks — keep the measured call from being elided as dead code. |
| 28 | +var ( |
| 29 | + gateInt int |
| 30 | + gateBool bool |
| 31 | + gateStr string |
| 32 | + gateBytes []byte |
| 33 | + gateResult Result |
| 34 | + gateAny any |
| 35 | + gateDiags []LSPDiagnostic |
| 36 | +) |
| 37 | + |
| 38 | +func TestAllocs_HotPrimitives(t *T) { |
| 39 | + ptr := &struct{ n int }{} |
| 40 | + bs := []byte("homelab") |
| 41 | + var ai32 AtomicInt32 |
| 42 | + var ab AtomicBool |
| 43 | + var au64 AtomicUint64 |
| 44 | + cases := []struct { |
| 45 | + name string |
| 46 | + ceiling int |
| 47 | + fn func() |
| 48 | + }{ |
| 49 | + // math / ordering — pure, must stay 0 |
| 50 | + {"Compare", 0, func() { gateInt = Compare(3, 7) }}, |
| 51 | + {"Min", 0, func() { gateInt = Min(3, 7) }}, |
| 52 | + {"Max", 0, func() { gateInt = Max(3, 7) }}, |
| 53 | + {"Abs", 0, func() { gateInt = Abs(-42) }}, |
| 54 | + {"Clamp", 0, func() { gateInt = Clamp(15, 0, 10) }}, |
| 55 | + {"Sign", 0, func() { gateInt = Sign(-3) }}, |
| 56 | + |
| 57 | + // string predicates / index — pure, must stay 0 |
| 58 | + {"Contains", 0, func() { gateBool = Contains("agent ready", "ready") }}, |
| 59 | + {"HasPrefix", 0, func() { gateBool = HasPrefix("agent.go", "agent") }}, |
| 60 | + {"HasSuffix", 0, func() { gateBool = HasSuffix("agent.go", ".go") }}, |
| 61 | + {"EqualFold", 0, func() { gateBool = EqualFold("Bearer", "bearer") }}, |
| 62 | + {"Index", 0, func() { gateInt = Index("agent", "e") }}, |
| 63 | + {"Count", 0, func() { gateInt = Count("banana", "a") }}, |
| 64 | + {"Trim", 0, func() { gateStr = Trim(" x ") }}, |
| 65 | + |
| 66 | + // slice search — pure, must stay 0 |
| 67 | + {"SliceContains", 0, func() { gateBool = SliceContains([]int{1, 2, 3}, 2) }}, |
| 68 | + {"SliceIndex", 0, func() { gateInt = SliceIndex([]int{1, 2, 3}, 2) }}, |
| 69 | + |
| 70 | + // unsafe zero-copy conversions — must stay 0 |
| 71 | + {"AsBytes", 0, func() { gateBytes = AsBytes("homelab") }}, |
| 72 | + {"AsString", 0, func() { gateStr = AsString(bs) }}, |
| 73 | + |
| 74 | + // Result constructors — pointer value boxes for free (0); a |
| 75 | + // non-pointer value pays the one inherent Result-boxing alloc. |
| 76 | + {"Ok_Pointer", 0, func() { gateResult = Ok(ptr) }}, |
| 77 | + {"Fail", 0, func() { gateResult = Fail(AnError) }}, |
| 78 | + {"Ok_String", 1, func() { gateResult = Ok("ready") }}, |
| 79 | + {"ResultOf_String", 1, func() { gateResult = ResultOf("ready", nil) }}, |
| 80 | + |
| 81 | + // Result accessors — pure reads, must stay 0 |
| 82 | + {"Result_Or", 0, func() { gateAny = (Result{OK: false}).Or("fallback") }}, |
| 83 | + |
| 84 | + // atomics — lock-free, must stay 0 |
| 85 | + {"AtomicInt32_Load", 0, func() { gateInt = int(ai32.Load()) }}, |
| 86 | + {"AtomicInt32_Store", 0, func() { ai32.Store(7) }}, |
| 87 | + {"AtomicInt32_Add", 0, func() { gateInt = int(ai32.Add(1)) }}, |
| 88 | + {"AtomicBool_Load", 0, func() { gateBool = ab.Load() }}, |
| 89 | + {"AtomicBool_Store", 0, func() { ab.Store(true) }}, |
| 90 | + {"AtomicUint64_Add", 0, func() { gateInt = int(au64.Add(1)) }}, |
| 91 | + |
| 92 | + // more math — pure, must stay 0 |
| 93 | + {"Pow", 0, func() { gateInt = int(Pow(2, 8)) }}, |
| 94 | + {"Floor", 0, func() { gateInt = int(Floor(3.7)) }}, |
| 95 | + {"Ceil", 0, func() { gateInt = int(Ceil(3.1)) }}, |
| 96 | + {"Round", 0, func() { gateInt = int(Round(3.5)) }}, |
| 97 | + |
| 98 | + // slice — Clone copies (1 alloc for the backing array); Reverse is in-place (0) |
| 99 | + {"SliceClone", 1, func() { gateBytes = SliceClone(bs) }}, |
| 100 | + } |
| 101 | + for _, c := range cases { |
| 102 | + avg := int(testing.AllocsPerRun(1000, c.fn)) |
| 103 | + AssertLessOrEqual(t, avg, c.ceiling, c.name) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// TestAllocs_LSPComputeDiagnostics locks the regex-hoist win (c1cf230): |
| 108 | +// with per-call regexp.Compile it allocated ~186/call; compiling the |
| 109 | +// naming-diagnostic patterns once dropped it to ~47. A regression to |
| 110 | +// per-call compilation jumps back over 100, so the ceiling catches it. |
| 111 | +func TestAllocs_LSPComputeDiagnostics(t *T) { |
| 112 | + uri := "file:///agent/worker.go" |
| 113 | + content := []byte("package worker\n\nfunc process(in string) (string, error) {\n\treturn in, nil\n}\n") |
| 114 | + LSPComputeDiagnostics(uri, content) // warm the per-dir cache → measure steady state |
| 115 | + avg := int(testing.AllocsPerRun(200, func() { |
| 116 | + gateDiags = LSPComputeDiagnostics(uri, content) |
| 117 | + })) |
| 118 | + AssertLessOrEqual(t, avg, 70, "LSPComputeDiagnostics") |
| 119 | +} |
| 120 | + |
| 121 | +var gateStrSlice []string |
| 122 | + |
| 123 | +// TestAllocs_FilterArgs locks the pre-size win (1f59e31): was an unpresized |
| 124 | +// 'var clean []string' + append (geometric regrow), now the single presized |
| 125 | +// result allocation. (Typed sink — assigning to `any` would box and add a |
| 126 | +// spurious alloc.) |
| 127 | +func TestAllocs_FilterArgs(t *T) { |
| 128 | + args := []string{"deploy", "--target", "homelab", "-v"} |
| 129 | + fa := int(testing.AllocsPerRun(1000, func() { gateStrSlice = FilterArgs(args) })) |
| 130 | + AssertLessOrEqual(t, fa, 1, "FilterArgs") |
| 131 | +} |
0 commit comments