From 6ff8316a1dc3ee452079467c3e02464aa3257c4a Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 16 Jul 2018 21:24:36 +0000 Subject: [PATCH] Add all elements then sort in Node.WithLatests() To save allocating and re-allocating the data structure by repeated addition. --- report/map_helpers.go | 35 +++++++++++++++++++++++++++++++++++ report/node.go | 4 +--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/report/map_helpers.go b/report/map_helpers.go index 480ddcf26..359785465 100644 --- a/report/map_helpers.go +++ b/report/map_helpers.go @@ -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 +} diff --git a/report/node.go b/report/node.go index 94fcb5c58..de32c5a2e 100644 --- a/report/node.go +++ b/report/node.go @@ -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 }