From 9b7e3527b0a87c6b0d353ff45642cb560d2440d1 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 6 Nov 2013 18:41:41 -0600 Subject: [PATCH] Optimize transaction Deserialize. The benchmark results for the current commit: Before: BenchmarkDeserializeTx 500000 4018 ns/op After: BenchmarkDeserializeTx 500000 3780 ns/op The cumulative benchmark results since commit b7b700fd5aebb86addc93efecaa367a2f4976058: Before: BenchmarkDeserializeTx 200000 10665 ns/op After: BenchmarkDeserializeTx 500000 3780 ns/op This is part of the ongoing effort to optimize serialization as noted in conformal/btcd#27. --- msgtx.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/msgtx.go b/msgtx.go index 1c73b833..7e0ac38b 100644 --- a/msgtx.go +++ b/msgtx.go @@ -229,10 +229,12 @@ func (tx *MsgTx) Copy() *MsgTx { // See Deserialize for decoding transactions stored to disk, such as in a // database, as opposed to decoding transactions from the wire. func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error { - err := readElement(r, &msg.Version) + buf := make([]byte, 4) + _, err := io.ReadFull(r, buf) if err != nil { return err } + msg.Version = binary.LittleEndian.Uint32(buf) count, err := readVarInt(r, pver) if err != nil { @@ -249,14 +251,14 @@ func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgTx.BtcDecode", str) } - msg.TxIn = make([]*TxIn, 0, count) + msg.TxIn = make([]*TxIn, count) for i := uint64(0); i < count; i++ { ti := TxIn{} err = readTxIn(r, pver, msg.Version, &ti) if err != nil { return err } - msg.TxIn = append(msg.TxIn, &ti) + msg.TxIn[i] = &ti } count, err = readVarInt(r, pver) @@ -274,20 +276,21 @@ func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgTx.BtcDecode", str) } - msg.TxOut = make([]*TxOut, 0, count) + msg.TxOut = make([]*TxOut, count) for i := uint64(0); i < count; i++ { to := TxOut{} err = readTxOut(r, pver, msg.Version, &to) if err != nil { return err } - msg.TxOut = append(msg.TxOut, &to) + msg.TxOut[i] = &to } - err = readElement(r, &msg.LockTime) + _, err = io.ReadFull(r, buf) if err != nil { return err } + msg.LockTime = binary.LittleEndian.Uint32(buf) return nil }