From eecea4180a420eda6baa4ee91ccc5ddc8bb0b0a6 Mon Sep 17 00:00:00 2001 From: David Hill Date: Thu, 20 Mar 2014 15:33:09 -0400 Subject: [PATCH] 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 --- rpcwebsocket.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpcwebsocket.go b/rpcwebsocket.go index d32bf774..15120eb0 100644 --- a/rpcwebsocket.go +++ b/rpcwebsocket.go @@ -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()) }