v0.6.1 aa12db3

prepare_alloc_test.go RawHistoryFolder

1package render
2
3import (
4	"testing"
5
6	"github.com/bnema/nefergui/internal/css"
7	"github.com/bnema/nefergui/internal/layout"
8)
9
10func rectCommands(n int) []layout.Command {
11	cmds := make([]layout.Command, n)
12	for i := range cmds {
13		cmds[i] = layout.Command{Op: "rect", Rect: layout.Rect{X: 1, Y: float64(i), W: 10, H: 1}, Color: css.Color{A: 1}, Opacity: 1}
14	}
15	return cmds
16}
17
18func TestQuadCapacityIsSufficientAndExactWithoutCulling(t *testing.T) {
19	glyphs := glyphCommand(t, "office")
20	n := 0
21	for _, r := range glyphs.Runs {
22		n += len(r.Glyphs)
23	}
24	cmds := []layout.Command{{Op: "clip-push", Rect: layout.Rect{W: 200, H: 100}}, rectCommands(1)[0], glyphs, {Op: "clip-pop"}}
25	if got := quadCapacity(cmds); got != n+1 {
26		t.Fatalf("capacity %d, want %d", got, n+1)
27	}
28	p, err := NewPreparer(256, 2)
29	if err != nil {
30		t.Fatal(err)
31	}
32	f, err := p.Prepare(cmds, 1, 200, 100)
33	if err != nil {
34		t.Fatal(err)
35	}
36	if len(f.Quads) == 0 || len(f.Quads) > n+1 || cap(f.Quads) != n+1 {
37		t.Fatalf("len=%d cap=%d, want cap %d", len(f.Quads), cap(f.Quads), n+1)
38	}
39}
40
41// Frames are detached: a later Prepare must not alias or overwrite an earlier
42// frame that is still queued, blocked or superseded.
43func TestSuccessiveFramesAreDetached(t *testing.T) {
44	p, err := NewPreparer(256, 2)
45	if err != nil {
46		t.Fatal(err)
47	}
48	a, err := p.Prepare(rectCommands(4), 1, 100, 100)
49	if err != nil {
50		t.Fatal(err)
51	}
52	want := append([]Quad(nil), a.Quads...)
53	b, err := p.Prepare(rectCommands(3), 2, 100, 100)
54	if err != nil {
55		t.Fatal(err)
56	}
57	if len(a.Quads) != 4 || len(b.Quads) != 3 || &a.Quads[0] == &b.Quads[0] {
58		t.Fatalf("frames alias: %d %d", len(a.Quads), len(b.Quads))
59	}
60	for i := range want {
61		if a.Quads[i] != want[i] {
62			t.Fatalf("earlier frame quad %d overwritten", i)
63		}
64	}
65	// Appending to a returned frame must not write into another frame.
66	b.Quads = append(b.Quads[:len(b.Quads):len(b.Quads)], Quad{Op: "x"})
67	if a.Quads[0] != want[0] {
68		t.Fatal("append leaked into earlier frame")
69	}
70}
71
72func TestFailedPrepareReturnsNoPartialFrame(t *testing.T) {
73	p, err := NewPreparer(256, 2)
74	if err != nil {
75		t.Fatal(err)
76	}
77	cmds := append(rectCommands(8), layout.Command{Op: "bogus"})
78	f, err := p.Prepare(cmds, 1, 100, 100)
79	if err == nil || f.Quads != nil || f.Uploads != nil {
80		t.Fatalf("partial frame on failure: %v %+v", err, f)
81	}
82	if f, err = p.Prepare(nil, 1, 100, 100); err != nil || f.Quads != nil {
83		t.Fatalf("empty frame: %v %+v", err, f)
84	}
85}
86
87// Preallocation must not disturb owed-upload accounting across frames whose
88// quads were sized differently.
89func TestPreallocatedFramesKeepUploadDebt(t *testing.T) {
90	cmd := glyphCommand(t, "office")
91	p, err := NewPreparer(256, 2)
92	if err != nil {
93		t.Fatal(err)
94	}
95	first, err := p.Prepare([]layout.Command{cmd}, 1, 200, 100)
96	if err != nil || len(first.Uploads) == 0 {
97		t.Fatalf("first: %v %d", err, len(first.Uploads))
98	}
99	// Superseding frame with no glyphs still resends the owed pages.
100	second, err := p.Prepare(rectCommands(2), 1, 200, 100)
101	if err != nil || len(second.Uploads) == 0 {
102		t.Fatalf("debt lost: %v %d", err, len(second.Uploads))
103	}
104	p.Submitted(first.Uploads)
105	if f, err := p.Prepare(rectCommands(2), 1, 200, 100); err != nil || len(f.Uploads) == 0 {
106		t.Fatalf("stale ack cleared debt: %v %d", err, len(f.Uploads))
107	}
108}
109
110// Box-only frames must cost one Quads allocation regardless of command count.
111func TestPrepareQuadAllocationsDoNotScaleWithCommands(t *testing.T) {
112	p, err := NewPreparer(256, 2)
113	if err != nil {
114		t.Fatal(err)
115	}
116	cmds := rectCommands(512)
117	allocs := testing.AllocsPerRun(20, func() {
118		if _, err := p.Prepare(cmds, 1, 100, 1000); err != nil {
119			t.Fatal(err)
120		}
121	})
122	if allocs > 2 {
123		t.Fatalf("%v allocs for 512 rects, want a small constant", allocs)
124	}
125}
126
127// PrepareInto reuses the caller's quads, so a steady frame allocates nothing,
128// and stale references from a longer frame are cleared.
129func TestPrepareIntoReusesQuads(t *testing.T) {
130	p, err := NewPreparer(256, 2)
131	if err != nil {
132		t.Fatal(err)
133	}
134	long, err := p.PrepareInto(nil, rectCommands(8), 1, 100, 1000)
135	if err != nil {
136		t.Fatal(err)
137	}
138	quads := long.Quads
139	short, err := p.PrepareInto(quads, rectCommands(3), 1, 100, 1000)
140	if err != nil || len(short.Quads) != 3 || &short.Quads[0] != &quads[0] {
141		t.Fatalf("not reused: %v %d", err, len(short.Quads))
142	}
143	if tail := quads[3:8]; tail[0] != (Quad{}) || tail[4] != (Quad{}) {
144		t.Fatal("stale quads kept past the new length")
145	}
146	cmds := rectCommands(64)
147	if allocs := testing.AllocsPerRun(20, func() {
148		f, err := p.PrepareInto(quads, cmds, 1, 100, 1000)
149		if err != nil {
150			t.Fatal(err)
151		}
152		quads = f.Quads
153	}); allocs != 0 {
154		t.Fatalf("PrepareInto allocs=%v, want 0", allocs)
155	}
156	// Mostly culled: the reservation stays large while few quads are visible.
157	culled := rectCommands(400)
158	if allocs := testing.AllocsPerRun(20, func() {
159		f, err := p.PrepareInto(quads, culled, 1, 100, 10)
160		if err != nil || len(f.Quads) > 10 {
161			t.Fatalf("culled frame: %v %d", err, len(f.Quads))
162		}
163		quads = f.Quads
164	}); allocs != 0 {
165		t.Fatalf("culled PrepareInto allocs=%v, want 0", allocs)
166	}
167	// A much smaller frame drops the past peak buffer.
168	f, err := p.PrepareInto(quads, rectCommands(2), 1, 100, 10)
169	if err != nil || cap(f.Quads) >= cap(quads) {
170		t.Fatalf("peak buffer kept: %v cap %d, was %d", err, cap(f.Quads), cap(quads))
171	}
172}
173
174func BenchmarkPrepareBoxes(b *testing.B) {
175	p, err := NewPreparer(256, 2)
176	if err != nil {
177		b.Fatal(err)
178	}
179	cmds := rectCommands(512)
180	b.ReportAllocs()
181	b.ResetTimer()
182	for i := 0; i < b.N; i++ {
183		if f, err := p.Prepare(cmds, 1, 100, 1000); err != nil || len(f.Quads) != 512 {
184			b.Fatal(err)
185		}
186	}
187}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 12

↑↓ moveenter openesc close