mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 18:20:27 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user