Skip to content

Commit

Permalink
LatestMap codec performance improvements and cleanups
Browse files Browse the repository at this point in the history
* Allocate all map entries of the intermadiate representation at once
* Use UnsafeMutableMap to improve performance of LatestMap construction
* Remove gob encoder/decoder
  • Loading branch information
Alfonso Acosta committed Jul 26, 2016
1 parent ecc8a31 commit a80429d
Showing 1 changed file with 3 additions and 29 deletions.
32 changes: 3 additions & 29 deletions report/latest_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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}
Expand All @@ -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
}

0 comments on commit a80429d

Please sign in to comment.