diff --git a/wire/msgaddr.go b/wire/msgaddr.go index a387c8de..89bfaf92 100644 --- a/wire/msgaddr.go +++ b/wire/msgaddr.go @@ -71,14 +71,15 @@ func (msg *MsgAddr) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgAddr.BtcDecode", str) } + addrList := make([]NetAddress, count) msg.AddrList = make([]*NetAddress, 0, count) for i := uint64(0); i < count; i++ { - na := NetAddress{} - err := readNetAddress(r, pver, &na, true) + na := &addrList[i] + err := readNetAddress(r, pver, na, true) if err != nil { return err } - msg.AddAddress(&na) + msg.AddAddress(na) } return nil } diff --git a/wire/msggetblocks.go b/wire/msggetblocks.go index 50459b09..d5b9e222 100644 --- a/wire/msggetblocks.go +++ b/wire/msggetblocks.go @@ -68,14 +68,17 @@ func (msg *MsgGetBlocks) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgGetBlocks.BtcDecode", str) } + // Create a contiguous slice of hashes to deserialize into in order to + // reduce the number of allocations. + locatorHashes := make([]chainhash.Hash, count) msg.BlockLocatorHashes = make([]*chainhash.Hash, 0, count) for i := uint64(0); i < count; i++ { - sha := chainhash.Hash{} - err := readElement(r, &sha) + hash := &locatorHashes[i] + err := readElement(r, hash) if err != nil { return err } - msg.AddBlockLocatorHash(&sha) + msg.AddBlockLocatorHash(hash) } err = readElement(r, &msg.HashStop) diff --git a/wire/msggetdata.go b/wire/msggetdata.go index 7db90177..cebd1862 100644 --- a/wire/msggetdata.go +++ b/wire/msggetdata.go @@ -50,14 +50,17 @@ func (msg *MsgGetData) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgGetData.BtcDecode", str) } + // Create a contiguous slice of inventory vectors to deserialize into in + // order to reduce the number of allocations. + invList := make([]InvVect, count) msg.InvList = make([]*InvVect, 0, count) for i := uint64(0); i < count; i++ { - iv := InvVect{} - err := readInvVect(r, pver, &iv) + iv := &invList[i] + err := readInvVect(r, pver, iv) if err != nil { return err } - msg.AddInvVect(&iv) + msg.AddInvVect(iv) } return nil diff --git a/wire/msggetheaders.go b/wire/msggetheaders.go index 0eec682b..e536ed39 100644 --- a/wire/msggetheaders.go +++ b/wire/msggetheaders.go @@ -65,14 +65,17 @@ func (msg *MsgGetHeaders) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgGetHeaders.BtcDecode", str) } + // Create a contiguous slice of hashes to deserialize into in order to + // reduce the number of allocations. + locatorHashes := make([]chainhash.Hash, count) msg.BlockLocatorHashes = make([]*chainhash.Hash, 0, count) for i := uint64(0); i < count; i++ { - sha := chainhash.Hash{} - err := readElement(r, &sha) + hash := &locatorHashes[i] + err := readElement(r, hash) if err != nil { return err } - msg.AddBlockLocatorHash(&sha) + msg.AddBlockLocatorHash(hash) } err = readElement(r, &msg.HashStop) diff --git a/wire/msgheaders.go b/wire/msgheaders.go index cdae6048..e8153b60 100644 --- a/wire/msgheaders.go +++ b/wire/msgheaders.go @@ -50,10 +50,13 @@ func (msg *MsgHeaders) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgHeaders.BtcDecode", str) } + // Create a contiguous slice of headers to deserialize into in order to + // reduce the number of allocations. + headers := make([]BlockHeader, count) msg.Headers = make([]*BlockHeader, 0, count) for i := uint64(0); i < count; i++ { - bh := BlockHeader{} - err := readBlockHeader(r, pver, &bh) + bh := &headers[i] + err := readBlockHeader(r, pver, bh) if err != nil { return err } @@ -69,7 +72,7 @@ func (msg *MsgHeaders) BtcDecode(r io.Reader, pver uint32) error { "transactions [count %v]", txCount) return messageError("MsgHeaders.BtcDecode", str) } - msg.AddBlockHeader(&bh) + msg.AddBlockHeader(bh) } return nil diff --git a/wire/msginv.go b/wire/msginv.go index 1a463741..6cc84f57 100644 --- a/wire/msginv.go +++ b/wire/msginv.go @@ -58,14 +58,17 @@ func (msg *MsgInv) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgInv.BtcDecode", str) } + // Create a contiguous slice of inventory vectors to deserialize into in + // order to reduce the number of allocations. + invList := make([]InvVect, count) msg.InvList = make([]*InvVect, 0, count) for i := uint64(0); i < count; i++ { - iv := InvVect{} - err := readInvVect(r, pver, &iv) + iv := &invList[i] + err := readInvVect(r, pver, iv) if err != nil { return err } - msg.AddInvVect(&iv) + msg.AddInvVect(iv) } return nil diff --git a/wire/msgmerkleblock.go b/wire/msgmerkleblock.go index a95744f6..45373d51 100644 --- a/wire/msgmerkleblock.go +++ b/wire/msgmerkleblock.go @@ -79,14 +79,17 @@ func (msg *MsgMerkleBlock) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgMerkleBlock.BtcDecode", str) } + // Create a contiguous slice of hashes to deserialize into in order to + // reduce the number of allocations. + hashes := make([]chainhash.Hash, count) msg.Hashes = make([]*chainhash.Hash, 0, count) for i := uint64(0); i < count; i++ { - var sha chainhash.Hash - err := readElement(r, &sha) + hash := &hashes[i] + err := readElement(r, hash) if err != nil { return err } - msg.AddTxHash(&sha) + msg.AddTxHash(hash) } err = readElement(r, &msg.STransactions) @@ -105,14 +108,15 @@ func (msg *MsgMerkleBlock) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgMerkleBlock.BtcDecode", str) } + hashes = make([]chainhash.Hash, count) msg.SHashes = make([]*chainhash.Hash, 0, scount) for i := uint64(0); i < scount; i++ { - var sha chainhash.Hash - err := readElement(r, &sha) + hash := &hashes[i] + err := readElement(r, hash) if err != nil { return err } - msg.AddSTxHash(&sha) + msg.AddSTxHash(hash) } msg.Flags, err = ReadVarBytes(r, pver, maxFlagsPerMerkleBlock, diff --git a/wire/msgnotfound.go b/wire/msgnotfound.go index 2d737792..89e9443a 100644 --- a/wire/msgnotfound.go +++ b/wire/msgnotfound.go @@ -47,14 +47,17 @@ func (msg *MsgNotFound) BtcDecode(r io.Reader, pver uint32) error { return messageError("MsgNotFound.BtcDecode", str) } + // Create a contiguous slice of inventory vectors to deserialize into in + // order to reduce the number of allocations. + invList := make([]InvVect, count) msg.InvList = make([]*InvVect, 0, count) for i := uint64(0); i < count; i++ { - iv := InvVect{} - err := readInvVect(r, pver, &iv) + iv := &invList[i] + err := readInvVect(r, pver, iv) if err != nil { return err } - msg.AddInvVect(&iv) + msg.AddInvVect(iv) } return nil diff --git a/wire/msgtx.go b/wire/msgtx.go index c03ef5e2..ec5e55d1 100644 --- a/wire/msgtx.go +++ b/wire/msgtx.go @@ -573,14 +573,15 @@ func (msg *MsgTx) decodePrefix(r io.Reader, pver uint32) error { } // TxIns. + txIns := make([]TxIn, count) msg.TxIn = make([]*TxIn, count) for i := uint64(0); i < count; i++ { - ti := TxIn{} - err = readTxInPrefix(r, pver, msg.Version, &ti) + ti := &txIns[i] + err = readTxInPrefix(r, pver, msg.Version, ti) if err != nil { return err } - msg.TxIn[i] = &ti + msg.TxIn[i] = ti } count, err = ReadVarInt(r, pver) @@ -599,14 +600,15 @@ func (msg *MsgTx) decodePrefix(r io.Reader, pver uint32) error { } // TxOuts. + txOuts := make([]TxOut, count) msg.TxOut = make([]*TxOut, count) for i := uint64(0); i < count; i++ { - to := TxOut{} - err = readTxOut(r, pver, msg.Version, &to) + to := &txOuts[i] + err = readTxOut(r, pver, msg.Version, to) if err != nil { return err } - msg.TxOut[i] = &to + msg.TxOut[i] = to } // Locktime and expiry. @@ -642,14 +644,15 @@ func (msg *MsgTx) decodeWitness(r io.Reader, pver uint32, isFull bool) error { return messageError("MsgTx.decodeWitness", str) } + txIns := make([]TxIn, count) msg.TxIn = make([]*TxIn, count) for i := uint64(0); i < count; i++ { - ti := TxIn{} - err = readTxInWitness(r, pver, msg.Version, &ti) + ti := &txIns[i] + err = readTxInWitness(r, pver, msg.Version, ti) if err != nil { return err } - msg.TxIn[i] = &ti + msg.TxIn[i] = ti } msg.TxOut = make([]*TxOut, 0) } else { @@ -683,9 +686,10 @@ func (msg *MsgTx) decodeWitness(r io.Reader, pver uint32, isFull bool) error { // Read in the witnesses, and copy them into the already generated // by decodePrefix TxIns. + txIns := make([]TxIn, count) for i := uint64(0); i < count; i++ { - ti := TxIn{} - err = readTxInWitness(r, pver, msg.Version, &ti) + ti := &txIns[i] + err = readTxInWitness(r, pver, msg.Version, ti) if err != nil { return err } @@ -719,14 +723,15 @@ func (msg *MsgTx) decodeWitnessSigning(r io.Reader, pver uint32) error { return messageError("MsgTx.decodeWitness", str) } + txIns := make([]TxIn, count) msg.TxIn = make([]*TxIn, count) for i := uint64(0); i < count; i++ { - ti := TxIn{} - err = readTxInWitnessSigning(r, pver, msg.Version, &ti) + ti := &txIns[i] + err = readTxInWitnessSigning(r, pver, msg.Version, ti) if err != nil { return err } - msg.TxIn[i] = &ti + msg.TxIn[i] = ti } msg.TxOut = make([]*TxOut, 0) @@ -752,14 +757,15 @@ func (msg *MsgTx) decodeWitnessValueSigning(r io.Reader, pver uint32) error { return messageError("MsgTx.decodeWitness", str) } + txIns := make([]TxIn, count) msg.TxIn = make([]*TxIn, count) for i := uint64(0); i < count; i++ { - ti := TxIn{} - err = readTxInWitnessValueSigning(r, pver, msg.Version, &ti) + ti := &txIns[i] + err = readTxInWitnessValueSigning(r, pver, msg.Version, ti) if err != nil { return err } - msg.TxIn[i] = &ti + msg.TxIn[i] = ti } msg.TxOut = make([]*TxOut, 0) @@ -843,14 +849,16 @@ func (msg *MsgTx) LegacyBtcDecode(r io.Reader, pver uint32) error { return messageError("MsgTx.BtcDecode", str) } + // Deserialize the inputs. + txIns := make([]TxIn, count) msg.TxIn = make([]*TxIn, count) for i := uint64(0); i < count; i++ { - ti := TxIn{} - err = legacyReadTxIn(r, pver, msg.Version, &ti) + ti := &txIns[i] + err = legacyReadTxIn(r, pver, msg.Version, ti) if err != nil { return err } - msg.TxIn[i] = &ti + msg.TxIn[i] = ti } count, err = ReadVarInt(r, pver) @@ -868,14 +876,16 @@ func (msg *MsgTx) LegacyBtcDecode(r io.Reader, pver uint32) error { return messageError("MsgTx.BtcDecode", str) } + // Deserialize the outputs. + txOuts := make([]TxOut, count) msg.TxOut = make([]*TxOut, count) for i := uint64(0); i < count; i++ { - to := TxOut{} - err = legacyReadTxOut(r, pver, msg.Version, &to) + to := &txOuts[i] + err = legacyReadTxOut(r, pver, msg.Version, to) if err != nil { return err } - msg.TxOut[i] = &to + msg.TxOut[i] = to } msg.LockTime, err = binarySerializer.Uint32(r, littleEndian)