performance: do Metrics merge with less copying

This commit is contained in:
Bryan Boreham
2020-06-04 16:16:16 +00:00
parent 74182867af
commit 09a81d8f3c
2 changed files with 16 additions and 5 deletions

View File

@@ -24,14 +24,25 @@ func (m Metrics) Merge(other Metrics) Metrics {
return m
}
result := m.Copy()
result.UnsafeMerge(other)
return result
}
// UnsafeMerge merges another set of Metrics into m, modifying the original.
func (m Metrics) UnsafeMerge(other Metrics) {
if len(m) == 0 {
if len(other) > 0 {
m = other.Copy()
}
return
}
for k, v := range other {
if rv, ok := result[k]; ok {
result[k] = rv.Merge(v)
if rv, ok := m[k]; ok {
m[k] = rv.Merge(v)
} else {
result[k] = v
m[k] = v
}
}
return result
}
// Copy returns a value copy of the sets map.

View File

@@ -186,7 +186,7 @@ func (n *Node) UnsafeMerge(other Node) {
n.Sets = n.Sets.Merge(other.Sets)
n.Adjacency = n.Adjacency.Merge(other.Adjacency)
n.Latest = n.Latest.Merge(other.Latest)
n.Metrics = n.Metrics.Merge(other.Metrics)
n.Metrics.UnsafeMerge(other.Metrics)
n.Parents = n.Parents.Merge(other.Parents)
n.Children = n.Children.Merge(other.Children)
}