gitstack

grindlemire/go-tui code browser

5.0 KB Go 204 lines 2026-06-03 · d15bb9f raw
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package layout

import "testing"

// buildTree creates a tree with the specified branching factor and depth.
// Total nodes = sum of (branching^i) for i from 0 to depth = (branching^(depth+1) - 1) / (branching - 1)
func buildTree(branching, depth int) *testNode {
	root := newTestNode(DefaultStyle())
	root.style.Width = Fixed(1000)
	root.style.Height = Fixed(1000)
	root.style.Display = DisplayFlex
	root.style.Direction = Row

	if depth > 0 {
		addChildrenRecursive(root, branching, depth-1)
	}

	return root
}

func addChildrenRecursive(parent *testNode, branching, remainingDepth int) {
	for range branching {
		child := newTestNode(DefaultStyle())
		child.style.FlexGrow = 1

		// Alternate direction at each level
		child.style.Display = DisplayFlex
		if parent.style.Direction == Row {
			child.style.Direction = Column
		} else {
			child.style.Direction = Row
		}

		parent.AddChild(child)

		if remainingDepth > 0 {
			addChildrenRecursive(child, branching, remainingDepth-1)
		}
	}
}

// buildLinearTree creates a tree with n nodes in a linear chain.
func buildLinearTree(n int) *testNode {
	root := newTestNode(DefaultStyle())
	root.style.Width = Fixed(1000)
	root.style.Height = Fixed(1000)
	root.style.Display = DisplayFlex
	root.style.Direction = Row

	for range n {
		child := newTestNode(DefaultStyle())
		child.style.Width = Fixed(10)
		child.style.Height = Fixed(100)
		root.AddChild(child)
	}

	return root
}

// countNodes counts the total number of nodes in a tree.
func countNodes(node *testNode) int {
	if node == nil {
		return 0
	}
	count := 1
	for _, child := range node.children {
		count += countNodes(child)
	}
	return count
}

// BenchmarkCalculate_10Nodes benchmarks layout calculation with ~10 nodes.
// Tree structure: branching=3, depth=2 = 1 + 3 + 9 = 13 nodes
func BenchmarkCalculate_10Nodes(b *testing.B) {
	root := buildTree(3, 2)
	nodeCount := countNodes(root)
	b.Logf("Node count: %d", nodeCount)

	// Initial calculation to warm up
	Calculate(root, 1000, 1000)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		// Mark root dirty to force full recalculation
		root.markDirty()
		Calculate(root, 1000, 1000)
	}
}

// BenchmarkCalculate_100Nodes benchmarks layout calculation with ~100 nodes.
// Tree structure: branching=3, depth=4 = 1 + 3 + 9 + 27 + 81 = 121 nodes
func BenchmarkCalculate_100Nodes(b *testing.B) {
	root := buildTree(3, 4)
	nodeCount := countNodes(root)
	b.Logf("Node count: %d", nodeCount)

	Calculate(root, 1000, 1000)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		root.markDirty()
		Calculate(root, 1000, 1000)
	}
}

// BenchmarkCalculate_1000Nodes benchmarks layout calculation with ~1000 nodes.
// Linear tree: 1 root + 999 children = 1000 nodes
func BenchmarkCalculate_1000Nodes(b *testing.B) {
	root := buildLinearTree(999)
	nodeCount := countNodes(root)
	b.Logf("Node count: %d", nodeCount)

	Calculate(root, 10000, 1000)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		root.markDirty()
		Calculate(root, 10000, 1000)
	}
}

// BenchmarkCalculate_Incremental benchmarks incremental layout
// when only a single leaf node is modified.
func BenchmarkCalculate_Incremental(b *testing.B) {
	root := buildTree(3, 4) // ~121 nodes
	Calculate(root, 1000, 1000)

	// Find a leaf node
	leaf := root
	for len(leaf.children) > 0 {
		leaf = leaf.children[0]
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		// Only mark the leaf dirty (should trigger path-to-root recalculation)
		leaf.markDirty()
		Calculate(root, 1000, 1000)
	}
}

// BenchmarkCalculate_IncrementalVsFull compares incremental vs full layout.
// Note: When a leaf is marked dirty, it propagates to the root, causing
// the entire tree to be recalculated. True incremental savings occur when
// only part of a tree (e.g., one subtree of many siblings) is dirty.
func BenchmarkCalculate_IncrementalVsFull(b *testing.B) {
	root := buildTree(3, 4) // ~121 nodes
	Calculate(root, 1000, 1000)

	// Find a leaf node
	leaf := root
	for len(leaf.children) > 0 {
		leaf = leaf.children[0]
	}

	b.Run("full", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			root.markDirty()
			Calculate(root, 1000, 1000)
		}
	})

	b.Run("incremental", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			leaf.markDirty()
			Calculate(root, 1000, 1000)
		}
	})
}

// BenchmarkCalculate_AllocationsPerNode verifies allocation behavior.
// Should allocate only for flexItem slices in nodes with children.
func BenchmarkCalculate_AllocationsPerNode(b *testing.B) {
	root := buildLinearTree(10) // 1 root + 10 children
	Calculate(root, 1000, 1000)

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		root.markDirty()
		Calculate(root, 1000, 1000)
	}

	// Expected: 1 allocation per Calculate call (for flexItem slice in root)
	// Leaf nodes don't allocate since they have no children
}

// BenchmarkNewTestNode benchmarks node creation.
func BenchmarkNewTestNode(b *testing.B) {
	style := DefaultStyle()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		_ = newTestNode(style)
	}
}

// BenchmarkDefaultStyle benchmarks style creation.
func BenchmarkDefaultStyle(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = DefaultStyle()
	}
}