From a81e081cc48935edf08ca060e9bbbd2c08502854 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 6 Nov 2013 17:43:44 -0600 Subject: [PATCH] Optimize transaction Serialize. The benchmark results for the current commit: Before: BenchmarkSerializeTx 1000000 1233 ns/op After: BenchmarkSerializeTx 1000000 1083 ns/op The cumulative benchmark results since commit b7b700fd5aebb86addc93efecaa367a2f4976058: Before: BenchmarkSerializeTx 1000000 5437 ns/op After: BenchmarkSerializeTx 1000000 1083 ns/op This is part of the ongoing effort to optimize serialization as noted in conformal/btcd#27. --- msgtx.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/msgtx.go b/msgtx.go index 182fb7eb..1c73b833 100644 --- a/msgtx.go +++ b/msgtx.go @@ -314,7 +314,9 @@ func (msg *MsgTx) Deserialize(r io.Reader) error { // See Serialize for encoding transactions to be stored to disk, such as in a // database, as opposed to encoding transactions for the wire. func (msg *MsgTx) BtcEncode(w io.Writer, pver uint32) error { - err := writeElement(w, msg.Version) + buf := make([]byte, 4) + binary.LittleEndian.PutUint32(buf, msg.Version) + _, err := w.Write(buf) if err != nil { return err } @@ -345,7 +347,8 @@ func (msg *MsgTx) BtcEncode(w io.Writer, pver uint32) error { } } - err = writeElement(w, msg.LockTime) + binary.LittleEndian.PutUint32(buf, msg.LockTime) + _, err = w.Write(buf) if err != nil { return err }