lbcd/wire/common.go

652 lines
17 KiB
Go
Raw Normal View History

// Copyright (c) 2013-2016 The btcsuite developers
2013-05-08 21:31:00 +02:00
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
2013-05-08 21:31:00 +02:00
import (
"crypto/rand"
"encoding/binary"
"fmt"
2013-05-08 21:31:00 +02:00
"io"
"math"
"time"
2014-07-03 02:43:33 +02:00
"github.com/lbryio/lbcd/chaincfg/chainhash"
2013-05-08 21:31:00 +02:00
)
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
const (
// MaxVarIntPayload is the maximum payload size for a variable length integer.
MaxVarIntPayload = 9
)
var (
// littleEndian is a convenience variable since binary.LittleEndian is
// quite long.
littleEndian = binary.LittleEndian
// bigEndian is a convenience variable since binary.BigEndian is quite
// long.
bigEndian = binary.BigEndian
)
// binaryFreeList defines a concurrent safe free list of byte slices (up to the
// maximum number defined by the binaryFreeListMaxItems constant) that have a
// cap of 8 (thus it supports up to a uint64). It is used to provide temporary
// buffers for serializing and deserializing primitive numbers to and from their
// binary encoding in order to greatly reduce the number of allocations
// required.
//
// For convenience, functions are provided for each of the primitive unsigned
// integers that automatically obtain a buffer from the free list, perform the
// necessary binary conversion, read from or write to the given io.Reader or
// io.Writer, and return the buffer to the free list.
type binaryFreeList chan []byte
// Uint8 reads a single byte from the provided reader using a buffer from the
// free list and returns it as a uint8.
func (l binaryFreeList) Uint8(r io.Reader) (uint8, error) {
buf := make([]byte, 1) // should be allocated on the stack
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
if _, err := io.ReadFull(r, buf); err != nil {
return 0, err
}
rv := buf[0]
return rv, nil
}
// Uint16 reads two bytes from the provided reader using a buffer from the
// free list, converts it to a number using the provided byte order, and returns
// the resulting uint16.
func (l binaryFreeList) Uint16(r io.Reader, byteOrder binary.ByteOrder) (uint16, error) {
buf := make([]byte, 2) // should be allocated on the stack
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
if _, err := io.ReadFull(r, buf); err != nil {
return 0, err
}
rv := byteOrder.Uint16(buf)
return rv, nil
}
// Uint32 reads four bytes from the provided reader using a buffer from the
// free list, converts it to a number using the provided byte order, and returns
// the resulting uint32.
func (l binaryFreeList) Uint32(r io.Reader, byteOrder binary.ByteOrder) (uint32, error) {
buf := make([]byte, 4) // should be allocated on the stack
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
if _, err := io.ReadFull(r, buf); err != nil {
return 0, err
}
rv := byteOrder.Uint32(buf)
return rv, nil
}
// Uint64 reads eight bytes from the provided reader using a buffer from the
// free list, converts it to a number using the provided byte order, and returns
// the resulting uint64.
func (l binaryFreeList) Uint64(r io.Reader, byteOrder binary.ByteOrder) (uint64, error) {
buf := make([]byte, 8) // should be allocated on the stack
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
if _, err := io.ReadFull(r, buf); err != nil {
return 0, err
}
rv := byteOrder.Uint64(buf)
return rv, nil
}
// PutUint8 copies the provided uint8 into a buffer from the free list and
// writes the resulting byte to the given writer.
func (l binaryFreeList) PutUint8(w io.Writer, val uint8) error {
buf := make([]byte, 1) // should be allocated on the stack
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
buf[0] = val
_, err := w.Write(buf)
return err
}
// PutUint16 serializes the provided uint16 using the given byte order into a
// buffer from the free list and writes the resulting two bytes to the given
// writer.
func (l binaryFreeList) PutUint16(w io.Writer, byteOrder binary.ByteOrder, val uint16) error {
buf := make([]byte, 2) // should be allocated on the stack
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
byteOrder.PutUint16(buf, val)
_, err := w.Write(buf)
return err
}
// PutUint32 serializes the provided uint32 using the given byte order into a
// buffer from the free list and writes the resulting four bytes to the given
// writer.
func (l binaryFreeList) PutUint32(w io.Writer, byteOrder binary.ByteOrder, val uint32) error {
buf := make([]byte, 4) // should be allocated on the stack
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
byteOrder.PutUint32(buf, val)
_, err := w.Write(buf)
return err
}
// PutUint64 serializes the provided uint64 using the given byte order into a
// buffer from the free list and writes the resulting eight bytes to the given
// writer.
func (l binaryFreeList) PutUint64(w io.Writer, byteOrder binary.ByteOrder, val uint64) error {
buf := make([]byte, 8) // should be allocated on the stack
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
byteOrder.PutUint64(buf, val)
_, err := w.Write(buf)
return err
}
// binarySerializer provides a free list of buffers to use for serializing and
// deserializing primitive integer values to and from io.Readers and io.Writers.
var binarySerializer binaryFreeList = make(chan []byte)
2013-05-08 21:31:00 +02:00
// errNonCanonicalVarInt is the common format string used for non-canonically
// encoded variable length integer errors.
var errNonCanonicalVarInt = "non-canonical varint %x - discriminant %x must " +
"encode a value greater than %x"
// uint32Time represents a unix timestamp encoded with a uint32. It is used as
// a way to signal the readElement function how to decode a timestamp into a Go
// time.Time since it is otherwise ambiguous.
type uint32Time time.Time
// int64Time represents a unix timestamp encoded with an int64. It is used as
// a way to signal the readElement function how to decode a timestamp into a Go
// time.Time since it is otherwise ambiguous.
type int64Time time.Time
2013-05-08 21:31:00 +02:00
// readElement reads the next sequence of bytes from r using little endian
// depending on the concrete type of element pointed to.
2013-05-08 21:31:00 +02:00
func readElement(r io.Reader, element interface{}) error {
// Attempt to read the element based on the concrete type via fast
// type assertions first.
switch e := element.(type) {
case *int32:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint32(r, littleEndian)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = int32(rv)
return nil
case *uint32:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint32(r, littleEndian)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = rv
return nil
case *int64:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint64(r, littleEndian)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = int64(rv)
return nil
case *uint64:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint64(r, littleEndian)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = rv
return nil
case *bool:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint8(r)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
if rv == 0x00 {
*e = false
} else {
*e = true
}
return nil
// Unix timestamp encoded as a uint32.
case *uint32Time:
rv, err := binarySerializer.Uint32(r, binary.LittleEndian)
if err != nil {
return err
}
*e = uint32Time(time.Unix(int64(rv), 0))
return nil
// Unix timestamp encoded as an int64.
case *int64Time:
rv, err := binarySerializer.Uint64(r, binary.LittleEndian)
if err != nil {
return err
}
*e = int64Time(time.Unix(int64(rv), 0))
return nil
// Message header checksum.
case *[4]byte:
_, err := io.ReadFull(r, e[:])
if err != nil {
return err
}
return nil
// Message header command.
case *[CommandSize]uint8:
_, err := io.ReadFull(r, e[:])
if err != nil {
return err
}
return nil
// IP address.
case *[16]byte:
_, err := io.ReadFull(r, e[:])
if err != nil {
return err
}
return nil
case *chainhash.Hash:
_, err := io.ReadFull(r, e[:])
if err != nil {
return err
}
return nil
case *ServiceFlag:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint64(r, littleEndian)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = ServiceFlag(rv)
return nil
case *InvType:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint32(r, littleEndian)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = InvType(rv)
return nil
case *BitcoinNet:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint32(r, littleEndian)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = BitcoinNet(rv)
return nil
case *BloomUpdateType:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint8(r)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = BloomUpdateType(rv)
return nil
case *RejectCode:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint8(r)
if err != nil {
return err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
*e = RejectCode(rv)
return nil
}
// Fall back to the slower binary.Read if a fast path was not available
// above.
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
return binary.Read(r, littleEndian, element)
2013-05-08 21:31:00 +02:00
}
// readElements reads multiple items from r. It is equivalent to multiple
// calls to readElement.
func readElements(r io.Reader, elements ...interface{}) error {
for _, element := range elements {
err := readElement(r, element)
if err != nil {
return err
}
}
return nil
}
// writeElement writes the little endian representation of element to w.
func writeElement(w io.Writer, element interface{}) error {
// Attempt to write the element based on the concrete type via fast
// type assertions first.
switch e := element.(type) {
case int32:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint32(w, littleEndian, uint32(e))
if err != nil {
return err
}
return nil
case uint32:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint32(w, littleEndian, e)
if err != nil {
return err
}
return nil
case int64:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint64(w, littleEndian, uint64(e))
if err != nil {
return err
}
return nil
case uint64:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint64(w, littleEndian, e)
if err != nil {
return err
}
return nil
case bool:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
var err error
if e {
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err = binarySerializer.PutUint8(w, 0x01)
} else {
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err = binarySerializer.PutUint8(w, 0x00)
}
if err != nil {
return err
}
return nil
// Message header checksum.
case [4]byte:
_, err := w.Write(e[:])
if err != nil {
return err
}
return nil
// Message header command.
case [CommandSize]uint8:
_, err := w.Write(e[:])
if err != nil {
return err
}
return nil
// IP address.
case [16]byte:
_, err := w.Write(e[:])
if err != nil {
return err
}
return nil
case *chainhash.Hash:
_, err := w.Write(e[:])
if err != nil {
return err
}
return nil
case ServiceFlag:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint64(w, littleEndian, uint64(e))
if err != nil {
return err
}
return nil
case InvType:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint32(w, littleEndian, uint32(e))
if err != nil {
return err
}
return nil
case BitcoinNet:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint32(w, littleEndian, uint32(e))
if err != nil {
return err
}
return nil
case BloomUpdateType:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint8(w, uint8(e))
if err != nil {
return err
}
return nil
case RejectCode:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint8(w, uint8(e))
if err != nil {
return err
}
return nil
}
// Fall back to the slower binary.Write if a fast path was not available
// above.
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
return binary.Write(w, littleEndian, element)
2013-05-08 21:31:00 +02:00
}
// writeElements writes multiple items to w. It is equivalent to multiple
// calls to writeElement.
func writeElements(w io.Writer, elements ...interface{}) error {
for _, element := range elements {
err := writeElement(w, element)
if err != nil {
return err
}
}
return nil
}
// ReadVarInt reads a variable length integer from r and returns it as a uint64.
func ReadVarInt(r io.Reader, pver uint32) (uint64, error) {
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
discriminant, err := binarySerializer.Uint8(r)
2013-05-08 21:31:00 +02:00
if err != nil {
return 0, err
}
var rv uint64
switch discriminant {
case 0xff:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
sv, err := binarySerializer.Uint64(r, littleEndian)
2013-05-08 21:31:00 +02:00
if err != nil {
return 0, err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv = sv
2013-05-08 21:31:00 +02:00
// The encoding is not canonical if the value could have been
// encoded using fewer bytes.
min := uint64(0x100000000)
if rv < min {
return 0, messageError("ReadVarInt", fmt.Sprintf(
errNonCanonicalVarInt, rv, discriminant, min))
}
2013-05-08 21:31:00 +02:00
case 0xfe:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
sv, err := binarySerializer.Uint32(r, littleEndian)
2013-05-08 21:31:00 +02:00
if err != nil {
return 0, err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv = uint64(sv)
2013-05-08 21:31:00 +02:00
// The encoding is not canonical if the value could have been
// encoded using fewer bytes.
min := uint64(0x10000)
if rv < min {
return 0, messageError("ReadVarInt", fmt.Sprintf(
errNonCanonicalVarInt, rv, discriminant, min))
}
2013-05-08 21:31:00 +02:00
case 0xfd:
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
sv, err := binarySerializer.Uint16(r, littleEndian)
2013-05-08 21:31:00 +02:00
if err != nil {
return 0, err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv = uint64(sv)
2013-05-08 21:31:00 +02:00
// The encoding is not canonical if the value could have been
// encoded using fewer bytes.
min := uint64(0xfd)
if rv < min {
return 0, messageError("ReadVarInt", fmt.Sprintf(
errNonCanonicalVarInt, rv, discriminant, min))
}
2013-05-08 21:31:00 +02:00
default:
rv = uint64(discriminant)
}
return rv, nil
}
// WriteVarInt serializes val to w using a variable number of bytes depending
2013-05-08 21:31:00 +02:00
// on its value.
func WriteVarInt(w io.Writer, pver uint32, val uint64) error {
if val < 0xfd {
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
return binarySerializer.PutUint8(w, uint8(val))
2013-05-08 21:31:00 +02:00
}
if val <= math.MaxUint16 {
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint8(w, 0xfd)
if err != nil {
return err
}
return binarySerializer.PutUint16(w, littleEndian, uint16(val))
2013-05-08 21:31:00 +02:00
}
if val <= math.MaxUint32 {
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint8(w, 0xfe)
if err != nil {
return err
}
return binarySerializer.PutUint32(w, littleEndian, uint32(val))
2013-05-08 21:31:00 +02:00
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
err := binarySerializer.PutUint8(w, 0xff)
if err != nil {
return err
}
return binarySerializer.PutUint64(w, littleEndian, val)
2013-05-08 21:31:00 +02:00
}
// VarIntSerializeSize returns the number of bytes it would take to serialize
// val as a variable length integer.
func VarIntSerializeSize(val uint64) int {
// The value is small enough to be represented by itself, so it's
// just 1 byte.
if val < 0xfd {
return 1
}
// Discriminant 1 byte plus 2 bytes for the uint16.
if val <= math.MaxUint16 {
return 3
}
// Discriminant 1 byte plus 4 bytes for the uint32.
if val <= math.MaxUint32 {
return 5
}
// Discriminant 1 byte plus 8 bytes for the uint64.
return 9
}
// ReadVarString reads a variable length string from r and returns it as a Go
// string. A variable length string is encoded as a variable length integer
// containing the length of the string followed by the bytes that represent the
// string itself. An error is returned if the length is greater than the
// maximum block payload size since it helps protect against memory exhaustion
// attacks and forced panics through malformed messages.
func ReadVarString(r io.Reader, pver uint32) (string, error) {
count, err := ReadVarInt(r, pver)
2013-05-08 21:31:00 +02:00
if err != nil {
return "", err
}
// Prevent variable length strings that are larger than the maximum
// message size. It would be possible to cause memory exhaustion and
// panics without a sane upper bound on this count.
if count > MaxMessagePayload {
str := fmt.Sprintf("variable length string is too long "+
"[count %d, max %d]", count, MaxMessagePayload)
return "", messageError("ReadVarString", str)
}
buf := make([]byte, count)
_, err = io.ReadFull(r, buf)
2013-05-08 21:31:00 +02:00
if err != nil {
return "", err
}
return string(buf), nil
}
// WriteVarString serializes str to w as a variable length integer containing
// the length of the string followed by the bytes that represent the string
// itself.
func WriteVarString(w io.Writer, pver uint32, str string) error {
err := WriteVarInt(w, pver, uint64(len(str)))
2013-05-08 21:31:00 +02:00
if err != nil {
return err
}
_, err = w.Write([]byte(str))
return err
2013-05-08 21:31:00 +02:00
}
// ReadVarBytes reads a variable length byte array. A byte array is encoded
// as a varInt containing the length of the array followed by the bytes
// themselves. An error is returned if the length is greater than the
// passed maxAllowed parameter which helps protect against memory exhaustion
// attacks and forced panics through malformed messages. The fieldName
// parameter is only used for the error message so it provides more context in
// the error.
func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
fieldName string) ([]byte, error) {
count, err := ReadVarInt(r, pver)
if err != nil {
return nil, err
}
// Prevent byte array larger than the max message size. It would
// be possible to cause memory exhaustion and panics without a sane
// upper bound on this count.
if count > uint64(maxAllowed) {
str := fmt.Sprintf("%s is larger than the max allowed size "+
"[count %d, max %d]", fieldName, count, maxAllowed)
return nil, messageError("ReadVarBytes", str)
}
b := make([]byte, count)
_, err = io.ReadFull(r, b)
if err != nil {
return nil, err
}
return b, nil
}
// WriteVarBytes serializes a variable length byte array to w as a varInt
// containing the number of bytes, followed by the bytes themselves.
func WriteVarBytes(w io.Writer, pver uint32, bytes []byte) error {
slen := uint64(len(bytes))
err := WriteVarInt(w, pver, slen)
if err != nil {
return err
}
_, err = w.Write(bytes)
return err
}
2013-05-08 21:31:00 +02:00
// randomUint64 returns a cryptographically random uint64 value. This
// unexported version takes a reader primarily to ensure the error paths
2013-05-10 04:24:47 +02:00
// can be properly tested by passing a fake reader in the tests.
2013-05-08 21:31:00 +02:00
func randomUint64(r io.Reader) (uint64, error) {
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
rv, err := binarySerializer.Uint64(r, bigEndian)
2013-05-08 21:31:00 +02:00
if err != nil {
return 0, err
}
wire: Reduce allocs with a binary free list. This introduces a new binary free list which provides a concurrent safe list of unused buffers for the purpose of serializing and deserializing primitive integers to their raw binary bytes. For convenience, the type also provides functions for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list. A global instance of the type has been introduced with a maximum number of 1024 items. Since each buffer is 8 bytes, it will consume a maximum of 8KB. Theoretically, this value would only allow up to 1024 peers simultaneously reading and writing without having to resort to burdening the garbage collector with additional allocations. However, due to the fact the code is designed in such a way that the buffers are quickly used and returned to the free list, in practice it can support much more than 1024 peers without involving the garbage collector since it is highly unlikely every peer would need a buffer at the exact same time. The following is a before and after comparison of the allocations with the benchmarks that did not change removed: benchmark old allocs new allocs delta ------------------------------------------------------------- WriteVarInt1 1 0 -100.00% WriteVarInt3 1 0 -100.00% WriteVarInt5 1 0 -100.00% WriteVarInt9 1 0 -100.00% ReadVarInt1 1 0 -100.00% ReadVarInt3 1 0 -100.00% ReadVarInt5 1 0 -100.00% ReadVarInt9 1 0 -100.00% ReadVarStr4 3 2 -33.33% ReadVarStr10 3 2 -33.33% WriteVarStr4 2 1 -50.00% WriteVarStr10 2 1 -50.00% ReadOutPoint 1 0 -100.00% WriteOutPoint 1 0 -100.00% ReadTxOut 3 1 -66.67% WriteTxOut 2 0 -100.00% ReadTxIn 5 2 -60.00% WriteTxIn 3 0 -100.00% DeserializeTxSmall 15 7 -53.33% DeserializeTxLarge 33428 16715 -50.00% SerializeTx 8 0 -100.00% ReadBlockHeader 7 1 -85.71% WriteBlockHeader 10 4 -60.00% DecodeGetHeaders 1004 501 -50.10% DecodeHeaders 18002 4001 -77.77% DecodeGetBlocks 1004 501 -50.10% DecodeAddr 9002 4001 -55.55% DecodeInv 150005 50003 -66.67% DecodeNotFound 150004 50002 -66.67% DecodeMerkleBlock 222 108 -51.35% TxSha 10 2 -80.00%
2016-04-21 06:03:00 +02:00
return rv, nil
2013-05-08 21:31:00 +02:00
}
// RandomUint64 returns a cryptographically random uint64 value.
func RandomUint64() (uint64, error) {
return randomUint64(rand.Reader)
}