Commit 9aa279c
authored
perf: streaming TxHash and block-level scratch buffer for deserialization (#106)
* perf: streaming TxHash and block-level scratch buffer for deserialization
TxHash optimization:
- Write tx data directly to sha256.New() instead of serializing into
an intermediate bytes.Buffer, eliminating a per-tx buffer allocation
proportional to the transaction size.
- Cache computed hash in MsgTx.cachedHash field, invalidated by
AddTxIn/AddTxOut. Applied to both MsgTx and MsgExtendedTx.
Block deserialization optimization:
- Add bsvdecodeWithScratch method that reads all scripts into a shared
scratch buffer (reused across transactions) instead of allocating a
fresh buffer per large script via scriptFreeList.Borrow.
- MsgBlock.Bsvdecode passes a shared scratch buffer to each tx,
growing it only when a script larger than any previous one is
encountered. After each tx, the buffer is reset (len=0) but
capacity is preserved.
- Pre-allocate MsgTx structs contiguously in MsgBlock.Bsvdecode
(one slice instead of per-tx heap allocations).
For a 3.64 GB testnet block (28,672 txs):
- Block deserialization: 7,514 MB → 3,790 MB (-49.6%)
- TxHash: 3,758 MB → 0 MB (eliminated from allocation profile)
* fix: resolve govet shadow lint warning in bsvdecodeWithScratch
Pre-declare scriptLen variable to avoid shadowing err from outer scope
in the output script reading loop.1 parent 0c3dba1 commit 9aa279c
File tree
5 files changed
+239
-60
lines changed5 files changed
+239
-60
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
87 | 93 | | |
88 | 94 | | |
89 | | - | |
| 95 | + | |
90 | 96 | | |
91 | | - | |
| 97 | + | |
92 | 98 | | |
93 | 99 | | |
94 | 100 | | |
95 | | - | |
96 | | - | |
97 | 101 | | |
98 | 102 | | |
99 | 103 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
19 | 27 | | |
20 | 28 | | |
21 | 29 | | |
| |||
57 | 65 | | |
58 | 66 | | |
59 | 67 | | |
| 68 | + | |
| 69 | + | |
60 | 70 | | |
61 | 71 | | |
62 | 72 | | |
| |||
207 | 217 | | |
208 | 218 | | |
209 | 219 | | |
| 220 | + | |
| 221 | + | |
210 | 222 | | |
211 | 223 | | |
212 | 224 | | |
| |||
322 | 334 | | |
323 | 335 | | |
324 | 336 | | |
| 337 | + | |
| 338 | + | |
325 | 339 | | |
326 | 340 | | |
327 | 341 | | |
| |||
340 | 354 | | |
341 | 355 | | |
342 | 356 | | |
| 357 | + | |
| 358 | + | |
343 | 359 | | |
344 | 360 | | |
345 | 361 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| |||
0 commit comments