Skip to content

Commit

Permalink
Merge pull request #1720 from weaveworks/reduce-gc
Browse files Browse the repository at this point in the history
Improve performance of immutable maps
  • Loading branch information
Alfonso Acosta authored Jul 26, 2016
2 parents 0a77d60 + b5c488f commit 2132528
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 307 deletions.
2 changes: 1 addition & 1 deletion common/xfer/plugin_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"sort"

"github.com/davecgh/go-spew/spew"
"github.com/mndrix/ps"
"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"

"github.com/weaveworks/scope/test/reflect"
)
Expand Down
2 changes: 1 addition & 1 deletion report/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"reflect"
"sort"

"github.com/mndrix/ps"
"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"
)

// Counters is a string->int map.
Expand Down
2 changes: 1 addition & 1 deletion report/edge_metadatas.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"sort"
"strconv"

"github.com/mndrix/ps"
"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"
)

// EdgeMetadatas collect metadata about each edge in a topology. Keys are the
Expand Down
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/mndrix/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
}
68 changes: 20 additions & 48 deletions report/latest_map_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,67 +163,39 @@ func TestLatestMapEncoding(t *testing.T) {
Set("foo", now, "bar").
Set("bar", now, "baz")

{
gobs, err := want.GobEncode()
if err != nil {
t.Fatal(err)
}
for _, h := range []codec.Handle{
codec.Handle(&codec.MsgpackHandle{}),
codec.Handle(&codec.JsonHandle{}),
} {
buf := &bytes.Buffer{}
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyLatestMap
have.GobDecode(gobs)
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}

{

for _, h := range []codec.Handle{
codec.Handle(&codec.MsgpackHandle{}),
codec.Handle(&codec.JsonHandle{}),
} {
buf := &bytes.Buffer{}
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyLatestMap
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
}
}

func TestLatestMapEncodingNil(t *testing.T) {
want := LatestMap{}

{
gobs, err := want.GobEncode()
if err != nil {
t.Fatal(err)
}
for _, h := range []codec.Handle{
codec.Handle(&codec.MsgpackHandle{}),
codec.Handle(&codec.JsonHandle{}),
} {
buf := &bytes.Buffer{}
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyLatestMap
have.GobDecode(gobs)
if have.Map == nil {
t.Error("Decoded LatestMap.psMap should not be nil")
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}

{

for _, h := range []codec.Handle{
codec.Handle(&codec.MsgpackHandle{}),
codec.Handle(&codec.JsonHandle{}),
} {
buf := &bytes.Buffer{}
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyLatestMap
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
}
}
2 changes: 1 addition & 1 deletion report/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"math"
"time"

"github.com/mndrix/ps"
"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"
)

// Metrics is a string->metric map.
Expand Down
2 changes: 1 addition & 1 deletion report/node_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"sort"

"github.com/davecgh/go-spew/spew"
"github.com/mndrix/ps"
"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"

"github.com/weaveworks/scope/test/reflect"
)
Expand Down
2 changes: 1 addition & 1 deletion report/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"reflect"
"sort"

"github.com/mndrix/ps"
"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"
)

// Sets is a string->set-of-strings map.
Expand Down
8 changes: 0 additions & 8 deletions vendor/github.com/mndrix/ps/README.md

This file was deleted.

46 changes: 0 additions & 46 deletions vendor/github.com/mndrix/ps/list_test.go

This file was deleted.

Loading

0 comments on commit 2132528

Please sign in to comment.