From a80429ded2d0536e1002c8e34bce38029e9035ae Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 26 Jul 2016 09:22:11 +0000 Subject: [PATCH] LatestMap codec performance improvements and cleanups * Allocate all map entries of the intermadiate representation at once * Use UnsafeMutableMap to improve performance of LatestMap construction * Remove gob encoder/decoder --- report/latest_map.go | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/report/latest_map.go b/report/latest_map.go index 4742ab1e0..d45e119c7 100644 --- a/report/latest_map.go +++ b/report/latest_map.go @@ -2,13 +2,12 @@ package report import ( "bytes" - "encoding/gob" "fmt" "sort" "time" - "github.com/weaveworks/ps" "github.com/ugorji/go/codec" + "github.com/weaveworks/ps" ) // LatestMap is a persitent map which support latest-win merges. We have to @@ -169,7 +168,7 @@ func (m LatestMap) DeepEqual(n LatestMap) bool { } func (m LatestMap) toIntermediate() map[string]LatestEntry { - intermediate := map[string]LatestEntry{} + intermediate := make(map[string]LatestEntry, m.Size()) if m.Map != nil { m.Map.ForEach(func(key string, val interface{}) { intermediate[key] = val.(LatestEntry) @@ -178,14 +177,6 @@ func (m LatestMap) toIntermediate() map[string]LatestEntry { return intermediate } -func (m LatestMap) fromIntermediate(in map[string]LatestEntry) LatestMap { - out := ps.NewMap() - for k, v := range in { - out = out.Set(k, v) - } - return LatestMap{out} -} - // CodecEncodeSelf implements codec.Selfer func (m *LatestMap) CodecEncodeSelf(encoder *codec.Encoder) { if m.Map != nil { @@ -233,7 +224,7 @@ func (m *LatestMap) CodecDecodeSelf(decoder *codec.Decoder) { decoder.Decode(&value) } - out = out.Set(key, value) + out = out.UnsafeMutableSet(key, value) } z.DecSendContainerState(containerMapEnd) *m = LatestMap{out} @@ -248,20 +239,3 @@ func (LatestMap) MarshalJSON() ([]byte, error) { func (*LatestMap) UnmarshalJSON(b []byte) error { panic("UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead") } - -// GobEncode implements gob.Marshaller -func (m LatestMap) GobEncode() ([]byte, error) { - buf := bytes.Buffer{} - err := gob.NewEncoder(&buf).Encode(m.toIntermediate()) - return buf.Bytes(), err -} - -// GobDecode implements gob.Unmarshaller -func (m *LatestMap) GobDecode(input []byte) error { - in := map[string]LatestEntry{} - if err := gob.NewDecoder(bytes.NewBuffer(input)).Decode(&in); err != nil { - return err - } - *m = LatestMap{}.fromIntermediate(in) - return nil -}