btcd/wire/msgcfcheckpt_bench_test.go
Olaoluwa Osuntokun d5cd95b9a9
wire: optimize parsing for CFCheckpkt message, reduce allocs by 96%
In this commit, we optimize the decoding for the CFCheckpkt message. The
old decode routine would do a fresh alloc for each hash to be read out.

Instead, we'll now allocate enough memory for the entire set of headers
to be decoded, then read them into that contiguous slice, and point to
members of this slice in the wire message itself.

We've also added benchmarks to show the improvement:
```
⛰   cat bench-cmp.txt
goos: darwin
goarch: arm64
pkg: github.com/btcsuite/btcd/wire
cpu: Apple M4 Max
                                     │ bench-old.txt │             bench-new.txt             │
                                     │    sec/op     │    sec/op     vs base                 │
MsgCFCheckptDecode/headers_1000-16     14.354µ ± ∞ ¹   6.919µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_10000-16    146.77µ ± ∞ ¹   70.23µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_100000-16   1473.6µ ± ∞ ¹   564.2µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_1000-16      7.021µ ± ∞ ¹   7.196µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_10000-16     88.97µ ± ∞ ¹   90.40µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_100000-16    861.0µ ± ∞ ¹   875.0µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecodeEmpty-16              68.28n ± ∞ ¹   66.61n ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                                 37.98µ         26.98µ        -28.98%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05

                                     │ bench-old.txt │             bench-new.txt             │
                                     │     B/op      │     B/op       vs base                │
MsgCFCheckptDecode/headers_1000-16     39.36Ki ± ∞ ¹   40.11Ki ± ∞ ¹       ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_10000-16    392.6Ki ± ∞ ¹   400.1Ki ± ∞ ¹       ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_100000-16   3.817Mi ± ∞ ¹   3.820Mi ± ∞ ¹       ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_1000-16     63.98Ki ± ∞ ¹   63.98Ki ± ∞ ¹       ~ (p=1.000 n=1) ³
MsgCFCheckptEncode/headers_10000-16    1.000Mi ± ∞ ¹   1.000Mi ± ∞ ¹       ~ (p=1.000 n=1) ³
MsgCFCheckptEncode/headers_100000-16   8.000Mi ± ∞ ¹   8.000Mi ± ∞ ¹       ~ (p=1.000 n=1) ³
MsgCFCheckptDecodeEmpty-16               112.0 ± ∞ ¹     112.0 ± ∞ ¹       ~ (p=1.000 n=1) ³
geomean                                166.5Ki         167.4Ki        +0.55%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05
³ all samples are equal

                                     │  bench-old.txt   │            bench-new.txt             │
                                     │    allocs/op     │  allocs/op   vs base                 │
MsgCFCheckptDecode/headers_1000-16       1003.000 ± ∞ ¹   4.000 ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_10000-16     10003.000 ± ∞ ¹   4.000 ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_100000-16   100003.000 ± ∞ ¹   4.000 ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_1000-16          11.00 ± ∞ ¹   11.00 ± ∞ ¹        ~ (p=1.000 n=1) ³
MsgCFCheckptEncode/headers_10000-16         15.00 ± ∞ ¹   15.00 ± ∞ ¹        ~ (p=1.000 n=1) ³
MsgCFCheckptEncode/headers_100000-16        18.00 ± ∞ ¹   18.00 ± ∞ ¹        ~ (p=1.000 n=1) ³
MsgCFCheckptDecodeEmpty-16                  2.000 ± ∞ ¹   2.000 ± ∞ ¹        ~ (p=1.000 n=1) ³
geomean                                     179.3         6.268        -96.50%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05
³ all samples are equal
```

Old bench:
```
goos: darwin
goarch: arm64
pkg: github.com/btcsuite/btcd/wire
cpu: Apple M4 Max
BenchmarkMsgCFCheckptDecode/headers_1000-16         	   74678	     14354 ns/op	   40304 B/op	    1003 allocs/op
BenchmarkMsgCFCheckptDecode/headers_10000-16        	    8234	    146770 ns/op	  402033 B/op	   10003 allocs/op
BenchmarkMsgCFCheckptDecode/headers_100000-16       	     822	   1473622 ns/op	 4002931 B/op	  100003 allocs/op
BenchmarkMsgCFCheckptEncode/headers_1000-16         	  173762	      7021 ns/op	   65520 B/op	      11 allocs/op
BenchmarkMsgCFCheckptEncode/headers_10000-16        	   13459	     88968 ns/op	 1048564 B/op	      15 allocs/op
BenchmarkMsgCFCheckptEncode/headers_100000-16       	    1399	    860985 ns/op	 8388592 B/op	      18 allocs/op
BenchmarkMsgCFCheckptDecodeEmpty-16                 	17459148	        68.28 ns/op	     112 B/op	       2 allocs/op
PASS
ok  	github.com/btcsuite/btcd/wire	10.135s

```

New bench:
```
goos: darwin
goarch: arm64
pkg: github.com/btcsuite/btcd/wire
cpu: Apple M4 Max
BenchmarkMsgCFCheckptDecode/headers_1000-16         	  166368	      6919 ns/op	   41072 B/op	       4 allocs/op
BenchmarkMsgCFCheckptDecode/headers_10000-16        	   17079	     70227 ns/op	  409712 B/op	       4 allocs/op
BenchmarkMsgCFCheckptDecode/headers_100000-16       	    2062	    564175 ns/op	 4006003 B/op	       4 allocs/op
BenchmarkMsgCFCheckptEncode/headers_1000-16         	  173940	      7196 ns/op	   65520 B/op	      11 allocs/op
BenchmarkMsgCFCheckptEncode/headers_10000-16        	   13054	     90401 ns/op	 1048564 B/op	      15 allocs/op
BenchmarkMsgCFCheckptEncode/headers_100000-16       	    1408	    875012 ns/op	 8388592 B/op	      18 allocs/op
BenchmarkMsgCFCheckptDecodeEmpty-16                 	17256627	        66.61 ns/op	     112 B/op	       2 allocs/op
PASS
ok  	github.com/btcsuite/btcd/wire	10.522s

```

As seen from the benchmarks, allocs have decreased by 96%, and the decode
speed by nearly 30%.
2025-07-14 18:36:27 -07:00

118 lines
2.7 KiB
Go

// Copyright (c) 2018 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
import (
"bytes"
"fmt"
"math/rand"
"testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
)
// BenchmarkMsgCFCheckptDecode benchmarks decoding of MsgCFCheckpt messages
// to measure the performance improvements from optimized memory allocation.
func BenchmarkMsgCFCheckptDecode(b *testing.B) {
pver := ProtocolVersion
// Test with varying number of headers: 1k, 10k, 100k.
headerCounts := []int{1000, 10000, 100000}
for _, numHeaders := range headerCounts {
b.Run(fmt.Sprintf("headers_%d", numHeaders), func(b *testing.B) {
var buf bytes.Buffer
msg := NewMsgCFCheckpt(
GCSFilterRegular, &chainhash.Hash{}, numHeaders,
)
rng := rand.New(rand.NewSource(12345))
for i := 0; i < numHeaders; i++ {
hash := chainhash.Hash{}
rng.Read(hash[:])
msg.AddCFHeader(&hash)
}
err := msg.BtcEncode(&buf, pver, BaseEncoding)
if err != nil {
b.Fatal(err)
}
encodedMsg := buf.Bytes()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
r := bytes.NewReader(encodedMsg)
var msg MsgCFCheckpt
err := msg.BtcDecode(r, pver, BaseEncoding)
if err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkMsgCFCheckptEncode benchmarks encoding of MsgCFCheckpt messages.
func BenchmarkMsgCFCheckptEncode(b *testing.B) {
pver := ProtocolVersion
// Test with varying number of headers: 1k, 10k, 100k.
headerCounts := []int{1000, 10000, 100000}
for _, numHeaders := range headerCounts {
b.Run(fmt.Sprintf("headers_%d", numHeaders), func(b *testing.B) {
msg := NewMsgCFCheckpt(
GCSFilterRegular, &chainhash.Hash{}, numHeaders,
)
rng := rand.New(rand.NewSource(12345))
for i := 0; i < numHeaders; i++ {
hash := chainhash.Hash{}
rng.Read(hash[:])
msg.AddCFHeader(&hash)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
err := msg.BtcEncode(&buf, pver, BaseEncoding)
if err != nil {
b.Fatal(err)
}
}
})
}
}
// BenchmarkMsgCFCheckptDecodeEmpty benchmarks decoding empty checkpoint
// messages to ensure edge cases are handled efficiently.
func BenchmarkMsgCFCheckptDecodeEmpty(b *testing.B) {
pver := ProtocolVersion
var buf bytes.Buffer
msg := NewMsgCFCheckpt(GCSFilterRegular, &chainhash.Hash{}, 0)
if err := msg.BtcEncode(&buf, pver, BaseEncoding); err != nil {
b.Fatal(err)
}
encodedMsg := buf.Bytes()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
r := bytes.NewReader(encodedMsg)
var msg MsgCFCheckpt
if err := msg.BtcDecode(r, pver, BaseEncoding); err != nil {
b.Fatal(err)
}
}
}