optimization: generate children counts on demand

Accumulating counts in the Node data structure takes a lot of effort,
and it turns out we only use this information in a few places where
it's easy to compute.
This commit is contained in:
Bryan Boreham
2020-06-08 16:42:24 +00:00
parent d76da767d8
commit 6a6ef51ef5
5 changed files with 20 additions and 28 deletions

View File

@@ -100,6 +100,17 @@ func (n Node) AddCounter(k string, value int) Node {
return n.WithLatest(name, mtime.Now(), strconv.Itoa(value))
}
// CountChildrenOfTopology returns how many children have a particular topology
func (n Node) CountChildrenOfTopology(topology string) int {
count := 0
for _, childID := range n.ChildIDs {
if ty, ok := NodeIDType(childID); ok && ty == topology {
count++
}
}
return count
}
// WithSet returns a fresh copy of n, with set merged in at key.
func (n Node) WithSet(key string, set StringSet) Node {
n.Sets = n.Sets.Add(key, set)