Kaynağa Gözat

Cleans up comments and names regarding Graph

Wade Simba Khadder 8 yıl önce
ebeveyn
işleme
5c6a9fbe03
2 değiştirilmiş dosya ile 84 ekleme ve 79 silme
  1. 22
    19
      internal/graph/graph.go
  2. 62
    60
      internal/graph/graph_test.go

+ 22
- 19
internal/graph/graph.go Dosyayı Görüntüle

@@ -144,14 +144,14 @@ type NodeMap map[NodeInfo]*Node
144 144
 // NodeSet is a collection of node info structs.
145 145
 type NodeSet map[NodeInfo]bool
146 146
 
147
-// NodePtrSet is a collection of Nodes. Trimming a graph or tree requires a set
148
-// of objects that uniquely identify which nodes to keep. In a graph, NodeInfo
149
-// works as a unique identifier; however, in a tree, multiple nodes may share
150
-// identical NodeInfos. *Node do uniquely identify nodes so we can use those
151
-// instead.Though *Node also uniquely identify nodes in a Graph, we currently
152
-// rebuild Graphs from scratch using only the NodeSet when trimming, so there
153
-// would not be the required context of the initial graph to allow for the use
154
-// of *Node to identify the nodes.
147
+// NodePtrSet is a collection of nodes. Trimming a graph or tree requires a set
148
+// of objects which uniquely identify the nodes to keep. In a graph, NodeInfo
149
+// works as a unique identifier; however, in a tree multiple nodes may share
150
+// identical NodeInfos. A *Node does uniquely identify a node so we can use that
151
+// instead. Though a *Node also uniquely identifies a node in a graph,
152
+// currently, during trimming, graphs are rebult from scratch using only the
153
+// NodeSet, so there would not be the required context of the initial graph to
154
+// allow for the use of *Node.
155 155
 type NodePtrSet map[*Node]bool
156 156
 
157 157
 // FindOrInsertNode takes the info for a node and either returns a matching node
@@ -341,8 +341,8 @@ func newTree(prof *profile.Profile, o *Options) (g *Graph) {
341 341
 	return selectNodesForGraph(nodes, o.DropNegative)
342 342
 }
343 343
 
344
-// Trims a Graph that is in forest form to contain only the nodes in kept. This
345
-// will not work correctly in the case that a node has multiple parents.
344
+// TrimTree trims a Graph in forest form, keeping only the nodes in kept. This
345
+// will not work correctly if even a single node has multiple parents.
346 346
 func (g *Graph) TrimTree(kept NodePtrSet) {
347 347
 	// Creates a new list of nodes
348 348
 	oldNodes := g.Nodes
@@ -354,14 +354,14 @@ func (g *Graph) TrimTree(kept NodePtrSet) {
354 354
 			panic("TrimTree only works on trees")
355 355
 		}
356 356
 
357
-		// If a node should be kept, add it to the next list of nodes
357
+		// If a node should be kept, add it to the new list of nodes
358 358
 		if _, ok := kept[cur]; ok {
359 359
 			g.Nodes = append(g.Nodes, cur)
360 360
 			continue
361 361
 		}
362 362
 
363
-		// If a node has no parents, delete all the in edges of the children to make them
364
-		// all roots of their own trees.
363
+		// If a node has no parents, then delete all of the in edges of its
364
+		// children to make them each roots of their own trees.
365 365
 		if len(cur.In) == 0 {
366 366
 			for _, outEdge := range cur.Out {
367 367
 				delete(outEdge.Dest.In, cur)
@@ -369,8 +369,11 @@ func (g *Graph) TrimTree(kept NodePtrSet) {
369 369
 			continue
370 370
 		}
371 371
 
372
-		// Get the parent. Since cur.In may only be of size 0 or 1, parent will be
373
-		// equal to either nil or the only node in cur.In
372
+		// Get the parent. This works since at this point cur.In must contain only
373
+		// one element.
374
+		if len(cur.In) != 1 {
375
+			panic("Get parent assertion failed. cur.In expected to be of length 1.")
376
+		}
374 377
 		var parent *Node
375 378
 		for _, edge := range cur.In {
376 379
 			parent = edge.Src
@@ -606,7 +609,7 @@ func (g *Graph) DiscardLowFrequencyNodes(nodeCutoff int64) NodeSet {
606 609
 	return makeNodeSet(g.Nodes, nodeCutoff)
607 610
 }
608 611
 
609
-// discardLowFrequencyNodePtrs returns a NodePtrSet of nodes at or over a
612
+// DiscardLowFrequencyNodePtrs returns a NodePtrSet of nodes at or over a
610 613
 // specific cum value cutoff.
611 614
 func (g *Graph) DiscardLowFrequencyNodePtrs(nodeCutoff int64) NodePtrSet {
612 615
 	cutNodes := getNodesAboveCumCutoff(g.Nodes, nodeCutoff)
@@ -626,8 +629,8 @@ func makeNodeSet(nodes Nodes, nodeCutoff int64) NodeSet {
626 629
 	return kept
627 630
 }
628 631
 
629
-// getNodesAboveCumCutoff returns all the nodes who have a Cum value greater
630
-// than or equal to cutoff
632
+// getNodesAboveCumCutoff returns all the nodes which have a Cum value greater
633
+// than or equal to cutoff.
631 634
 func getNodesAboveCumCutoff(nodes Nodes, nodeCutoff int64) Nodes {
632 635
 	cutoffNodes := make(Nodes, 0, len(nodes))
633 636
 	for _, n := range nodes {
@@ -705,7 +708,7 @@ func (g *Graph) SelectTopNodes(maxNodes int, visualMode bool) NodeSet {
705 708
 	return makeNodeSet(g.selectTopNodes(maxNodes, visualMode), 0)
706 709
 }
707 710
 
708
-// selectTopNodes returns a slice of the top maxNodes nodes in a graph
711
+// selectTopNodes returns a slice of the top maxNodes nodes in a graph.
709 712
 func (g *Graph) selectTopNodes(maxNodes int, visualMode bool) Nodes {
710 713
 	if maxNodes > 0 {
711 714
 		if visualMode {

+ 62
- 60
internal/graph/graph_test.go Dosyayı Görüntüle

@@ -44,13 +44,13 @@ func graphDebugString(graph *Graph) string {
44 44
 	return debug
45 45
 }
46 46
 
47
-func expectedNodesDebugString(Expected []ExpectedNode) string {
47
+func expectedNodesDebugString(expected []ExpectedNode) string {
48 48
 	debug := ""
49
-	for i, node := range Expected {
49
+	for i, node := range expected {
50 50
 		debug += fmt.Sprintf("Node %d: %p\n", i, node.Node)
51 51
 	}
52 52
 
53
-	for i, node := range Expected {
53
+	for i, node := range expected {
54 54
 		debug += "\n"
55 55
 		debug += fmt.Sprintf("===  Node %d: %p  ===\n", i, node.Node)
56 56
 		debug += edgeMapsDebugString(node.In, node.Out)
@@ -58,7 +58,7 @@ func expectedNodesDebugString(Expected []ExpectedNode) string {
58 58
 	return debug
59 59
 }
60 60
 
61
-// Checks if all the edges in this equal all the edges in that.
61
+// edgeMapsEqual checks if all the edges in this equal all the edges in that.
62 62
 func edgeMapsEqual(this, that EdgeMap) bool {
63 63
 	if len(this) != len(that) {
64 64
 		return false
@@ -71,25 +71,25 @@ func edgeMapsEqual(this, that EdgeMap) bool {
71 71
 	return true
72 72
 }
73 73
 
74
-// Check if node is equal to Expected
75
-func nodesEqual(node *Node, Expected ExpectedNode) bool {
76
-	return node == Expected.Node && edgeMapsEqual(node.In, Expected.In) &&
77
-		edgeMapsEqual(node.Out, Expected.Out)
74
+// nodesEqual checks if node is equal to expected.
75
+func nodesEqual(node *Node, expected ExpectedNode) bool {
76
+	return node == expected.Node && edgeMapsEqual(node.In, expected.In) &&
77
+		edgeMapsEqual(node.Out, expected.Out)
78 78
 }
79 79
 
80
-// Check if the graph equals the one templated by Expected.
81
-func graphsEqual(graph *Graph, Expected []ExpectedNode) bool {
82
-	if len(graph.Nodes) != len(Expected) {
80
+// graphsEqual checks if graph is equivalent to the graph templated by expected.
81
+func graphsEqual(graph *Graph, expected []ExpectedNode) bool {
82
+	if len(graph.Nodes) != len(expected) {
83 83
 		return false
84 84
 	}
85
-	ExpectedSet := make(map[*Node]ExpectedNode)
86
-	for i := range Expected {
87
-		ExpectedSet[Expected[i].Node] = Expected[i]
85
+	expectedSet := make(map[*Node]ExpectedNode)
86
+	for i := range expected {
87
+		expectedSet[expected[i].Node] = expected[i]
88 88
 	}
89 89
 
90 90
 	for _, node := range graph.Nodes {
91
-		ExpectedNode, found := ExpectedSet[node]
92
-		if !found || !nodesEqual(node, ExpectedNode) {
91
+		expectedNode, found := expectedSet[node]
92
+		if !found || !nodesEqual(node, expectedNode) {
93 93
 			return false
94 94
 		}
95 95
 	}
@@ -107,7 +107,7 @@ type TrimTreeTestCase struct {
107 107
 	Keep     NodePtrSet
108 108
 }
109 109
 
110
-// Makes the edge from parent to child residual
110
+// makeExpectedEdgeResidual makes the edge from parent to child residual.
111 111
 func makeExpectedEdgeResidual(parent, child ExpectedNode) {
112 112
 	parent.Out[child.Node].Residual = true
113 113
 	child.In[parent.Node].Residual = true
@@ -121,7 +121,7 @@ func setEdgeWeight(edgeMap EdgeMap, node *Node, weight int64) {
121 121
 	edgeMap[node].Weight = weight
122 122
 }
123 123
 
124
-// Creates a directed edges from the parent to each of the children
124
+// createEdges creates directed edges from the parent to each of the children.
125 125
 func createEdges(parent *Node, children ...*Node) {
126 126
 	for _, child := range children {
127 127
 		edge := &Edge{
@@ -133,7 +133,7 @@ func createEdges(parent *Node, children ...*Node) {
133 133
 	}
134 134
 }
135 135
 
136
-// Creates a node without any edges
136
+// createEmptyNode creates a node without any edges.
137 137
 func createEmptyNode() *Node {
138 138
 	return &Node{
139 139
 		In:  make(EdgeMap),
@@ -141,24 +141,25 @@ func createEmptyNode() *Node {
141 141
 	}
142 142
 }
143 143
 
144
-// Creates an array of ExpectedNodes from nodes.
144
+// createExpectedNodes creates a slice of ExpectedNodes from nodes.
145 145
 func createExpectedNodes(nodes ...*Node) ([]ExpectedNode, NodePtrSet) {
146
-	Expected := make([]ExpectedNode, len(nodes))
147
-	Keep := make(NodePtrSet, len(nodes))
146
+	expected := make([]ExpectedNode, len(nodes))
147
+	keep := make(NodePtrSet, len(nodes))
148 148
 
149 149
 	for i, node := range nodes {
150
-		Expected[i] = ExpectedNode{
150
+		expected[i] = ExpectedNode{
151 151
 			Node: node,
152 152
 			In:   make(EdgeMap),
153 153
 			Out:  make(EdgeMap),
154 154
 		}
155
-		Keep[node] = true
155
+		keep[node] = true
156 156
 	}
157 157
 
158
-	return Expected, Keep
158
+	return expected, keep
159 159
 }
160 160
 
161
-// Creates a directed edges from the parent to each of the children
161
+// createExpectedEdges creates directed edges from the parent to each of the
162
+// children.
162 163
 func createExpectedEdges(parent ExpectedNode, children ...ExpectedNode) {
163 164
 	for _, child := range children {
164 165
 		edge := &Edge{
@@ -170,17 +171,17 @@ func createExpectedEdges(parent ExpectedNode, children ...ExpectedNode) {
170 171
 	}
171 172
 }
172 173
 
173
-// The first test case looks like:
174
+// createTestCase1 creates a test case that initally looks like:
174 175
 //     0
175 176
 //     |(5)
176 177
 //     1
177 178
 // (3)/ \(4)
178
-//   2   3
179
+//   2   3.
179 180
 //
180
-// After Keeping 0, 2, 3. We should see:
181
+// After keeping 0, 2, and 3, it expects the graph:
181 182
 //     0
182 183
 // (3)/ \(4)
183
-//   2   3
184
+//   2   3.
184 185
 func createTestCase1() TrimTreeTestCase {
185 186
 	// Create Initial graph
186 187
 	graph := &Graph{make(Nodes, 4)}
@@ -197,21 +198,21 @@ func createTestCase1() TrimTreeTestCase {
197 198
 	setEdgeWeight(nodes[1].Out, nodes[3], 4)
198 199
 
199 200
 	// Create Expected graph
200
-	Expected, Keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
201
-	createExpectedEdges(Expected[0], Expected[1], Expected[2])
202
-	makeEdgeInline(Expected[0].Out, Expected[1].Node)
203
-	makeExpectedEdgeResidual(Expected[0], Expected[1])
204
-	makeExpectedEdgeResidual(Expected[0], Expected[2])
205
-	setEdgeWeight(Expected[0].Out, Expected[1].Node, 3)
206
-	setEdgeWeight(Expected[0].Out, Expected[2].Node, 4)
201
+	expected, keep := createExpectedNodes(nodes[0], nodes[2], nodes[3])
202
+	createExpectedEdges(expected[0], expected[1], expected[2])
203
+	makeEdgeInline(expected[0].Out, expected[1].Node)
204
+	makeExpectedEdgeResidual(expected[0], expected[1])
205
+	makeExpectedEdgeResidual(expected[0], expected[2])
206
+	setEdgeWeight(expected[0].Out, expected[1].Node, 3)
207
+	setEdgeWeight(expected[0].Out, expected[2].Node, 4)
207 208
 	return TrimTreeTestCase{
208 209
 		Initial:  graph,
209
-		Expected: Expected,
210
-		Keep:     Keep,
210
+		Expected: expected,
211
+		Keep:     keep,
211 212
 	}
212 213
 }
213 214
 
214
-// This test case looks like:
215
+// createTestCase2 creates a test case that initially looks like:
215 216
 //   3
216 217
 //   | (12)
217 218
 //   1
@@ -220,12 +221,12 @@ func createTestCase1() TrimTreeTestCase {
220 221
 //   | (15)
221 222
 //   0
222 223
 //   | (10)
223
-//   4
224
+//   4.
224 225
 //
225
-// After Keeping 3 and 4. We should see:
226
+// After keeping 3 and 4, it expects the graph:
226 227
 //   3
227 228
 //   | (10)
228
-//   4
229
+//   4.
229 230
 func createTestCase2() TrimTreeTestCase {
230 231
 	// Create Initial graph
231 232
 	graph := &Graph{make(Nodes, 5)}
@@ -243,44 +244,45 @@ func createTestCase2() TrimTreeTestCase {
243 244
 	setEdgeWeight(nodes[0].Out, nodes[4], 10)
244 245
 
245 246
 	// Create Expected graph
246
-	Expected, Keep := createExpectedNodes(nodes[3], nodes[4])
247
-	createExpectedEdges(Expected[0], Expected[1])
248
-	makeExpectedEdgeResidual(Expected[0], Expected[1])
249
-	setEdgeWeight(Expected[0].Out, Expected[1].Node, 10)
247
+	expected, keep := createExpectedNodes(nodes[3], nodes[4])
248
+	createExpectedEdges(expected[0], expected[1])
249
+	makeExpectedEdgeResidual(expected[0], expected[1])
250
+	setEdgeWeight(expected[0].Out, expected[1].Node, 10)
250 251
 	return TrimTreeTestCase{
251 252
 		Initial:  graph,
252
-		Expected: Expected,
253
-		Keep:     Keep,
253
+		Expected: expected,
254
+		Keep:     keep,
254 255
 	}
255 256
 }
256 257
 
257
-// If we trim an empty graph it should still be empty afterwards
258
+// createTestCase3 creates an initally empty graph and expects an empty graph
259
+// after trimming.
258 260
 func createTestCase3() TrimTreeTestCase {
259 261
 	graph := &Graph{make(Nodes, 0)}
260
-	Expected, Keep := createExpectedNodes()
262
+	expected, keep := createExpectedNodes()
261 263
 	return TrimTreeTestCase{
262 264
 		Initial:  graph,
263
-		Expected: Expected,
264
-		Keep:     Keep,
265
+		Expected: expected,
266
+		Keep:     keep,
265 267
 	}
266 268
 }
267 269
 
268
-// This test case looks like:
269
-//   0
270
+// createTestCase4 creates a test case that initially looks like:
271
+//   0.
270 272
 //
271
-// After Keeping 0. We should see:
272
-//   0
273
+// After keeping 0, it expects the graph:
274
+//   0.
273 275
 func createTestCase4() TrimTreeTestCase {
274 276
 	graph := &Graph{make(Nodes, 1)}
275 277
 	nodes := graph.Nodes
276 278
 	for i := range nodes {
277 279
 		nodes[i] = createEmptyNode()
278 280
 	}
279
-	Expected, Keep := createExpectedNodes(nodes[0])
281
+	expected, keep := createExpectedNodes(nodes[0])
280 282
 	return TrimTreeTestCase{
281 283
 		Initial:  graph,
282
-		Expected: Expected,
283
-		Keep:     Keep,
284
+		Expected: expected,
285
+		Keep:     keep,
284 286
 	}
285 287
 }
286 288