Skip to content

Commit

Permalink
Write psMap out directly
Browse files Browse the repository at this point in the history
rather than going via intermediate data structure
  • Loading branch information
bboreham committed Apr 25, 2017
1 parent 0281d4a commit 36ff5ab
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 57 deletions.
18 changes: 3 additions & 15 deletions extras/generate_latest_map
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,11 @@ function generate_latest_map() {
})
}
func (m ${latest_map_type}) toIntermediate() map[string]${entry_type} {
intermediate := make(map[string]${entry_type}, m.Size())
if m.Map != nil {
m.Map.ForEach(func(key string, val interface{}) {
intermediate[key] = *val.(*${entry_type})
})
}
return intermediate
}
// CodecEncodeSelf implements codec.Selfer.
func (m *${latest_map_type}) CodecEncodeSelf(encoder *codec.Encoder) {
if m.Map != nil {
encoder.Encode(m.toIntermediate())
} else {
encoder.Encode(nil)
}
mapWrite(m.Map, encoder, func(encoder *codec.Encoder, val interface{}) {
val.(*${entry_type}).CodecEncodeSelf(encoder)
})
}
// CodecDecodeSelf implements codec.Selfer.
Expand Down
5 changes: 4 additions & 1 deletion report/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ func (c Counters) fromIntermediate(in map[string]int) Counters {

// CodecEncodeSelf implements codec.Selfer
func (c *Counters) CodecEncodeSelf(encoder *codec.Encoder) {
encoder.Encode(c.toIntermediate())
mapWrite(c.psMap, encoder, func(encoder *codec.Encoder, val interface{}) {
i := val.(int)
encoder.Encode(i)
})
}

// CodecDecodeSelf implements codec.Selfer
Expand Down
9 changes: 4 additions & 5 deletions report/edge_metadatas.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,10 @@ func (c EdgeMetadatas) fromIntermediate(in map[string]EdgeMetadata) EdgeMetadata

// CodecEncodeSelf implements codec.Selfer
func (c *EdgeMetadatas) CodecEncodeSelf(encoder *codec.Encoder) {
if c.psMap != nil {
encoder.Encode(c.toIntermediate())
} else {
encoder.Encode(nil)
}
mapWrite(c.psMap, encoder, func(encoder *codec.Encoder, val interface{}) {
e := val.(EdgeMetadata)
(&e).CodecEncodeSelf(encoder)
})
}

// CodecDecodeSelf implements codec.Selfer
Expand Down
36 changes: 6 additions & 30 deletions report/latest_map_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,11 @@ func (m StringLatestMap) DeepEqual(n StringLatestMap) bool {
})
}

func (m StringLatestMap) toIntermediate() map[string]stringLatestEntry {
intermediate := make(map[string]stringLatestEntry, m.Size())
if m.Map != nil {
m.Map.ForEach(func(key string, val interface{}) {
intermediate[key] = *val.(*stringLatestEntry)
})
}
return intermediate
}

// CodecEncodeSelf implements codec.Selfer.
func (m *StringLatestMap) CodecEncodeSelf(encoder *codec.Encoder) {
if m.Map != nil {
encoder.Encode(m.toIntermediate())
} else {
encoder.Encode(nil)
}
mapWrite(m.Map, encoder, func(encoder *codec.Encoder, val interface{}) {
val.(*stringLatestEntry).CodecEncodeSelf(encoder)
})
}

// CodecDecodeSelf implements codec.Selfer.
Expand Down Expand Up @@ -274,23 +262,11 @@ func (m NodeControlDataLatestMap) DeepEqual(n NodeControlDataLatestMap) bool {
})
}

func (m NodeControlDataLatestMap) toIntermediate() map[string]nodeControlDataLatestEntry {
intermediate := make(map[string]nodeControlDataLatestEntry, m.Size())
if m.Map != nil {
m.Map.ForEach(func(key string, val interface{}) {
intermediate[key] = *val.(*nodeControlDataLatestEntry)
})
}
return intermediate
}

// CodecEncodeSelf implements codec.Selfer.
func (m *NodeControlDataLatestMap) CodecEncodeSelf(encoder *codec.Encoder) {
if m.Map != nil {
encoder.Encode(m.toIntermediate())
} else {
encoder.Encode(nil)
}
mapWrite(m.Map, encoder, func(encoder *codec.Encoder, val interface{}) {
val.(*nodeControlDataLatestEntry).CodecEncodeSelf(encoder)
})
}

// CodecDecodeSelf implements codec.Selfer.
Expand Down
21 changes: 20 additions & 1 deletion report/map_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ const (
containerMapKey = 2
containerMapValue = 3
containerMapEnd = 4
// from https://github.com/ugorji/go/blob/master/codec/helper.go#L152
cUTF8 = 2
)

// CodecDecodeSelf implements codec.Selfer.
// This implementation does not use the intermediate form as that was a
// performance issue; skipping it saved almost 10% CPU. Note this means
// we are using undocumented, internal APIs, which could break in the future.
Expand Down Expand Up @@ -118,3 +119,21 @@ func mapRead(decoder *codec.Decoder, decodeValue func(isNil bool) interface{}) p
z.DecSendContainerState(containerMapEnd)
return out
}

// Inverse of mapRead, done for performance. Same comments about
// undocumented internal APIs apply.
func mapWrite(m ps.Map, encoder *codec.Encoder, encodeValue func(*codec.Encoder, interface{})) {
z, r := codec.GenHelperEncoder(encoder)
if m == nil {
r.EncodeNil()
return
}
r.EncodeMapStart(m.Size())
m.ForEach(func(key string, val interface{}) {
z.EncSendContainerState(containerMapKey)
r.EncodeString(cUTF8, key)
z.EncSendContainerState(containerMapValue)
encodeValue(encoder, val)
})
z.EncSendContainerState(containerMapEnd)
}
4 changes: 4 additions & 0 deletions report/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (s *dummySelfer) CodecDecodeSelf(decoder *codec.Decoder) {
panic("This shouldn't happen: perhaps something has gone wrong in code generation?")
}

func (s *dummySelfer) CodecEncodeSelf(encoder *codec.Encoder) {
panic("This shouldn't happen: perhaps something has gone wrong in code generation?")
}

// WriteBinary writes a Report as a gzipped msgpack.
func (rep Report) WriteBinary(w io.Writer, compressionLevel int) error {
gzwriter, err := gzip.NewWriterLevel(w, compressionLevel)
Expand Down
8 changes: 3 additions & 5 deletions report/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,9 @@ func (s Sets) fromIntermediate(in map[string]StringSet) Sets {

// CodecEncodeSelf implements codec.Selfer
func (s *Sets) CodecEncodeSelf(encoder *codec.Encoder) {
if s.psMap != nil {
encoder.Encode(s.toIntermediate())
} else {
encoder.Encode(nil)
}
mapWrite(s.psMap, encoder, func(encoder *codec.Encoder, val interface{}) {
encoder.Encode(val.(StringSet))
})
}

// CodecDecodeSelf implements codec.Selfer
Expand Down

0 comments on commit 36ff5ab

Please sign in to comment.