Skip to content

Commit 1004ee6

Browse files
authored
perf: streaming subtree serialization with go-bt v2.6.0 (#100)
* perf: use SerializeTo for streaming subtree serialization Replace SerializeBytes() + Write() with SerializeTo(w) in WriteTransactionsToWriter, writing transaction data directly to the io.Writer without allocating intermediate byte slices. This eliminates per-tx Output.Bytes() and Input.ExtendedBytes() allocations during subtree serialization. For a 3.64 GB block (28,672 txs), this reduced HandleBlockDirect allocations by ~10.6 GB. Depends on go-bt SerializeTo method (bsv-blockchain/go-bt#117). * perf: upgrade go-bt to v2.6.0 for streaming serialization Update go-bt dependency to v2.6.0 which adds zero-allocation WriteTo/ SerializeTo methods. The subtree serialization now uses SerializeTo to stream transaction data directly to the writer instead of allocating intermediate byte slices.
1 parent 1d0c624 commit 1004ee6

3 files changed

Lines changed: 11 additions & 12 deletions

File tree

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ module github.com/bsv-blockchain/go-subtree
33
go 1.24.6
44

55
require (
6-
github.com/bsv-blockchain/go-bt/v2 v2.5.3
6+
github.com/bsv-blockchain/go-bt/v2 v2.6.0
77
github.com/bsv-blockchain/go-safe-conversion v1.1.2
88
github.com/bsv-blockchain/go-tx-map v1.3.2
99
github.com/stretchr/testify v1.11.1
1010
golang.org/x/exp v0.0.0-20260112195511-716be5621a96
1111
)
1212

1313
require (
14-
github.com/bsv-blockchain/go-sdk v1.2.17 // indirect
14+
github.com/bsv-blockchain/go-sdk v1.2.18 // indirect
1515
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1616
github.com/dolthub/maphash v0.1.0 // indirect
1717
github.com/dolthub/swiss v0.2.1 // indirect
1818
github.com/pkg/errors v0.9.1 // indirect
1919
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
20-
golang.org/x/crypto v0.47.0 // indirect
20+
golang.org/x/crypto v0.48.0 // indirect
2121
google.golang.org/protobuf v1.36.11 // indirect
2222
gopkg.in/yaml.v3 v3.0.1 // indirect
2323
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
github.com/bsv-blockchain/go-bt/v2 v2.5.3 h1:QfVNnlXl4r+Sq+tfG8likv29b85gWcAq6v/IzvytAvY=
2-
github.com/bsv-blockchain/go-bt/v2 v2.5.3/go.mod h1:IW4ejL7cKDj3qRb+nZppWeu3/RzgLbDzsnzQrPmhUHc=
1+
github.com/bsv-blockchain/go-bt/v2 v2.6.0 h1:sac1aqOEC82z+WO1/oMlhWA3mtJ/X2LQ1TzT6nJwNeM=
2+
github.com/bsv-blockchain/go-bt/v2 v2.6.0/go.mod h1:npGAU4KG2iL9X/mKWqovMdb6ME5DIz+xeqY4CWO4dr4=
33
github.com/bsv-blockchain/go-safe-conversion v1.1.2 h1:otAM71jUp+rBvEaNjfLTxlBKNnMbdbvDoew7brwPg0k=
44
github.com/bsv-blockchain/go-safe-conversion v1.1.2/go.mod h1:KwO5HkH9S11kppAm7SedJhgaJnZbUMYRZalSq9fxLHQ=
5-
github.com/bsv-blockchain/go-sdk v1.2.17 h1:neQ/fxFL01yPFe3gbMDyBmIYqk+Vm7AQY1wtGsYQFFI=
6-
github.com/bsv-blockchain/go-sdk v1.2.17/go.mod h1:QWYwia7QSPB8+sLWyVldsIg0wPPzvEmXL5wGAT0dgaA=
5+
github.com/bsv-blockchain/go-sdk v1.2.18 h1:JFl8TNM7lf80CslrXjlungDOyuvL9COzond9BOR81Us=
6+
github.com/bsv-blockchain/go-sdk v1.2.18/go.mod h1:QWYwia7QSPB8+sLWyVldsIg0wPPzvEmXL5wGAT0dgaA=
77
github.com/bsv-blockchain/go-tx-map v1.3.2 h1:gixVwgGJveghUb1Ov6KEpJ4YS1F+k0E8lEcKRW29e4I=
88
github.com/bsv-blockchain/go-tx-map v1.3.2/go.mod h1:0BmYhEcYabfQZNy3JKXw5iu1vkW2xF/cL7p5Jt+aD2c=
99
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -26,8 +26,8 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
2626
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
2727
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
2828
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
29-
golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8=
30-
golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A=
29+
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
30+
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
3131
golang.org/x/exp v0.0.0-20260112195511-716be5621a96 h1:Z/6YuSHTLOHfNFdb8zVZomZr7cqNgTJvA8+Qz75D8gU=
3232
golang.org/x/exp v0.0.0-20260112195511-716be5621a96/go.mod h1:nzimsREAkjBCIEFtHiYkrJyT+2uy9YZJB7H1k68CXZU=
3333
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=

subtree_data.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ func (s *Data) WriteTransactionsToWriter(w io.Writer, startIdx, endIdx int) erro
138138
return ErrTransactionNil
139139
}
140140

141-
// Serialize and stream transaction bytes to writer
142-
txBytes := s.Txs[i].SerializeBytes()
143-
if _, err := w.Write(txBytes); err != nil {
141+
// Stream transaction directly to writer without intermediate allocation
142+
if _, err := s.Txs[i].SerializeTo(w); err != nil {
144143
return fmt.Errorf("%w at index %d: %w", ErrTransactionWrite, i, err)
145144
}
146145
}

0 commit comments

Comments
 (0)