-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphase2c_int2_closedloop_zeroalloc_test.go
More file actions
113 lines (101 loc) · 3.65 KB
/
Copy pathphase2c_int2_closedloop_zeroalloc_test.go
File metadata and controls
113 lines (101 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package g729
import "testing"
// TestNoAllocationInClosedloopStep pins I4 (zero-allocation contract)
// on the package-internal closedloopStep entry point introduced in
// Phase 2c. Construct a fresh Encoder, prime it with one lpcStep +
// openloopStep call (so e.aHatSF{1,2}, e.tOp, e.lpResidualMemQ,
// e.swMemErr and e.oldExc carry production-shaped state), then call
// closedloopStep(0) followed by closedloopStep(1) repeatedly and
// assert testing.AllocsPerRun returns exactly 0 for each.
//
// The 183-sample on-stack scratch buffer (excSearch =
// [PitchMaxInt+SubframeLen]int16) introduced by the OQ-K<40 LP-residual
// extension refactor must not escape to the heap; the closedloop
// SearchInteger / RefineFraction / AdaptiveVector / Interpolate3
// primitives are individually pinned to zero-alloc by their unit
// tests, so the only remaining heap risk is the encoder driver itself.
//
// Phase 2c INT-2 gate. See
// docs/superpowers/plans/2026-05-09-phase2c-closed-loop-pitch-plan.md
// §6 Task INT-2 Step 1 + §7 INT-2 alloc/bench table.
func TestNoAllocationInClosedloopStep(t *testing.T) {
enc := NewEncoder()
var pcm [FrameSamples]int16
for i := range pcm {
pcm[i] = int16(((i * 137) & 0x3FFF) - 0x2000)
}
// Warm up: prime LPC + open-loop state so closedloopStep sees
// production-shaped inputs (aHatSF{1,2} populated, tOp valid,
// lpResidualMemQ and swMemErr non-degenerate).
if _, err := enc.lpcStep(pcm[:]); err != nil {
t.Fatalf("lpcStep warmup returned error: %v", err)
}
_ = enc.openloopStep()
_, _ = enc.closedloopStep(0)
_, _ = enc.closedloopStep(1)
// Subframe 0 in isolation.
allocsSF0 := testing.AllocsPerRun(64, func() {
_, _ = enc.closedloopStep(0)
})
if allocsSF0 != 0 {
t.Fatalf("Encoder.closedloopStep(0) allocated %.2f times per call; want 0 (I4)", allocsSF0)
}
// Subframe 1 in isolation.
allocsSF1 := testing.AllocsPerRun(64, func() {
_, _ = enc.closedloopStep(1)
})
if allocsSF1 != 0 {
t.Fatalf("Encoder.closedloopStep(1) allocated %.2f times per call; want 0 (I4)", allocsSF1)
}
}
// TestNoAllocationInLPCStepPlusOpenloopPlusClosedloop pins I4 on the
// end-to-end lpcStep + openloopStep + 2× closedloopStep composition
// for one frame — the full Phase 2c production hot path the encoder
// executes every 10 ms.
//
// Phase 2c INT-2 gate per plan §6 Task INT-2 Step 1.
func TestNoAllocationInLPCStepPlusOpenloopPlusClosedloop(t *testing.T) {
enc := NewEncoder()
var pcm [FrameSamples]int16
for i := range pcm {
pcm[i] = int16(((i * 137) & 0x3FFF) - 0x2000)
}
// Warm up the full chain.
if _, err := enc.lpcStep(pcm[:]); err != nil {
t.Fatalf("lpcStep warmup returned error: %v", err)
}
_ = enc.openloopStep()
_, _ = enc.closedloopStep(0)
_, _ = enc.closedloopStep(1)
allocs := testing.AllocsPerRun(64, func() {
if _, err := enc.lpcStep(pcm[:]); err != nil {
t.Fatalf("lpcStep returned error: %v", err)
}
_ = enc.openloopStep()
_, _ = enc.closedloopStep(0)
_, _ = enc.closedloopStep(1)
})
if allocs != 0 {
t.Fatalf("lpcStep+openloopStep+2×closedloopStep allocated %.2f times per call; want 0 (I4)", allocs)
}
}
// BenchmarkClosedloopStep captures Phase 2c INT-2 bench numbers for
// the closure report. Reports per-subframe ns/op + B/op + allocs/op
// for the production hot path.
func BenchmarkClosedloopStep(b *testing.B) {
enc := NewEncoder()
var pcm [FrameSamples]int16
for i := range pcm {
pcm[i] = int16(((i * 137) & 0x3FFF) - 0x2000)
}
if _, err := enc.lpcStep(pcm[:]); err != nil {
b.Fatalf("lpcStep warmup returned error: %v", err)
}
_ = enc.openloopStep()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = enc.closedloopStep(0)
_, _ = enc.closedloopStep(1)
}
}