Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of immutable maps #1720

Merged
merged 3 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.


"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 {

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

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