Skip to content

Commit

Permalink
go/callgraph/vta: optimize scc type initialization
Browse files Browse the repository at this point in the history
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 <zpavlinovic@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Tim King <taking@google.com>
Trust: Zvonimir Pavlinovic <zpavlinovic@google.com>
  • Loading branch information
zpavlinovic committed Sep 17, 2021
1 parent 1a7ca93 commit aba0c5f
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions go/callgraph/vta/propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,35 +118,32 @@ func (ptm propTypeMap) propTypes(n node) map[propType]bool {
// reaching the node. `canon` is used for type uniqueness.
func propagate(graph vtaGraph, canon *typeutil.Map) propTypeMap {
nodeToScc, sccID := scc(graph)
// Initialize sccToTypes to avoid repeated check
// for initialization later.
sccToTypes := make(map[int]map[propType]bool, sccID)
for i := 0; i <= sccID; i++ {
sccToTypes[i] = make(map[propType]bool)
}

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

// Initialize sccToTypes to avoid repeated check
// for initialization later.
sccToTypes := make(map[int]map[propType]bool, sccID)
for i := 0; i <= sccID; i++ {
sccToTypes[i] = nodeTypes(sccs[i], canon)
}

for i := len(sccs) - 1; i >= 0; i-- {
nodes := sccs[i]
// Save the types induced by the nodes of the SCC.
mergeTypes(sccToTypes[i], nodeTypes(nodes, canon))
nextSccs := make(map[int]bool)
for _, node := range nodes {
nextSccs := make(map[int]struct{})
for _, node := range sccs[i] {
for succ := range graph[node] {
nextSccs[nodeToScc[succ]] = true
nextSccs[nodeToScc[succ]] = struct{}{}
}
}
// Propagate types to all successor SCCs.
for nextScc := range nextSccs {
mergeTypes(sccToTypes[nextScc], sccToTypes[i])
}
}

return propTypeMap{nodeToScc: nodeToScc, sccToTypes: sccToTypes}
}

Expand Down

0 comments on commit aba0c5f

Please sign in to comment.