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 b7b700fd5a:

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.
This commit is contained in:
Dave Collins 2013-11-06 17:43:44 -06:00
parent fe713b8013
commit a81e081cc4

View file

@ -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 // See Serialize for encoding transactions to be stored to disk, such as in a
// database, as opposed to encoding transactions for the wire. // database, as opposed to encoding transactions for the wire.
func (msg *MsgTx) BtcEncode(w io.Writer, pver uint32) error { 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 { if err != nil {
return err 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 { if err != nil {
return err return err
} }