Commit 27a931a
committed
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%.1 parent 4530538 commit 27a931a
2 files changed
+148
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
23 | 30 | | |
24 | 31 | | |
25 | 32 | | |
| |||
77 | 84 | | |
78 | 85 | | |
79 | 86 | | |
80 | | - | |
81 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
82 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
83 | 100 | | |
84 | | - | |
85 | | - | |
86 | | - | |
| 101 | + | |
87 | 102 | | |
88 | 103 | | |
89 | | - | |
| 104 | + | |
90 | 105 | | |
91 | 106 | | |
92 | 107 | | |
| |||
151 | 166 | | |
152 | 167 | | |
153 | 168 | | |
154 | | - | |
155 | | - | |
156 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
157 | 173 | | |
158 | 174 | | |
159 | | - | |
160 | | - | |
| 175 | + | |
| 176 | + | |
161 | 177 | | |
162 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
163 | 182 | | |
164 | 183 | | |
165 | 184 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
0 commit comments