Preallocate the tx serialization buffer.

Benchmarking shows this is slightly faster due to avoiding the extra
garbage collection in addition to less peak memory usage.

ok @davecgh
This commit is contained in:
David Hill 2014-03-20 15:33:09 -04:00
parent f1a4dfb86d
commit eecea4180a

View file

@ -549,9 +549,9 @@ func (*wsNotificationManager) removeSpentRequest(ops map[btcwire.OutPoint]map[ch
// txHexString returns the serialized transaction encoded in hexadecimal.
func txHexString(tx *btcutil.Tx) string {
var buf bytes.Buffer
buf := bytes.NewBuffer(make([]byte, 0, tx.MsgTx().SerializeSize()))
// Ignore Serialize's error, as writing to a bytes.buffer cannot fail.
tx.MsgTx().Serialize(&buf)
tx.MsgTx().Serialize(buf)
return hex.EncodeToString(buf.Bytes())
}