Skip to content

Commit

Permalink
Use faster code to accumulate children when rendering
Browse files Browse the repository at this point in the history
Make sure all the Children NodeSets are not shared with any other
nodes, then we can use the non-persistent add path.
  • Loading branch information
bboreham committed Apr 12, 2018
1 parent 593952c commit f60d947
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 4 additions & 2 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ type joinResults struct {
func newJoinResults(inputNodes report.Nodes) joinResults {
nodes := make(report.Nodes, len(inputNodes))
for id, n := range inputNodes {
n.Adjacency = nil // result() assumes all nodes start with no adjacencies
n.Adjacency = nil // result() assumes all nodes start with no adjacencies
n.Children = n.Children.Copy() // so we can do unsafe adds
nodes[id] = n
}
return joinResults{nodes: nodes, mapped: map[string]string{}, multi: map[string][]string{}}
Expand All @@ -191,7 +192,7 @@ func (ret *joinResults) addUnmappedChild(m report.Node, id string, topology stri
if !exists {
result = report.MakeNode(id).WithTopology(topology)
}
result.Children = result.Children.Add(m)
result.Children.SingleOwnerAdd(m)
if m.Topology != report.Endpoint { // optimisation: we never look at endpoint counts
result.Counters = result.Counters.Add(m.Topology, 1)
}
Expand Down Expand Up @@ -219,6 +220,7 @@ func (ret *joinResults) addChildAndChildren(m report.Node, id string, topology s
func (ret *joinResults) passThrough(n report.Node) {
n.Adjacency = nil // result() assumes all nodes start with no adjacencies
ret.nodes[n.ID] = n
n.Children = n.Children.Copy() // so we can do unsafe adds
ret.mapChild(n.ID, n.ID)
}

Expand Down
20 changes: 18 additions & 2 deletions report/node_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,24 @@ func MakeNodeSet(nodes ...Node) NodeSet {
return emptyNodeSet.Add(nodes...)
}

// Add adds the nodes to the NodeSet. Add is the only valid way to grow a
// NodeSet. Add returns the NodeSet to enable chaining.
// Copy returns a value copy of the Nodes.
func (n NodeSet) Copy() NodeSet {
result := ps.NewMap()
n.ForEach(func(node Node) {
result = result.UnsafeMutableSet(node.ID, node)
})
return NodeSet{result}
}

// SingleOwnerAdd adds the nodes to the NodeSet. Only call this if n has one owner.
func (n *NodeSet) SingleOwnerAdd(node Node) {
if n.psMap == nil {
n.psMap = ps.NewMap()
}
n.psMap = n.psMap.UnsafeMutableSet(node.ID, node)
}

// Add adds the nodes to the NodeSet, and returns the NodeSet to enable chaining.
func (n NodeSet) Add(nodes ...Node) NodeSet {
if len(nodes) == 0 {
return n
Expand Down

0 comments on commit f60d947

Please sign in to comment.