Use bytes.NewReader for deserialize when possible.

Rather than using bytes.NewBuffer, which is a read/write entity
(io.ReadWriter), use bytes.NewReader which is only a read entitiy
(io.Reader) in all cases where it is possible.  Benchmarking shows it's
slightly faster and it's also technically more accurate since it ensures
the data is read-only.

There are a few cases where bytes.NewBuffer must still be used since a
buffer with a known length is required for those instances.
This commit is contained in:
Dave Collins 2014-06-04 23:39:03 -05:00
parent 2054fa7581
commit ee46a0b108
25 changed files with 58 additions and 58 deletions

View file

@ -92,7 +92,7 @@ func BenchmarkWriteVarInt9(b *testing.B) {
func BenchmarkReadVarInt1(b *testing.B) {
buf := []byte{0x01}
for i := 0; i < b.N; i++ {
btcwire.TstReadVarInt(bytes.NewBuffer(buf), 0)
btcwire.TstReadVarInt(bytes.NewReader(buf), 0)
}
}
@ -101,7 +101,7 @@ func BenchmarkReadVarInt1(b *testing.B) {
func BenchmarkReadVarInt3(b *testing.B) {
buf := []byte{0x0fd, 0xff, 0xff}
for i := 0; i < b.N; i++ {
btcwire.TstReadVarInt(bytes.NewBuffer(buf), 0)
btcwire.TstReadVarInt(bytes.NewReader(buf), 0)
}
}
@ -110,7 +110,7 @@ func BenchmarkReadVarInt3(b *testing.B) {
func BenchmarkReadVarInt5(b *testing.B) {
buf := []byte{0xfe, 0xff, 0xff, 0xff, 0xff}
for i := 0; i < b.N; i++ {
btcwire.TstReadVarInt(bytes.NewBuffer(buf), 0)
btcwire.TstReadVarInt(bytes.NewReader(buf), 0)
}
}
@ -119,7 +119,7 @@ func BenchmarkReadVarInt5(b *testing.B) {
func BenchmarkReadVarInt9(b *testing.B) {
buf := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
for i := 0; i < b.N; i++ {
btcwire.TstReadVarInt(bytes.NewBuffer(buf), 0)
btcwire.TstReadVarInt(bytes.NewReader(buf), 0)
}
}
@ -128,7 +128,7 @@ func BenchmarkReadVarInt9(b *testing.B) {
func BenchmarkReadVarStr4(b *testing.B) {
buf := []byte{0x04, 't', 'e', 's', 't'}
for i := 0; i < b.N; i++ {
btcwire.TstReadVarString(bytes.NewBuffer(buf), 0)
btcwire.TstReadVarString(bytes.NewReader(buf), 0)
}
}
@ -137,7 +137,7 @@ func BenchmarkReadVarStr4(b *testing.B) {
func BenchmarkReadVarStr10(b *testing.B) {
buf := []byte{0x0a, 't', 'e', 's', 't', '0', '1', '2', '3', '4', '5'}
for i := 0; i < b.N; i++ {
btcwire.TstReadVarString(bytes.NewBuffer(buf), 0)
btcwire.TstReadVarString(bytes.NewReader(buf), 0)
}
}
@ -169,7 +169,7 @@ func BenchmarkReadOutPoint(b *testing.B) {
}
var op btcwire.OutPoint
for i := 0; i < b.N; i++ {
btcwire.TstReadOutPoint(bytes.NewBuffer(buf), 0, 0, &op)
btcwire.TstReadOutPoint(bytes.NewReader(buf), 0, 0, &op)
}
}
@ -205,7 +205,7 @@ func BenchmarkReadTxOut(b *testing.B) {
}
var txOut btcwire.TxOut
for i := 0; i < b.N; i++ {
btcwire.TstReadTxOut(bytes.NewBuffer(buf), 0, 0, &txOut)
btcwire.TstReadTxOut(bytes.NewReader(buf), 0, 0, &txOut)
}
}
@ -233,7 +233,7 @@ func BenchmarkReadTxIn(b *testing.B) {
}
var txIn btcwire.TxIn
for i := 0; i < b.N; i++ {
btcwire.TstReadTxIn(bytes.NewBuffer(buf), 0, 0, &txIn)
btcwire.TstReadTxIn(bytes.NewReader(buf), 0, 0, &txIn)
}
}
@ -278,7 +278,7 @@ func BenchmarkDeserializeTx(b *testing.B) {
}
var tx btcwire.MsgTx
for i := 0; i < b.N; i++ {
tx.Deserialize(bytes.NewBuffer(buf))
tx.Deserialize(bytes.NewReader(buf))
}
}
@ -313,7 +313,7 @@ func BenchmarkReadBlockHeader(b *testing.B) {
}
var header btcwire.BlockHeader
for i := 0; i < b.N; i++ {
btcwire.TstReadBlockHeader(bytes.NewBuffer(buf), 0, &header)
btcwire.TstReadBlockHeader(bytes.NewReader(buf), 0, &header)
}
}

View file

@ -141,7 +141,7 @@ func TestBlockHeaderWire(t *testing.T) {
// Decode the block header from wire format.
var bh btcwire.BlockHeader
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = btcwire.TstReadBlockHeader(rbuf, test.pver, &bh)
if err != nil {
t.Errorf("readBlockHeader #%d error %v", i, err)
@ -215,7 +215,7 @@ func TestBlockHeaderSerialize(t *testing.T) {
// Deserialize the block header.
var bh btcwire.BlockHeader
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = bh.Deserialize(rbuf)
if err != nil {
t.Errorf("Deserialize #%d error %v", i, err)

View file

@ -151,7 +151,7 @@ func TestElementWire(t *testing.T) {
}
// Read from wire format.
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
val := test.in
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
val = reflect.New(reflect.TypeOf(test.in)).Interface()
@ -294,7 +294,7 @@ func TestVarIntWire(t *testing.T) {
}
// Decode from wire format.
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
val, err := btcwire.TstReadVarInt(rbuf, test.pver)
if err != nil {
t.Errorf("readVarInt #%d error %v", i, err)
@ -426,7 +426,7 @@ func TestVarStringWire(t *testing.T) {
}
// Decode from wire format.
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
val, err := btcwire.TstReadVarString(rbuf, test.pver)
if err != nil {
t.Errorf("readVarString #%d error %v", i, err)
@ -508,7 +508,7 @@ func TestVarStringOverflowErrors(t *testing.T) {
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Decode from wire format.
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
_, err := btcwire.TstReadVarString(rbuf, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("readVarString #%d wrong error got: %v, "+
@ -556,7 +556,7 @@ func TestVarBytesWire(t *testing.T) {
}
// Decode from wire format.
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
val, err := btcwire.TstReadVarBytes(rbuf, test.pver,
btcwire.MaxMessagePayload, "test payload")
if err != nil {
@ -640,7 +640,7 @@ func TestVarBytesOverflowErrors(t *testing.T) {
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Decode from wire format.
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
_, err := btcwire.TstReadVarBytes(rbuf, test.pver,
btcwire.MaxMessagePayload, "test payload")
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {

View file

@ -253,7 +253,7 @@ func TestInvVectWire(t *testing.T) {
// Decode the message from wire format.
var iv btcwire.InvVect
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = btcwire.TstReadInvVect(rbuf, test.pver, &iv)
if err != nil {
t.Errorf("readInvVect #%d error %v", i, err)

View file

@ -153,7 +153,7 @@ func readMessageHeader(r io.Reader) (int, *messageHeader, error) {
if err != nil {
return n, nil, err
}
hr := bytes.NewBuffer(headerBytes[:])
hr := bytes.NewReader(headerBytes[:])
// Create and populate a messageHeader struct from the raw header bytes.
hdr := messageHeader{}
@ -346,7 +346,8 @@ func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []
return totalBytes, nil, nil, messageError("ReadMessage", str)
}
// Unmarshal message.
// Unmarshal message. NOTE: This must be a *bytes.Buffer since the
// MsgVersion BtcDecode function requires it.
pr := bytes.NewBuffer(payload)
err = msg.BtcDecode(pr, pver)
if err != nil {

View file

@ -122,7 +122,7 @@ func TestMessage(t *testing.T) {
}
// Decode from wire format.
rbuf := bytes.NewBuffer(buf.Bytes())
rbuf := bytes.NewReader(buf.Bytes())
nr, msg, _, err := btcwire.ReadMessageN(rbuf, test.pver, test.btcnet)
if err != nil {
t.Errorf("ReadMessage #%d error %v, msg %v", i, err,
@ -155,7 +155,7 @@ func TestMessage(t *testing.T) {
}
// Decode from wire format.
rbuf := bytes.NewBuffer(buf.Bytes())
rbuf := bytes.NewReader(buf.Bytes())
msg, _, err := btcwire.ReadMessage(rbuf, test.pver, test.btcnet)
if err != nil {
t.Errorf("ReadMessage #%d error %v, msg %v", i, err,

View file

@ -191,7 +191,7 @@ func TestAddrWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgAddr
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -157,7 +157,7 @@ func TestMsgAlertWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgAlert
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)
@ -440,7 +440,7 @@ func TestAlertErrors(t *testing.T) {
0x55, 0x52, 0x47, 0x45, 0x4e, 0x54, 0x00, //|URGENT.|
}
var alert btcwire.Alert
r := bytes.NewBuffer(badAlertEncoded)
r := bytes.NewReader(badAlertEncoded)
err := alert.Deserialize(r, pver)
if _, ok := err.(*btcwire.MessageError); !ok {
t.Errorf("Alert.Deserialize wrong error got: %T, want: %T",
@ -457,7 +457,7 @@ func TestAlertErrors(t *testing.T) {
0x73, 0x68, 0x69, 0x3a, 0x30, 0x2e, 0x37, 0x2e, 0x32, 0x2f, 0x88, 0x13, 0x00, 0x00, 0x00, 0x06, //|shi:0.7.2/......|
0x55, 0x52, 0x47, 0x45, 0x4e, 0x54, 0x00, //|URGENT.|
}
r = bytes.NewBuffer(badAlertEncoded)
r = bytes.NewReader(badAlertEncoded)
err = alert.Deserialize(r, pver)
if _, ok := err.(*btcwire.MessageError); !ok {
t.Errorf("Alert.Deserialize wrong error got: %T, want: %T",

View file

@ -183,7 +183,7 @@ func TestBlockWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgBlock
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)
@ -287,7 +287,7 @@ func TestBlockSerialize(t *testing.T) {
// Deserialize the block.
var block btcwire.MsgBlock
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = block.Deserialize(rbuf)
if err != nil {
t.Errorf("Deserialize #%d error %v", i, err)
@ -302,8 +302,8 @@ func TestBlockSerialize(t *testing.T) {
// Deserialize the block while gathering transaction location
// information.
var txLocBlock btcwire.MsgBlock
rbuf = bytes.NewBuffer(test.buf)
txLocs, err := txLocBlock.DeserializeTxLoc(rbuf)
br := bytes.NewBuffer(test.buf)
txLocs, err := txLocBlock.DeserializeTxLoc(br)
if err != nil {
t.Errorf("DeserializeTxLoc #%d error %v", i, err)
continue
@ -371,8 +371,8 @@ func TestBlockSerializeErrors(t *testing.T) {
}
var txLocBlock btcwire.MsgBlock
rbuf := bytes.NewBuffer(test.buf[0:test.max])
_, err = txLocBlock.DeserializeTxLoc(rbuf)
br := bytes.NewBuffer(test.buf[0:test.max])
_, err = txLocBlock.DeserializeTxLoc(br)
if err != test.readErr {
t.Errorf("DeserializeTxLoc #%d wrong error got: %v, want: %v",
i, err, test.readErr)
@ -421,7 +421,7 @@ func TestBlockOverflowErrors(t *testing.T) {
for i, test := range tests {
// Decode from wire format.
var msg btcwire.MsgBlock
r := bytes.NewBuffer(test.buf)
r := bytes.NewReader(test.buf)
err := msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
@ -430,7 +430,7 @@ func TestBlockOverflowErrors(t *testing.T) {
}
// Deserialize from wire format.
r = bytes.NewBuffer(test.buf)
r = bytes.NewReader(test.buf)
err = msg.Deserialize(r)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
@ -439,8 +439,8 @@ func TestBlockOverflowErrors(t *testing.T) {
}
// Deserialize with transaction location info from wire format.
r = bytes.NewBuffer(test.buf)
_, err = msg.DeserializeTxLoc(r)
br := bytes.NewBuffer(test.buf)
_, err = msg.DeserializeTxLoc(br)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("DeserializeTxLoc #%d wrong error got: %v, "+
"want: %v", i, err, reflect.TypeOf(test.err))

View file

@ -113,7 +113,7 @@ func TestFilterClearWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgFilterClear
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -119,8 +119,7 @@ func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
0x00, // update Type
}
// Decode with latest protocol version.
buf1 := bytes.NewBuffer(newBuf)
readbuf := bytes.NewReader(buf1.Bytes())
readbuf := bytes.NewReader(newBuf)
err = msg.BtcDecode(readbuf, btcwire.ProtocolVersion)
if err == nil {
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",

View file

@ -107,7 +107,7 @@ func TestGetAddrWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgGetAddr
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -246,7 +246,7 @@ func TestGetBlocksWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgGetBlocks
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -217,7 +217,7 @@ func TestGetDataWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgGetData
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -236,7 +236,7 @@ func TestGetHeadersWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgGetHeaders
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -199,7 +199,7 @@ func TestHeadersWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgHeaders
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -217,7 +217,7 @@ func TestInvWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgInv
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -176,7 +176,7 @@ func TestMerkleBlockWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgMerkleBlock
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -208,7 +208,7 @@ func TestNotFoundWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgNotFound
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -180,7 +180,7 @@ func TestPingWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgPing
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -189,7 +189,7 @@ func TestPongWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgPong
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -284,7 +284,7 @@ func TestRejectWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgReject
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -287,7 +287,7 @@ func TestTxWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgTx
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)
@ -416,7 +416,7 @@ func TestTxSerialize(t *testing.T) {
// Deserialize the transaction.
var tx btcwire.MsgTx
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = tx.Deserialize(rbuf)
if err != nil {
t.Errorf("Deserialize #%d error %v", i, err)
@ -568,7 +568,7 @@ func TestTxOverflowErrors(t *testing.T) {
for i, test := range tests {
// Decode from wire format.
var msg btcwire.MsgTx
r := bytes.NewBuffer(test.buf)
r := bytes.NewReader(test.buf)
err := msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
@ -577,7 +577,7 @@ func TestTxOverflowErrors(t *testing.T) {
}
// Decode from wire format.
r = bytes.NewBuffer(test.buf)
r = bytes.NewReader(test.buf)
err = msg.Deserialize(r)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("Deserialize #%d wrong error got: %v, want: %v",

View file

@ -106,7 +106,7 @@ func TestVerAckWire(t *testing.T) {
// Decode the message from wire format.
var msg btcwire.MsgVerAck
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)

View file

@ -201,7 +201,7 @@ func TestNetAddressWire(t *testing.T) {
// Decode the message from wire format.
var na btcwire.NetAddress
rbuf := bytes.NewBuffer(test.buf)
rbuf := bytes.NewReader(test.buf)
err = btcwire.TstReadNetAddress(rbuf, test.pver, &na, test.ts)
if err != nil {
t.Errorf("readNetAddress #%d error %v", i, err)