-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathslice_alloc_test.go
More file actions
82 lines (72 loc) · 1.99 KB
/
Copy pathslice_alloc_test.go
File metadata and controls
82 lines (72 loc) · 1.99 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
package jinja
import (
"testing"
)
// measure compares the allocations of two templates rendered with the
// same data. Returns (with - without) — the cost attributable to the
// extra slice expression.
func measure(t *testing.T, withExpr, withoutExpr string, items int) (delta float64) {
t.Helper()
mk := func(src string) *Template {
tmpl, err := Compile(src)
if err != nil {
t.Fatal(err)
}
return tmpl
}
xs := make([]any, items)
for i := range xs {
xs[i] = i
}
data := map[string]any{"items": xs}
with := mk(withExpr)
without := mk(withoutExpr)
for range 5 {
_, _ = with.Render(data)
_, _ = without.Render(data)
}
a := testing.AllocsPerRun(2000, func() {
_, _ = with.Render(data)
})
b := testing.AllocsPerRun(2000, func() {
_, _ = without.Render(data)
})
t.Logf("with=%.2f without=%.2f delta=%.2f", a, b, a-b)
return a - b
}
// TestEvalSliceAllocs_Step1 verifies the step==1 fast path allocates the
// minimum: one []Value backing array + one *List wrapper = 2 allocs.
func TestEvalSliceAllocs_Step1(t *testing.T) {
delta := measure(t,
`{% set tail = items[1:] %}{{ tail|length }}`,
`{{ items|length }}`,
64,
)
if delta > 3.0 {
t.Errorf("step==1 slice should add ≤3 allocs (slice + *List + ?), got delta=%.2f", delta)
}
}
// TestEvalSliceAllocs_StepN verifies the general step path is also at
// minimum cost when the pre-size is exact.
func TestEvalSliceAllocs_StepN(t *testing.T) {
delta := measure(t,
`{% set evens = items[0:64:2] %}{{ evens|length }}`,
`{{ items|length }}`,
64,
)
if delta > 3.0 {
t.Errorf("step==2 slice with pre-size should add ≤3 allocs, got delta=%.2f", delta)
}
}
// TestEvalSliceAllocs_NegStart verifies the start<0 case is still bound
// by pre-sizing (no append growth).
func TestEvalSliceAllocs_NegStart(t *testing.T) {
delta := measure(t,
`{% set tail = items[-10:] %}{{ tail|length }}`,
`{{ items|length }}`,
64,
)
if delta > 3.0 {
t.Errorf("negative start should add ≤3 allocs, got delta=%.2f", delta)
}
}