mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-09-03 02:35:15 +00:00
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.
|
// readVarInt reads a variable length integer from r and returns it as a uint64.
|
||||||
func readVarInt(r io.Reader, pver uint32) (uint64, error) {
|
func readVarInt(r io.Reader, pver uint32) (uint64, error) {
|
||||||
b := make([]byte, 8)
|
var b [8]byte
|
||||||
_, err := io.ReadFull(r, b[0:1])
|
_, err := io.ReadFull(r, b[0:1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -293,25 +293,25 @@ func readVarInt(r io.Reader, pver uint32) (uint64, error) {
|
||||||
discriminant := uint8(b[0])
|
discriminant := uint8(b[0])
|
||||||
switch discriminant {
|
switch discriminant {
|
||||||
case 0xff:
|
case 0xff:
|
||||||
_, err := io.ReadFull(r, b)
|
_, err := io.ReadFull(r, b[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
rv = binary.LittleEndian.Uint64(b)
|
rv = binary.LittleEndian.Uint64(b[:])
|
||||||
|
|
||||||
case 0xfe:
|
case 0xfe:
|
||||||
_, err := io.ReadFull(r, b[0:4])
|
_, err := io.ReadFull(r, b[0:4])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
rv = uint64(binary.LittleEndian.Uint32(b))
|
rv = uint64(binary.LittleEndian.Uint32(b[:]))
|
||||||
|
|
||||||
case 0xfd:
|
case 0xfd:
|
||||||
_, err := io.ReadFull(r, b[0:2])
|
_, err := io.ReadFull(r, b[0:2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
rv = uint64(binary.LittleEndian.Uint16(b))
|
rv = uint64(binary.LittleEndian.Uint16(b[:]))
|
||||||
|
|
||||||
default:
|
default:
|
||||||
rv = uint64(discriminant)
|
rv = uint64(discriminant)
|
||||||
|
@ -329,25 +329,25 @@ func writeVarInt(w io.Writer, pver uint32, val uint64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if val <= math.MaxUint16 {
|
if val <= math.MaxUint16 {
|
||||||
buf := make([]byte, 3)
|
var buf [3]byte
|
||||||
buf[0] = 0xfd
|
buf[0] = 0xfd
|
||||||
binary.LittleEndian.PutUint16(buf[1:], uint16(val))
|
binary.LittleEndian.PutUint16(buf[1:], uint16(val))
|
||||||
_, err := w.Write(buf)
|
_, err := w.Write(buf[:])
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if val <= math.MaxUint32 {
|
if val <= math.MaxUint32 {
|
||||||
buf := make([]byte, 5)
|
var buf [5]byte
|
||||||
buf[0] = 0xfe
|
buf[0] = 0xfe
|
||||||
binary.LittleEndian.PutUint32(buf[1:], uint32(val))
|
binary.LittleEndian.PutUint32(buf[1:], uint32(val))
|
||||||
_, err := w.Write(buf)
|
_, err := w.Write(buf[:])
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 9)
|
var buf [9]byte
|
||||||
buf[0] = 0xff
|
buf[0] = 0xff
|
||||||
binary.LittleEndian.PutUint64(buf[1:], val)
|
binary.LittleEndian.PutUint64(buf[1:], val)
|
||||||
_, err := w.Write(buf)
|
_, err := w.Write(buf[:])
|
||||||
return err
|
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
|
// unexported version takes a reader primarily to ensure the error paths
|
||||||
// can be properly tested by passing a fake reader in the tests.
|
// can be properly tested by passing a fake reader in the tests.
|
||||||
func randomUint64(r io.Reader) (uint64, error) {
|
func randomUint64(r io.Reader) (uint64, error) {
|
||||||
b := make([]byte, 8)
|
var b [8]byte
|
||||||
n, err := r.Read(b)
|
n, err := r.Read(b[:])
|
||||||
if n != len(b) {
|
if n != len(b) {
|
||||||
return 0, io.ErrShortBuffer
|
return 0, io.ErrShortBuffer
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
return binary.BigEndian.Uint64(b), nil
|
return binary.BigEndian.Uint64(b[:]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomUint64 returns a cryptographically random uint64 value.
|
// 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
|
// 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
|
// short read so the proper amount of read bytes are known. This works
|
||||||
// since the header is a fixed size.
|
// since the header is a fixed size.
|
||||||
headerBytes := make([]byte, MessageHeaderSize)
|
var headerBytes [MessageHeaderSize]byte
|
||||||
n, err := io.ReadFull(r, headerBytes)
|
n, err := io.ReadFull(r, headerBytes[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, nil, err
|
return n, nil, err
|
||||||
}
|
}
|
||||||
hr := bytes.NewBuffer(headerBytes)
|
hr := bytes.NewBuffer(headerBytes[:])
|
||||||
|
|
||||||
// Create and populate a messageHeader struct from the raw header bytes.
|
// Create and populate a messageHeader struct from the raw header bytes.
|
||||||
hdr := messageHeader{}
|
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
|
// See Deserialize for decoding transactions stored to disk, such as in a
|
||||||
// database, as opposed to decoding transactions from the wire.
|
// database, as opposed to decoding transactions from the wire.
|
||||||
func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error {
|
func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error {
|
||||||
buf := make([]byte, 4)
|
var buf [4]byte
|
||||||
_, err := io.ReadFull(r, buf)
|
_, err := io.ReadFull(r, buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
msg.Version = binary.LittleEndian.Uint32(buf)
|
msg.Version = binary.LittleEndian.Uint32(buf[:])
|
||||||
|
|
||||||
count, err := readVarInt(r, pver)
|
count, err := readVarInt(r, pver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -292,11 +292,11 @@ func (msg *MsgTx) BtcDecode(r io.Reader, pver uint32) error {
|
||||||
msg.TxOut[i] = &to
|
msg.TxOut[i] = &to
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = io.ReadFull(r, buf)
|
_, err = io.ReadFull(r, buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
msg.LockTime = binary.LittleEndian.Uint32(buf)
|
msg.LockTime = binary.LittleEndian.Uint32(buf[:])
|
||||||
|
|
||||||
return nil
|
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
|
// 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 {
|
||||||
buf := make([]byte, 4)
|
var buf [4]byte
|
||||||
binary.LittleEndian.PutUint32(buf, msg.Version)
|
binary.LittleEndian.PutUint32(buf[:], msg.Version)
|
||||||
_, err := w.Write(buf)
|
_, err := w.Write(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -356,8 +356,8 @@ func (msg *MsgTx) BtcEncode(w io.Writer, pver uint32) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
binary.LittleEndian.PutUint32(buf, msg.LockTime)
|
binary.LittleEndian.PutUint32(buf[:], msg.LockTime)
|
||||||
_, err = w.Write(buf)
|
_, err = w.Write(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -434,12 +434,12 @@ func readOutPoint(r io.Reader, pver uint32, version uint32, op *OutPoint) error
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 4)
|
var buf [4]byte
|
||||||
_, err = io.ReadFull(r, buf)
|
_, err = io.ReadFull(r, buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
op.Index = binary.LittleEndian.Uint32(buf)
|
op.Index = binary.LittleEndian.Uint32(buf[:])
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,9 +451,9 @@ func writeOutPoint(w io.Writer, pver uint32, version uint32, op *OutPoint) error
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 4)
|
var buf [4]byte
|
||||||
binary.LittleEndian.PutUint32(buf, op.Index)
|
binary.LittleEndian.PutUint32(buf[:], op.Index)
|
||||||
_, err = w.Write(buf)
|
_, err = w.Write(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -492,12 +492,12 @@ func readTxIn(r io.Reader, pver uint32, version uint32, ti *TxIn) error {
|
||||||
}
|
}
|
||||||
ti.SignatureScript = b
|
ti.SignatureScript = b
|
||||||
|
|
||||||
b = make([]byte, 4)
|
var buf [4]byte
|
||||||
_, err = io.ReadFull(r, b)
|
_, err = io.ReadFull(r, buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ti.Sequence = binary.LittleEndian.Uint32(b)
|
ti.Sequence = binary.LittleEndian.Uint32(buf[:])
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -521,9 +521,9 @@ func writeTxIn(w io.Writer, pver uint32, version uint32, ti *TxIn) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 4)
|
var buf [4]byte
|
||||||
binary.LittleEndian.PutUint32(buf, ti.Sequence)
|
binary.LittleEndian.PutUint32(buf[:], ti.Sequence)
|
||||||
_, err = w.Write(buf)
|
_, err = w.Write(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
// readTxOut reads the next sequence of bytes from r as a transaction output
|
||||||
// (TxOut).
|
// (TxOut).
|
||||||
func readTxOut(r io.Reader, pver uint32, version uint32, to *TxOut) error {
|
func readTxOut(r io.Reader, pver uint32, version uint32, to *TxOut) error {
|
||||||
buf := make([]byte, 8)
|
var buf [8]byte
|
||||||
_, err := io.ReadFull(r, buf)
|
_, err := io.ReadFull(r, buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
to.Value = int64(binary.LittleEndian.Uint64(buf))
|
to.Value = int64(binary.LittleEndian.Uint64(buf[:]))
|
||||||
|
|
||||||
count, err := readVarInt(r, pver)
|
count, err := readVarInt(r, pver)
|
||||||
if err != nil {
|
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
|
// writeTxOut encodes to into the bitcoin protocol encoding for a transaction
|
||||||
// output (TxOut) to w.
|
// output (TxOut) to w.
|
||||||
func writeTxOut(w io.Writer, pver uint32, version uint32, to *TxOut) error {
|
func writeTxOut(w io.Writer, pver uint32, version uint32, to *TxOut) error {
|
||||||
buf := make([]byte, 8)
|
var buf [8]byte
|
||||||
binary.LittleEndian.PutUint64(buf, uint64(to.Value))
|
binary.LittleEndian.PutUint64(buf[:], uint64(to.Value))
|
||||||
_, err := w.Write(buf)
|
_, err := w.Write(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue