diff --git a/data/transactions/transaction.go b/data/transactions/transaction.go index 29594cbdbd..725f9d7ec2 100644 --- a/data/transactions/transaction.go +++ b/data/transactions/transaction.go @@ -196,8 +196,8 @@ var txEncodingPool = sync.Pool{ } // getTxEncodingBuf returns a wrapped byte slice that can be used for encoding a -// temporary message. The byte slice has zero length but potentially -// non-zero capacity. The caller gets full ownership of the byte slice, +// temporary message. The byte slice length of encoded Transaction{} object. +// The caller gets full ownership of the byte slice, // but is encouraged to return it using putEncodingBuf(). func getTxEncodingBuf() *txEncodingBuf { buf := txEncodingPool.Get().(*txEncodingBuf) @@ -221,7 +221,7 @@ func (tx Transaction) ID() Txid { buf := getTxEncodingBuf() enc := tx.MarshalMsg(buf.b) if cap(enc) > cap(buf.b) { - // use a bigger buffer is New's estimate was too small + // use a bigger buffer as New's estimate was too small buf.b = enc } defer putTxEncodingBuf(buf) diff --git a/protocol/codec.go b/protocol/codec.go index 85b59851f2..62abaedad6 100644 --- a/protocol/codec.go +++ b/protocol/codec.go @@ -288,18 +288,21 @@ func (d *MsgpDecoderBytes) Remaining() int { // encodingPool holds temporary byte slice buffers used for encoding messages. var encodingPool = sync.Pool{ New: func() interface{} { - return &EncodingBuf{b: make([]byte, 0, 1024)} + return &EncodingBuf{b: make([]byte, 0)} }, } +// EncodingBuf is a wrapper for a byte slice that can be used for encoding type EncodingBuf struct { b []byte } +// Bytes returns the underlying byte slice func (eb *EncodingBuf) Bytes() []byte { return eb.b } +// Update updates the underlying byte slice to the given one if its capacity exceeds the current one. func (eb *EncodingBuf) Update(v []byte) *EncodingBuf { if cap(eb.b) < cap(v) { eb.b = v