Skip to content
This repository was archived by the owner on May 8, 2019. It is now read-only.

Commit be6e46f

Browse files
committed
Various performance and documentation improvements
1 parent c26bd37 commit be6e46f

File tree

6 files changed

+175
-284
lines changed

6 files changed

+175
-284
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SHELL := /bin/bash
1010

1111
BIN = $(GOBIN)/msgp
1212

13-
.PHONY: clean wipe install get-deps bench all
13+
.PHONY: fmt clean wipe install get-deps bench all
1414

1515
$(BIN): */*.go
1616
@go install ./...
@@ -23,6 +23,9 @@ $(GGEN): ./tests/def.go
2323
$(MGEN): ./msgp/defs_test.go
2424
go generate ./msgp
2525

26+
fmt:
27+
gofmt -s -w -e ./
28+
2629
test: all
2730
go test -v ./...
2831

msgp/errors.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"reflect"
66
)
77

8-
// ErrShortBytes is returned when the slice being decoded is too short
9-
// to contain the contents of the message.
8+
// ErrShortBytes is returned when the slice being decoded is too short to contain
9+
// the contents of the message.
1010
var ErrShortBytes error = errShort{}
1111

1212
// A fatal error is only returned if we reach code that should be unreachable.
@@ -30,38 +30,36 @@ type errFatal struct{}
3030
func (f errFatal) Error() string { return "msgp: fatal decoding error (unreachable code)" }
3131
func (f errFatal) Resumable() bool { return false }
3232

33-
// ArrayError is an error returned when decoding a fix-sized array
34-
// of the wrong size.
33+
// An ArrayError error is returned when decoding a fix-sized array of the wrong size.
3534
type ArrayError struct {
36-
Wanted uint32
37-
Got uint32
35+
Wanted, Got uint32
3836
}
3937

40-
// Error implements the error interface
38+
// Error implements the error interface.
4139
func (a ArrayError) Error() string {
4240
return fmt.Sprintf("msgp: wanted array of size %d; got %d", a.Wanted, a.Got)
4341
}
4442

4543
// Resumable is always true for ArrayErrors.
4644
func (a ArrayError) Resumable() bool { return true }
4745

48-
// IntOverflow is returned when a call would downcast an integer to a type
46+
// An IntOverflow error is returned when an operation would downcast an integer to a type
4947
// with too few bits to hold its value.
5048
type IntOverflow struct {
5149
Value int64 // the value of the integer
5250
FailedBitsize int // the bit size that the int64 could not fit into
5351
}
5452

55-
// Error implements the error interface
53+
// Error implements the error interface.
5654
func (i IntOverflow) Error() string {
5755
return fmt.Sprintf("msgp: %d overflows int%d", i.Value, i.FailedBitsize)
5856
}
5957

6058
// Resumable is always true for overflows.
6159
func (i IntOverflow) Resumable() bool { return true }
6260

63-
// UintOverflow is returned when a call would downcast an unsigned integer to
64-
// a type with too few bits to hold its value
61+
// A UintOverflow error is returned when an operation would downcast an unsigned integer to
62+
// a type with too few bits to hold its value.
6563
type UintOverflow struct {
6664
Value uint64 // value of the uint
6765
FailedBitsize int // the bit size that couldn't fit the value
@@ -122,5 +120,5 @@ type ErrUnsupportedType struct {
122120
// Error implements error
123121
func (e *ErrUnsupportedType) Error() string { return fmt.Sprintf("msgp: type %q not supported", e.T) }
124122

125-
// Resumable returns 'true' for ErrUnsupportedType
123+
// Resumable returns true for ErrUnsupportedType.
126124
func (e *ErrUnsupportedType) Resumable() bool { return true }

msgp/extension.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,30 @@ func (e ExtensionTypeError) Error() string {
5454
return fmt.Sprintf("msgp: error decoding extension: wanted type %d; got type %d", e.Want, e.Got)
5555
}
5656

57-
// Resumable returns 'true' for ExtensionTypeErrors
57+
// Resumable returns true for ExtensionTypeErrors.
5858
func (e ExtensionTypeError) Resumable() bool { return true }
5959

6060
func errExt(got int8, wanted int8) error {
6161
return ExtensionTypeError{Got: got, Want: wanted}
6262
}
6363

64-
// Extension is the interface fulfilled
65-
// by types that want to define their
66-
// own binary encoding.
64+
// Extension is the interface implemented by types that want to define their own binary encoding.
6765
type Extension interface {
68-
// ExtensionType should return
69-
// a int8 that identifies the concrete
70-
// type of the extension. (Types <0 are
71-
// officially reserved by the MessagePack
72-
// specifications.)
66+
// ExtensionType returns an int8 that identifies the extension's concrete type.
67+
// (Types <0 are reserved by the MessagePack specification.)
7368
ExtensionType() int8
7469

75-
// Len should return the length
76-
// of the data to be encoded
70+
// Len returns the length of the data to be encoded.
7771
Len() int
7872

79-
// MarshalBinaryTo should copy
80-
// the data into the supplied slice,
81-
// assuming that the slice has length Len()
73+
// MarshalBinaryTo copies the data into the supplied slice, assuming that the
74+
// slice has length Len().
8275
MarshalBinaryTo([]byte) error
8376

8477
UnmarshalBinary([]byte) error
8578
}
8679

87-
// RawExtension implements the Extension interface
80+
// RawExtension implements the Extension interface.
8881
type RawExtension struct {
8982
Data []byte
9083
Type int8
@@ -93,18 +86,17 @@ type RawExtension struct {
9386
// ExtensionType implements Extension.ExtensionType, and returns r.Type
9487
func (r *RawExtension) ExtensionType() int8 { return r.Type }
9588

96-
// Len implements Extension.Len, and returns len(r.Data)
89+
// Len implements Extension.Len.
9790
func (r *RawExtension) Len() int { return len(r.Data) }
9891

99-
// MarshalBinaryTo implements Extension.MarshalBinaryTo,
100-
// and returns a copy of r.Data
92+
// MarshalBinaryTo implements Extension.MarshalBinaryTo and returns a copy of r.Data.
10193
func (r *RawExtension) MarshalBinaryTo(d []byte) error {
10294
copy(d, r.Data)
10395
return nil
10496
}
10597

106-
// UnmarshalBinary implements Extension.UnmarshalBinary,
107-
// and sets r.Data to the contents of the provided slice
98+
// UnmarshalBinary implements Extension.UnmarshalBinary and sets r.Data to the contents
99+
// of the provided slice.
108100
func (r *RawExtension) UnmarshalBinary(b []byte) error {
109101
if cap(r.Data) >= len(b) {
110102
r.Data = r.Data[0:len(b)]
@@ -115,7 +107,7 @@ func (r *RawExtension) UnmarshalBinary(b []byte) error {
115107
return nil
116108
}
117109

118-
// WriteExtension writes an extension type to the writer
110+
// WriteExtension writes an extension type to the writer.
119111
func (mw *Writer) WriteExtension(e Extension) error {
120112
l := e.Len()
121113
var err error
@@ -218,8 +210,7 @@ func (mw *Writer) WriteExtension(e Extension) error {
218210
return nil
219211
}
220212

221-
// peek at the extension type, assuming the next
222-
// kind to be read is Extension
213+
// peek at the extension type, assuming the next kind to be read is Extension.
223214
func (m *Reader) peekExtensionType() (int8, error) {
224215
p, err := m.R.Peek(2)
225216
if err != nil {
@@ -262,19 +253,16 @@ func peekExtension(b []byte) (int8, error) {
262253
return int8(b[size-1]), nil
263254
}
264255

265-
// ReadExtension reads the next object from the reader
266-
// as an extension. ReadExtension will fail if the next
267-
// object in the stream is not an extension, or if
268-
// e.Type() is not the same as the wire type.
256+
// ReadExtension reads the next object from the reader as an extension. ReadExtension will fail if
257+
// the next object in the stream is not an extension, or if e.Type() is not the same as the wire type.
269258
func (m *Reader) ReadExtension(e Extension) (err error) {
270-
var p []byte
271-
p, err = m.R.Peek(2)
259+
260+
p, err := m.R.Peek(2)
272261
if err != nil {
273262
return
274263
}
275264
lead := p[0]
276-
var read int
277-
var off int
265+
var read, off int
278266
switch lead {
279267
case mfixext1:
280268
if int8(p[1]) != e.ExtensionType() {

msgp/json.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ var (
1616

1717
var defuns [_maxtype]func(jsWriter, *Reader) (int, error)
1818

19-
// note: there is an initialization loop if
20-
// this isn't set up during init()
19+
// Note: there is an initialization loop if this isn't set up during init()
2120
func init() {
22-
// since none of these functions are inline-able,
23-
// there is not much of a penalty to the indirect
24-
// call. however, this is best expressed as a jump-table...
21+
// Since none of these functions are inline-able, there is not much of a
22+
// penalty to the indirect call. However, this is best expressed as a jump-table.
2523
defuns = [_maxtype]func(jsWriter, *Reader) (int, error){
2624
StrType: rwString,
2725
BinType: rwBytes,
@@ -40,26 +38,24 @@ func init() {
4038
}
4139
}
4240

43-
// this is the interface
44-
// used to write json
41+
// jsWriter is the interface used to write JSON.
4542
type jsWriter interface {
4643
io.Writer
4744
io.ByteWriter
4845
WriteString(string) (int, error)
4946
}
5047

51-
// CopyToJSON reads MessagePack from 'src' and copies it
52-
// as JSON to 'dst' until EOF.
48+
// CopyToJSON reads MessagePack from src and copies it as JSON to dst until EOF.
5349
func CopyToJSON(dst io.Writer, src io.Reader) (n int64, err error) {
5450
r := NewReader(src)
5551
n, err = r.WriteToJSON(dst)
56-
freeR(r)
52+
readerPool.Put(r)
5753
return
5854
}
5955

60-
// WriteToJSON translates MessagePack from 'r' and writes it as
61-
// JSON to 'w' until the underlying reader returns io.EOF. It returns
62-
// the number of bytes written, and an error if it stopped before EOF.
56+
// WriteToJSON translates MessagePack from r and writes it as JSON to w until the underlying
57+
// reader returns io.EOF. WriteToJSON returns the number of bytes written, and an error if
58+
// it stopped before EOF.
6359
func (r *Reader) WriteToJSON(w io.Writer) (n int64, err error) {
6460
var j jsWriter
6561
var bf *bufio.Writer
@@ -266,8 +262,7 @@ func rwExtension(dst jsWriter, src *Reader) (n int, err error) {
266262
return 0, err
267263
}
268264

269-
// registered extensions can override
270-
// the JSON encoding
265+
// Registered extensions can override the JSON encoding.
271266
if j, ok := extensionReg[et]; ok {
272267
var bts []byte
273268
e := j()
@@ -410,7 +405,7 @@ func rwBytes(dst jsWriter, src *Reader) (n int, err error) {
410405
// Below (c) The Go Authors, 2009-2014
411406
// Subject to the BSD-style license found at http://golang.org
412407
//
413-
// see: encoding/json/encode.go:(*encodeState).stringbytes()
408+
// See: encoding/json/encode.go:(*encodeState).stringbytes()
414409
func rwquoted(dst jsWriter, s []byte) (n int, err error) {
415410
var nn int
416411
err = dst.WriteByte('"')

0 commit comments

Comments
 (0)