Skip to content

Commit

Permalink
Call CodecDecodeSelf() instead of Decode()
Browse files Browse the repository at this point in the history
This avoids a runtime type lookup, so goes a little faster.
Also having less recursion makes it easier to interpret profiles.
  • Loading branch information
bboreham committed Mar 20, 2017
1 parent 03411a7 commit f239b71
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
5 changes: 3 additions & 2 deletions extras/generate_latest_map
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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.
Expand Down Expand Up @@ -124,7 +125,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.
Expand Down Expand Up @@ -180,7 +181,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
})
Expand Down
5 changes: 2 additions & 3 deletions report/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion report/edge_metadatas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions report/latest_map_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
})
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
})
Expand Down
7 changes: 7 additions & 0 deletions report/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions report/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit f239b71

Please sign in to comment.