Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wire: only borrow/return binaryFreeList buffers at the message level #2073

Merged
merged 52 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b434080
wire/bench_test: report allocs in benchmarks
cfromknecht Jan 25, 2020
a9edc32
wire/bench: add witness block
cfromknecht Jan 25, 2020
a371aeb
wire/common: optimize Read/WriteVarInt
cfromknecht Jan 25, 2020
6275db9
wire: introduce Read/WriteVarIntBuf to reuse buffers between invocations
cfromknecht Jan 25, 2020
e58aadc
wire/msgtx: use Read/WriteVarIntBuf in tx serialization
cfromknecht Jan 25, 2020
e12d32d
wire/msgtx: reuse tx-level buffer for version and locktime
cfromknecht Jan 25, 2020
7951aa5
wire/common: add optimized Read/WriteVarBytesBuf
cfromknecht Jan 25, 2020
b171012
wire/msgtx: introduce optimized read/writeOutPointBuf
cfromknecht Jan 25, 2020
d43d9d5
wire/msgtx: introduce optimized writeTxInBuf
cfromknecht Jan 25, 2020
4829ff7
wire/msgtx: use writeTxInBuf in txn encoding
cfromknecht Jan 25, 2020
99f6488
wire/msgtx: introduce optimized readScriptBuf
cfromknecht Apr 30, 2019
6f4a7a1
wire/msgtx: introduce optimized readTxInBuf
cfromknecht Apr 30, 2019
607eea1
wire/msgtx: use readTxInBuf in txn serialization
cfromknecht Jan 25, 2020
7c8844f
wire/msgtx: introduce optimized WriteTxOutBuf
cfromknecht Jan 25, 2020
48d31e5
wire/msgtx: use WriteTxOutBuf in txn serialization
cfromknecht Jan 25, 2020
aebc743
wire/msgtx: introduce optimized readTxOutBuf
cfromknecht Jan 25, 2020
24d4217
wire/msgtx: use readTxOutBuf in txn serialization
cfromknecht Jan 25, 2020
3bfd0c6
wire/msgtx: introduce optimized writeTxWitnessBuf
cfromknecht Jan 25, 2020
3a91303
wire/msgtx: use writeTxWitnessBuf in txn serialization
cfromknecht Jan 25, 2020
0cf8c19
wire/msgtx: use readScriptBuf in txn serialization
cfromknecht Jan 25, 2020
aa769e3
wire/bench_test: introduce optimized readBlockHeaderBuf
cfromknecht Jan 25, 2020
3cee06e
wire/blockheader: introduce optimized writeBlockHeaderBuf
cfromknecht Jan 25, 2020
674c220
wire/invvect: add optimized readInvVectBuf and writeInvVectBuf
cfromknecht Jan 25, 2020
4ebc651
wire/msggetblocks: optimize by reusing small buffer
cfromknecht Jan 25, 2020
ee1f807
wire/msgblock: use only one small buffer per block encode/decode
cfromknecht Jan 25, 2020
d8e0817
wire/msgblock: optimize DeserializeTxLoc by reusing small buffers
cfromknecht Jan 25, 2020
c0d35e6
wire/msggetheaders: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
83675cb
wire/msgheaders: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
d042fe0
wire/msggetcfheaders: use single small buffer for encode/decode
cfromknecht Jan 25, 2020
1c525db
wire/msgcfheaders: optimize encode/decode by using one small buffer
cfromknecht Jan 25, 2020
1990555
wire/msggetcfcheckpt: optimize by removing read/writeElement
cfromknecht Jan 25, 2020
f37f475
wire/msgcfcheckpt: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
2383a04
wire/msginv: optimize by reusing small buffers
cfromknecht Jan 25, 2020
d6594da
wire/msggetdata: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
ddeba60
wiree/msggetcfilters: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
834febb
wire/msgcfilter: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
efcf964
wire/msgnotfound: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
1cd5e02
wire/invvect: remove unused readInvVect and writeInvVect
cfromknecht Jan 25, 2020
57daac3
wire/common: add optimized writeVarStrBuf an readVarStrBuf
cfromknecht Jan 25, 2020
dc4fbb0
wire/msgreject: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
8bf07cc
wire/netaddress: add optimiezed read/writeNetAddressBuf
cfromknecht Jan 25, 2020
7207967
wire/msgmerkleblock: optimize serialization by reusing small buffers
cfromknecht Jan 25, 2020
3698f2d
wire/msgping: remove usage for read/writeElement
cfromknecht Jan 25, 2020
80ae5d3
wire/msgpong: remove usage of read/writeElement
cfromknecht Jan 25, 2020
da89ed6
wire/msgtx: remove unused writeTxWitness
cfromknecht Jan 25, 2020
f0184e5
wire/msgtx: remove unused writeTxIn
cfromknecht Jan 25, 2020
e0fa866
wire/msgtx: remove unused readTxIn
cfromknecht Jan 25, 2020
4cc4f76
wire/msgtx: remove unused readScript
cfromknecht Jan 25, 2020
2e6eefc
wire/msgtx: remove unused read/writeOutPoint
cfromknecht Jan 25, 2020
d7396dc
wire/msgtx: use tx-level script slab
cfromknecht Jan 25, 2020
8c4da83
wire/msgblock+msgtx: user block-level script slab
cfromknecht Jan 25, 2020
b0e9636
wire: consistently use defer for returning scratch buffers
Roasbeef Dec 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions wire/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,28 @@ func BenchmarkReadOutPoint(b *testing.B) {
}
}

// BenchmarkReadOutPointBuf performs a benchmark on how long it takes to read a
// transaction output point.
func BenchmarkReadOutPointBuf(b *testing.B) {
b.ReportAllocs()

buffer := binarySerializer.Borrow()
buf := []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
0xff, 0xff, 0xff, 0xff, // Previous output index
}
r := bytes.NewReader(buf)
var op OutPoint
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
readOutPointBuf(r, 0, 0, &op, buffer)
}
binarySerializer.Return(buffer)
}

// BenchmarkWriteOutPoint performs a benchmark on how long it takes to write a
// transaction output point.
func BenchmarkWriteOutPoint(b *testing.B) {
Expand All @@ -340,6 +362,22 @@ func BenchmarkWriteOutPoint(b *testing.B) {
}
}

// BenchmarkWriteOutPointBuf performs a benchmark on how long it takes to write a
// transaction output point.
func BenchmarkWriteOutPointBuf(b *testing.B) {
b.ReportAllocs()

buf := binarySerializer.Borrow()
op := &OutPoint{
Hash: chainhash.Hash{},
Index: 0,
}
for i := 0; i < b.N; i++ {
writeOutPointBuf(ioutil.Discard, 0, 0, op, buf)
}
binarySerializer.Return(buf)
}

// BenchmarkReadTxOut performs a benchmark on how long it takes to read a
// transaction output.
func BenchmarkReadTxOut(b *testing.B) {
Expand Down
49 changes: 45 additions & 4 deletions wire/msgtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,25 +968,66 @@ func NewMsgTx(version int32) *MsgTx {
}

// readOutPoint reads the next sequence of bytes from r as an OutPoint.
//
// DEPRECATED: Use readOutPointBuf instead.
func readOutPoint(r io.Reader, pver uint32, version int32, op *OutPoint) error {
buf := binarySerializer.Borrow()
err := readOutPointBuf(r, pver, version, op, buf)
binarySerializer.Return(buf)
return err
}

// readOutPointBuf reads the next sequence of bytes from r as an OutPoint.
//
// If b is non-nil, the provided buffer will be used for serializing small
// values. Otherwise a buffer will be drawn from the binarySerializer's pool
Roasbeef marked this conversation as resolved.
Show resolved Hide resolved
// and return when the method finishes.
//
// NOTE: b MUST either be nil or at least an 8-byte slice.
func readOutPointBuf(r io.Reader, pver uint32, version int32, op *OutPoint,
buf []byte) error {

_, err := io.ReadFull(r, op.Hash[:])
if err != nil {
return err
}

op.Index, err = binarySerializer.Uint32(r, littleEndian)
if _, err := io.ReadFull(r, buf[:4]); err != nil {
return err
}
op.Index = littleEndian.Uint32(buf[:4])

return nil
}

// WriteOutPoint encodes op to the bitcoin protocol encoding for an OutPoint to
// w.
func WriteOutPoint(w io.Writer, pver uint32, version int32, op *OutPoint) error {
buf := binarySerializer.Borrow()
err := writeOutPointBuf(w, pver, version, op, buf)
binarySerializer.Return(buf)
return err
}

// WriteOutPoint encodes op to the bitcoin protocol encoding for an OutPoint
// writeOutPointBuf encodes op to the bitcoin protocol encoding for an OutPoint
// to w.
func WriteOutPoint(w io.Writer, pver uint32, version int32, op *OutPoint) error {
//
// If b is non-nil, the provided buffer will be used for serializing small
// values. Otherwise a buffer will be drawn from the binarySerializer's pool
// and return when the method finishes.
//
// NOTE: b MUST either be nil or at least an 8-byte slice.
func writeOutPointBuf(w io.Writer, pver uint32, version int32, op *OutPoint,
buf []byte) error {

_, err := w.Write(op.Hash[:])
if err != nil {
return err
}

return binarySerializer.PutUint32(w, littleEndian, op.Index)
littleEndian.PutUint32(buf[:4], op.Index)
_, err = w.Write(buf[:4])
return err
}

// readScript reads a variable length byte array that represents a transaction
Expand Down