Add all elements then sort in Node.WithLatests()

To save allocating and re-allocating the data structure by repeated
addition.
This commit is contained in:
Bryan Boreham
2018-07-16 21:24:36 +00:00
parent 761fafe61b
commit 6ff8316a1d
2 changed files with 36 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"sort"
"time"
"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"
@@ -138,3 +139,37 @@ func mapWrite(m ps.Map, encoder *codec.Encoder, encodeValue func(*codec.Encoder,
})
z.EncSendContainerState(containerMapEnd)
}
// Now follow helpers for StringLatestMap
// These let us sort a StringLatestMap strings by key
func (m StringLatestMap) Len() int { return len(m) }
func (m StringLatestMap) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m StringLatestMap) Less(i, j int) bool { return m[i].key < m[j].key }
// sort entries and shuffle down any duplicates
func (m StringLatestMap) fixup() {
sort.Sort(m)
for i := 1; i < len(m); {
if m[i-1].key == m[i].key {
if m[i-1].Timestamp.Before(m[i].Timestamp) {
m = append(m[:i-1], m[i:]...)
} else {
m = append(m[:i], m[i+1:]...)
}
} else {
i++
}
}
}
// add several entries at the same timestamp
func (m StringLatestMap) addMapEntries(ts time.Time, n map[string]string) StringLatestMap {
out := make(StringLatestMap, len(m), len(m)+len(n))
copy(out, m)
for k, v := range n {
out = append(out, stringLatestEntry{key: k, Value: v, Timestamp: ts})
}
out.fixup()
return out
}

View File

@@ -73,9 +73,7 @@ func (n Node) After(other Node) bool {
// WithLatests returns a fresh copy of n, with Metadata m merged in.
func (n Node) WithLatests(m map[string]string) Node {
ts := mtime.Now()
for k, v := range m {
n.Latest = n.Latest.Set(k, ts, v)
}
n.Latest = n.Latest.addMapEntries(ts, m)
return n
}