Use the stack for most known sizes.
By using the stack for known sizes, there is less pressure on the garbage collector.
This commit is contained in:
parent
f0581b565c
commit
805ce37d31
3 changed files with 45 additions and 45 deletions
28
common.go
28
common.go
|
@ -283,7 +283,7 @@ func writeElements(w io.Writer, elements ...interface{}) error {
|
|||
|
||||
// readVarInt reads a variable length integer from r and returns it as a uint64.
|
||||
func readVarInt(r io.Reader, pver uint32) (uint64, error) {
|
||||
b := make([]byte, 8)
|
||||
var b [8]byte
|
||||
_, err := io.ReadFull(r, b[0:1])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@ -293,25 +293,25 @@ func readVarInt(r io.Reader, pver uint32) (uint64, error) {
|
|||
discriminant := uint8(b[0])
|
||||
switch discriminant {
|
||||
case 0xff:
|
||||
_, err := io.ReadFull(r, b)
|
||||
_, err := io.ReadFull(r, b[:])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rv = binary.LittleEndian.Uint64(b)
|
||||
rv = binary.LittleEndian.Uint64(b[:])
|
||||
|
||||
case 0xfe:
|
||||
_, err := io.ReadFull(r, b[0:4])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rv = uint64(binary.LittleEndian.Uint32(b))
|
||||
rv = uint64(binary.LittleEndian.Uint32(b[:]))
|
||||
|
||||
case 0xfd:
|
||||
_, err := io.ReadFull(r, b[0:2])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rv = uint64(binary.LittleEndian.Uint16(b))
|
||||
rv = uint64(binary.LittleEndian.Uint16(b[:]))
|
||||
|
||||
default:
|
||||
rv = uint64(discriminant)
|
||||
|
@ -329,25 +329,25 @@ func writeVarInt(w io.Writer, pver uint32, val uint64) error {
|
|||
}
|
||||
|
||||
if val <= math.MaxUint16 {
|
||||
buf := make([]byte, 3)
|
||||
var buf [3]byte
|
||||
buf[0] = 0xfd
|
||||
binary.LittleEndian.PutUint16(buf[1:], uint16(val))
|
||||
_, err := w.Write(buf)
|
||||
_, err := w.Write(buf[:])
|
||||
return err
|
||||
}
|
||||
|
||||
if val <= math.MaxUint32 {
|
||||
buf := make([]byte, 5)
|
||||
var buf [5]byte
|
||||
buf[0] = 0xfe
|
||||
binary.LittleEndian.PutUint32(buf[1:], uint32(val))
|
||||
_, err := w.Write(buf)
|
||||
_, err := w.Write(buf[:])
|
||||
return err
|
||||
}
|
||||
|
||||
buf := make([]byte, 9)
|
||||
var buf [9]byte
|
||||
buf[0] = 0xff
|
||||
binary.LittleEndian.PutUint64(buf[1:], val)
|
||||
_, err := w.Write(buf)
|
||||
_, err := w.Write(buf[:])
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -422,15 +422,15 @@ func writeVarString(w io.Writer, pver uint32, str string) error {
|
|||
// unexported version takes a reader primarily to ensure the error paths
|
||||
// can be properly tested by passing a fake reader in the tests.
|
||||
func randomUint64(r io.Reader) (uint64, error) {
|
||||
b := make([]byte, 8)
|
||||
n, err := r.Read(b)
|
||||
var b [8]byte
|
||||
n, err := r.Read(b[:])
|
||||
if n != len(b) {
|
||||
return 0, io.ErrShortBuffer
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return binary.BigEndian.Uint64(b), nil
|
||||
return binary.BigEndian.Uint64(b[:]), nil
|
||||
}
|
||||
|
||||
// RandomUint64 returns a cryptographically random uint64 value.
|
||||
|
|
|
@ -128,12 +128,12 @@ func readMessageHeader(r io.Reader) (int, *messageHeader, error) {
|
|||
// to read the entire header into a buffer first in case there is a
|
||||
// short read so the proper amount of read bytes are known. This works
|
||||
// since the header is a fixed size.
|
||||
headerBytes := make([]byte, MessageHeaderSize)
|
||||
n, err := io.ReadFull(r, headerBytes)
|
||||
var headerBytes [MessageHeaderSize]byte
|
||||
n, err := io.ReadFull(r, headerBytes[:])
|
||||
if err != nil {
|
||||
return n, nil, err
|
||||
}
|
||||
hr := bytes.NewBuffer(headerBytes)
|
||||
hr := bytes.NewBuffer(headerBytes[:])
|
||||
|
||||
// Create and populate a messageHeader struct from the raw header bytes.
|
||||
hdr := messageHeader{}
|
||||
|
|
56
msgtx.go
56
msgtx.go
|
@ -235,12 +235,12 @@ func (msg *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 {
|
||||
buf := make([]byte, 4)
|
||||
_, err := io.ReadFull(r, buf)
|
||||
var buf [4]byte
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msg.Version = binary.LittleEndian.Uint32(buf)
|
||||
msg.Version = binary.LittleEndian.Uint32(buf[:])
|
||||
|
||||
count, err := readVarInt(r, pver)
|
||||
if err != nil {
|
||||
|
@ -292,11 +292,11 @@ func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error {
|
|||
msg.TxOut[i] = &to
|
||||
}
|
||||
|
||||
_, err = io.ReadFull(r, buf)
|
||||
_, err = io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msg.LockTime = binary.LittleEndian.Uint32(buf)
|
||||
msg.LockTime = binary.LittleEndian.Uint32(buf[:])
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -323,9 +323,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 {
|
||||
buf := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(buf, msg.Version)
|
||||
_, err := w.Write(buf)
|
||||
var buf [4]byte
|
||||
binary.LittleEndian.PutUint32(buf[:], msg.Version)
|
||||
_, err := w.Write(buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -356,8 +356,8 @@ func (msg *MsgTx) BtcEncode(w io.Writer, pver uint32) error {
|
|||
}
|
||||
}
|
||||
|
||||
binary.LittleEndian.PutUint32(buf, msg.LockTime)
|
||||
_, err = w.Write(buf)
|
||||
binary.LittleEndian.PutUint32(buf[:], msg.LockTime)
|
||||
_, err = w.Write(buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -434,12 +434,12 @@ func readOutPoint(r io.Reader, pver uint32, version uint32, op *OutPoint) error
|
|||
return err
|
||||
}
|
||||
|
||||
buf := make([]byte, 4)
|
||||
_, err = io.ReadFull(r, buf)
|
||||
var buf [4]byte
|
||||
_, err = io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
op.Index = binary.LittleEndian.Uint32(buf)
|
||||
op.Index = binary.LittleEndian.Uint32(buf[:])
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -451,9 +451,9 @@ func writeOutPoint(w io.Writer, pver uint32, version uint32, op *OutPoint) error
|
|||
return err
|
||||
}
|
||||
|
||||
buf := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(buf, op.Index)
|
||||
_, err = w.Write(buf)
|
||||
var buf [4]byte
|
||||
binary.LittleEndian.PutUint32(buf[:], op.Index)
|
||||
_, err = w.Write(buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -492,12 +492,12 @@ func readTxIn(r io.Reader, pver uint32, version uint32, ti *TxIn) error {
|
|||
}
|
||||
ti.SignatureScript = b
|
||||
|
||||
b = make([]byte, 4)
|
||||
_, err = io.ReadFull(r, b)
|
||||
var buf [4]byte
|
||||
_, err = io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ti.Sequence = binary.LittleEndian.Uint32(b)
|
||||
ti.Sequence = binary.LittleEndian.Uint32(buf[:])
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -521,9 +521,9 @@ func writeTxIn(w io.Writer, pver uint32, version uint32, ti *TxIn) error {
|
|||
return err
|
||||
}
|
||||
|
||||
buf := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(buf, ti.Sequence)
|
||||
_, err = w.Write(buf)
|
||||
var buf [4]byte
|
||||
binary.LittleEndian.PutUint32(buf[:], ti.Sequence)
|
||||
_, err = w.Write(buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -534,12 +534,12 @@ func writeTxIn(w io.Writer, pver uint32, version uint32, ti *TxIn) error {
|
|||
// readTxOut reads the next sequence of bytes from r as a transaction output
|
||||
// (TxOut).
|
||||
func readTxOut(r io.Reader, pver uint32, version uint32, to *TxOut) error {
|
||||
buf := make([]byte, 8)
|
||||
_, err := io.ReadFull(r, buf)
|
||||
var buf [8]byte
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
to.Value = int64(binary.LittleEndian.Uint64(buf))
|
||||
to.Value = int64(binary.LittleEndian.Uint64(buf[:]))
|
||||
|
||||
count, err := readVarInt(r, pver)
|
||||
if err != nil {
|
||||
|
@ -569,9 +569,9 @@ func readTxOut(r io.Reader, pver uint32, version uint32, to *TxOut) error {
|
|||
// writeTxOut encodes to into the bitcoin protocol encoding for a transaction
|
||||
// output (TxOut) to w.
|
||||
func writeTxOut(w io.Writer, pver uint32, version uint32, to *TxOut) error {
|
||||
buf := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(buf, uint64(to.Value))
|
||||
_, err := w.Write(buf)
|
||||
var buf [8]byte
|
||||
binary.LittleEndian.PutUint64(buf[:], uint64(to.Value))
|
||||
_, err := w.Write(buf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue