Skip to content

Commit

Permalink
wire: consistently use defer for returning scratch buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Dec 29, 2023
1 parent 8c4da83 commit b0e9636
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 187 deletions.
63 changes: 26 additions & 37 deletions wire/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"compress/bzip2"
"fmt"
"io"
"io/ioutil"
"net"
"os"
Expand Down Expand Up @@ -66,7 +67,7 @@ func BenchmarkWriteVarInt1(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
WriteVarInt(ioutil.Discard, 0, 1)
WriteVarInt(io.Discard, 0, 1)
}
}

Expand All @@ -76,7 +77,7 @@ func BenchmarkWriteVarInt3(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
WriteVarInt(ioutil.Discard, 0, 65535)
WriteVarInt(io.Discard, 0, 65535)
}
}

Expand All @@ -86,7 +87,7 @@ func BenchmarkWriteVarInt5(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
WriteVarInt(ioutil.Discard, 0, 4294967295)
WriteVarInt(io.Discard, 0, 4294967295)
}
}

Expand All @@ -96,7 +97,7 @@ func BenchmarkWriteVarInt9(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
WriteVarInt(ioutil.Discard, 0, 18446744073709551615)
WriteVarInt(io.Discard, 0, 18446744073709551615)
}
}

Expand Down Expand Up @@ -159,7 +160,7 @@ func BenchmarkWriteVarIntBuf1(b *testing.B) {

buffer := binarySerializer.Borrow()
for i := 0; i < b.N; i++ {
WriteVarIntBuf(ioutil.Discard, 0, 1, buffer)
WriteVarIntBuf(io.Discard, 0, 1, buffer)
}
binarySerializer.Return(buffer)
}
Expand All @@ -171,7 +172,7 @@ func BenchmarkWriteVarIntBuf3(b *testing.B) {

buffer := binarySerializer.Borrow()
for i := 0; i < b.N; i++ {
WriteVarIntBuf(ioutil.Discard, 0, 65535, buffer)
WriteVarIntBuf(io.Discard, 0, 65535, buffer)
}
binarySerializer.Return(buffer)
}
Expand All @@ -183,7 +184,7 @@ func BenchmarkWriteVarIntBuf5(b *testing.B) {

buffer := binarySerializer.Borrow()
for i := 0; i < b.N; i++ {
WriteVarIntBuf(ioutil.Discard, 0, 4294967295, buffer)
WriteVarIntBuf(io.Discard, 0, 4294967295, buffer)
}
binarySerializer.Return(buffer)
}
Expand All @@ -195,7 +196,7 @@ func BenchmarkWriteVarIntBuf9(b *testing.B) {

buffer := binarySerializer.Borrow()
for i := 0; i < b.N; i++ {
WriteVarIntBuf(ioutil.Discard, 0, 18446744073709551615, buffer)
WriteVarIntBuf(io.Discard, 0, 18446744073709551615, buffer)
}
binarySerializer.Return(buffer)
}
Expand Down Expand Up @@ -292,7 +293,7 @@ func BenchmarkWriteVarStr4(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
WriteVarString(ioutil.Discard, 0, "test")
WriteVarString(io.Discard, 0, "test")
}
}

Expand All @@ -302,7 +303,7 @@ func BenchmarkWriteVarStr10(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
WriteVarString(ioutil.Discard, 0, "test012345")
WriteVarString(io.Discard, 0, "test012345")
}
}

Expand Down Expand Up @@ -343,7 +344,7 @@ func BenchmarkWriteVarStrBuf4(b *testing.B) {

buf := binarySerializer.Borrow()
for i := 0; i < b.N; i++ {
writeVarStringBuf(ioutil.Discard, 0, "test", buf)
writeVarStringBuf(io.Discard, 0, "test", buf)
}
binarySerializer.Return(buf)
}
Expand All @@ -355,7 +356,7 @@ func BenchmarkWriteVarStrBuf10(b *testing.B) {

buf := binarySerializer.Borrow()
for i := 0; i < b.N; i++ {
writeVarStringBuf(ioutil.Discard, 0, "test012345", buf)
writeVarStringBuf(io.Discard, 0, "test012345", buf)
}
binarySerializer.Return(buf)
}
Expand Down Expand Up @@ -392,7 +393,7 @@ func BenchmarkWriteOutPoint(b *testing.B) {
Index: 0,
}
for i := 0; i < b.N; i++ {
WriteOutPoint(ioutil.Discard, 0, 0, op)
WriteOutPoint(io.Discard, 0, 0, op)
}
}

Expand All @@ -407,7 +408,7 @@ func BenchmarkWriteOutPointBuf(b *testing.B) {
Index: 0,
}
for i := 0; i < b.N; i++ {
writeOutPointBuf(ioutil.Discard, 0, 0, op, buf)
writeOutPointBuf(io.Discard, 0, 0, op, buf)
}
binarySerializer.Return(buf)
}
Expand Down Expand Up @@ -480,7 +481,7 @@ func BenchmarkWriteTxOut(b *testing.B) {

txOut := blockOne.Transactions[0].TxOut[0]
for i := 0; i < b.N; i++ {
WriteTxOut(ioutil.Discard, 0, 0, txOut)
WriteTxOut(io.Discard, 0, 0, txOut)
}
}

Expand All @@ -492,7 +493,7 @@ func BenchmarkWriteTxOutBuf(b *testing.B) {
buf := binarySerializer.Borrow()
txOut := blockOne.Transactions[0].TxOut[0]
for i := 0; i < b.N; i++ {
WriteTxOutBuf(ioutil.Discard, 0, 0, txOut, buf)
WriteTxOutBuf(io.Discard, 0, 0, txOut, buf)
}
binarySerializer.Return(buf)
}
Expand Down Expand Up @@ -533,7 +534,7 @@ func BenchmarkWriteTxIn(b *testing.B) {
buf := binarySerializer.Borrow()
txIn := blockOne.Transactions[0].TxIn[0]
for i := 0; i < b.N; i++ {
writeTxInBuf(ioutil.Discard, 0, 0, txIn, buf)
writeTxInBuf(io.Discard, 0, 0, txIn, buf)
}
binarySerializer.Return(buf)
}
Expand Down Expand Up @@ -608,15 +609,9 @@ func BenchmarkDeserializeTxLarge(b *testing.B) {
}

func BenchmarkDeserializeBlock(b *testing.B) {
f, err := os.Open(
buf, err := os.ReadFile(
"testdata/block-00000000000000000021868c2cefc52a480d173c849412fe81c4e5ab806f94ab.blk",
)
if err != nil {
b.Fatalf("Failed to open block file: %v", err)
}
defer f.Close()

buf, err := ioutil.ReadAll(f)
if err != nil {
b.Fatalf("Failed to read block data: %v", err)
}
Expand All @@ -633,15 +628,9 @@ func BenchmarkDeserializeBlock(b *testing.B) {
}

func BenchmarkSerializeBlock(b *testing.B) {
f, err := os.Open(
buf, err := os.ReadFile(
"testdata/block-00000000000000000021868c2cefc52a480d173c849412fe81c4e5ab806f94ab.blk",
)
if err != nil {
b.Fatalf("Failed to open block file: %v", err)
}
defer f.Close()

buf, err := ioutil.ReadAll(f)
if err != nil {
b.Fatalf("Failed to read block data: %v", err)
}
Expand All @@ -656,7 +645,7 @@ func BenchmarkSerializeBlock(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
block.Serialize(ioutil.Discard)
block.Serialize(io.Discard)
}
}

Expand All @@ -667,7 +656,7 @@ func BenchmarkSerializeTx(b *testing.B) {

tx := blockOne.Transactions[0]
for i := 0; i < b.N; i++ {
tx.Serialize(ioutil.Discard)
tx.Serialize(io.Discard)

}
}
Expand Down Expand Up @@ -710,7 +699,7 @@ func BenchmarkSerializeTxSmall(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
tx.Serialize(ioutil.Discard)
tx.Serialize(io.Discard)
}
}

Expand All @@ -736,7 +725,7 @@ func BenchmarkSerializeTxLarge(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
tx.Serialize(ioutil.Discard)
tx.Serialize(io.Discard)
}
}

Expand Down Expand Up @@ -805,7 +794,7 @@ func BenchmarkWriteBlockHeader(b *testing.B) {

header := blockOne.Header
for i := 0; i < b.N; i++ {
writeBlockHeader(ioutil.Discard, 0, &header)
writeBlockHeader(io.Discard, 0, &header)
}
}

Expand All @@ -817,7 +806,7 @@ func BenchmarkWriteBlockHeaderBuf(b *testing.B) {
buf := binarySerializer.Borrow()
header := blockOne.Header
for i := 0; i < b.N; i++ {
writeBlockHeaderBuf(ioutil.Discard, 0, &header, buf)
writeBlockHeaderBuf(io.Discard, 0, &header, buf)
}
binarySerializer.Return(buf)
}
Expand Down
Loading

0 comments on commit b0e9636

Please sign in to comment.