Skip to content

Commit aba0c5f

Browse files
committed
go/callgraph/vta: optimize scc type initialization
During type propagation, initial types for an scc are two times stored to a map: once they are computed and the second time when they are saved to the type set of the scc. This CL ensures only one traversal is done. Change-Id: I7f6babae6a1130721c467e33da4f2d23462116d6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/350160 Run-TryBot: Zvonimir Pavlinovic <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Tim King <[email protected]> Trust: Zvonimir Pavlinovic <[email protected]>
1 parent 1a7ca93 commit aba0c5f

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

go/callgraph/vta/propagation.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -118,35 +118,32 @@ func (ptm propTypeMap) propTypes(n node) map[propType]bool {
118118
// reaching the node. `canon` is used for type uniqueness.
119119
func propagate(graph vtaGraph, canon *typeutil.Map) propTypeMap {
120120
nodeToScc, sccID := scc(graph)
121-
// Initialize sccToTypes to avoid repeated check
122-
// for initialization later.
123-
sccToTypes := make(map[int]map[propType]bool, sccID)
124-
for i := 0; i <= sccID; i++ {
125-
sccToTypes[i] = make(map[propType]bool)
126-
}
127121

128122
// We also need the reverse map, from ids to SCCs.
129123
sccs := make(map[int][]node, sccID)
130124
for n, id := range nodeToScc {
131125
sccs[id] = append(sccs[id], n)
132126
}
133127

128+
// Initialize sccToTypes to avoid repeated check
129+
// for initialization later.
130+
sccToTypes := make(map[int]map[propType]bool, sccID)
131+
for i := 0; i <= sccID; i++ {
132+
sccToTypes[i] = nodeTypes(sccs[i], canon)
133+
}
134+
134135
for i := len(sccs) - 1; i >= 0; i-- {
135-
nodes := sccs[i]
136-
// Save the types induced by the nodes of the SCC.
137-
mergeTypes(sccToTypes[i], nodeTypes(nodes, canon))
138-
nextSccs := make(map[int]bool)
139-
for _, node := range nodes {
136+
nextSccs := make(map[int]struct{})
137+
for _, node := range sccs[i] {
140138
for succ := range graph[node] {
141-
nextSccs[nodeToScc[succ]] = true
139+
nextSccs[nodeToScc[succ]] = struct{}{}
142140
}
143141
}
144142
// Propagate types to all successor SCCs.
145143
for nextScc := range nextSccs {
146144
mergeTypes(sccToTypes[nextScc], sccToTypes[i])
147145
}
148146
}
149-
150147
return propTypeMap{nodeToScc: nodeToScc, sccToTypes: sccToTypes}
151148
}
152149

0 commit comments

Comments
 (0)