Call CodecDecodeSelf() instead of Decode()

This avoids a runtime type lookup, so goes a little faster.
Also having less recursion makes it easier to interpret profiles.
This commit is contained in:
Bryan Boreham
2017-03-20 22:52:13 +00:00
parent 6f80fcd870
commit b3f53a7a81
6 changed files with 22 additions and 13 deletions

View File

@@ -47,6 +47,7 @@ function generate_latest_map() {
type ${entry_type} struct {
Timestamp time.Time ${json_timestamp}
Value ${data_type} ${json_value}
dummySelfer
}
// String returns the StringLatestEntry's string representation.
@@ -122,7 +123,7 @@ function generate_latest_map() {
if m.Map == nil {
m.Map = ps.NewMap()
}
return ${latest_map_type}{m.Map.Set(key, &${entry_type}{timestamp, value})}
return ${latest_map_type}{m.Map.Set(key, &${entry_type}{Timestamp: timestamp, Value: value})}
}
// Delete the value for the given key.
@@ -178,7 +179,7 @@ function generate_latest_map() {
out := mapRead(decoder, func(isNil bool) interface{} {
value := &${entry_type}{}
if !isNil {
decoder.Decode(value)
value.CodecDecodeSelf(decoder)
}
return value
})

View File

@@ -91,6 +91,7 @@ func (nc NodeControls) Add(ids ...string) NodeControls {
type wireNodeControls struct {
Timestamp string `json:"timestamp,omitempty"`
Controls StringSet `json:"controls,omitempty"`
dummySelfer
}
// CodecEncodeSelf implements codec.Selfer
@@ -104,9 +105,7 @@ func (nc *NodeControls) CodecEncodeSelf(encoder *codec.Encoder) {
// CodecDecodeSelf implements codec.Selfer
func (nc *NodeControls) CodecDecodeSelf(decoder *codec.Decoder) {
in := wireNodeControls{}
if err := decoder.Decode(&in); err != nil {
return
}
in.CodecDecodeSelf(decoder)
*nc = NodeControls{
Timestamp: parseTime(in.Timestamp),
Controls: in.Controls,

View File

@@ -180,7 +180,7 @@ func (c *EdgeMetadatas) CodecDecodeSelf(decoder *codec.Decoder) {
out := mapRead(decoder, func(isNil bool) interface{} {
var value EdgeMetadata
if !isNil {
decoder.Decode(&value)
value.CodecDecodeSelf(decoder)
}
return value
})
@@ -221,6 +221,7 @@ type EdgeMetadata struct {
IngressPacketCount *uint64 `json:"ingress_packet_count,omitempty"`
EgressByteCount *uint64 `json:"egress_byte_count,omitempty"` // Transport layer
IngressByteCount *uint64 `json:"ingress_byte_count,omitempty"` // Transport layer
dummySelfer
}
// String returns a string representation of this EdgeMetadata

View File

@@ -14,6 +14,7 @@ import (
type stringLatestEntry struct {
Timestamp time.Time `json:"timestamp"`
Value string `json:"value"`
dummySelfer
}
// String returns the StringLatestEntry's string representation.
@@ -89,7 +90,7 @@ func (m StringLatestMap) Set(key string, timestamp time.Time, value string) Stri
if m.Map == nil {
m.Map = ps.NewMap()
}
return StringLatestMap{m.Map.Set(key, &stringLatestEntry{timestamp, value})}
return StringLatestMap{m.Map.Set(key, &stringLatestEntry{Timestamp: timestamp, Value: value})}
}
// Delete the value for the given key.
@@ -145,7 +146,7 @@ func (m *StringLatestMap) CodecDecodeSelf(decoder *codec.Decoder) {
out := mapRead(decoder, func(isNil bool) interface{} {
value := &stringLatestEntry{}
if !isNil {
decoder.Decode(value)
value.CodecDecodeSelf(decoder)
}
return value
})
@@ -165,6 +166,7 @@ func (*StringLatestMap) UnmarshalJSON(b []byte) error {
type nodeControlDataLatestEntry struct {
Timestamp time.Time `json:"timestamp"`
Value NodeControlData `json:"value"`
dummySelfer
}
// String returns the StringLatestEntry's string representation.
@@ -240,7 +242,7 @@ func (m NodeControlDataLatestMap) Set(key string, timestamp time.Time, value Nod
if m.Map == nil {
m.Map = ps.NewMap()
}
return NodeControlDataLatestMap{m.Map.Set(key, &nodeControlDataLatestEntry{timestamp, value})}
return NodeControlDataLatestMap{m.Map.Set(key, &nodeControlDataLatestEntry{Timestamp: timestamp, Value: value})}
}
// Delete the value for the given key.
@@ -296,7 +298,7 @@ func (m *NodeControlDataLatestMap) CodecDecodeSelf(decoder *codec.Decoder) {
out := mapRead(decoder, func(isNil bool) interface{} {
value := &nodeControlDataLatestEntry{}
if !isNil {
decoder.Decode(value)
value.CodecDecodeSelf(decoder)
}
return value
})

View File

@@ -10,6 +10,13 @@ import (
"github.com/ugorji/go/codec"
)
// Include this in a struct to be able to call CodecDecodeSelf() before code generation
type dummySelfer struct{}
func (s *dummySelfer) CodecDecodeSelf(decoder *codec.Decoder) {
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)

View File

@@ -225,6 +225,7 @@ type WireMetrics struct {
Max float64 `json:"max"`
First string `json:"first,omitempty"`
Last string `json:"last,omitempty"`
dummySelfer
}
func renderTime(t time.Time) string {
@@ -272,9 +273,7 @@ func (m *Metric) CodecEncodeSelf(encoder *codec.Encoder) {
// CodecDecodeSelf implements codec.Selfer
func (m *Metric) CodecDecodeSelf(decoder *codec.Decoder) {
in := WireMetrics{}
if err := decoder.Decode(&in); err != nil {
return
}
in.CodecDecodeSelf(decoder)
*m = in.FromIntermediate()
}