feat(subtree): NodeAllocator + DeserializeFromReaderWithAllocator + ReleaseNodes#124
Merged
Conversation
…eleaseNodes
For multi-hundred-million-tx blocks each subtree allocates ~1M *Node*
entries (48 B each) inside deserializeNodes. At 600+ subtrees per
block that is ~29 GB allocated then freed per block. The existing
DeserializeFromReader / deserializeNodes / deserializeFromReaderMmap
paths are untouched.
New surface:
type NodeAllocator func(numLeaves int) []Node
func (st *Subtree) DeserializeFromReaderWithAllocator(reader io.Reader,
alloc NodeAllocator) error
func (st *Subtree) ReleaseNodes() []Node
When alloc is non-nil the deserializer slices into the caller-supplied
backing rather than make()ing a fresh slice. If alloc returns nil or
too-small a slice the deserializer falls back to make() so callers
cannot break existing behaviour. ReleaseNodes returns the full-cap
slice and clears st.Nodes so the caller can return the backing to a
pool exactly once.
Tests cover the happy path, nil-allocator fallback, too-small fallback,
and ReleaseNodes cap preservation.
Merged
9 tasks
mrz1836
requested changes
May 18, 2026
mrz1836
left a comment
Collaborator
There was a problem hiding this comment.
LGTM - CI is failing - fix the failing checks please.
… wrappers DeserializeFromReader and deserializeNodes were near-copies of their allocator-aware twins introduced in this PR, differing only in how st.Nodes is allocated. SonarCloud flagged the duplication (18.7% new-code duplication, threshold 3%). Have the legacy entry points delegate to the *WithAllocator variants with a nil allocator — the nil-allocator branch falls back to make(), exactly matching the old behavior. Zero functional change; all tests remain green under -race. Recovery error string now reports "recovered in DeserializeFromReaderWithAllocator" when the panic originated under the legacy entry point. No tests pin the old string.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Add an opt-in caller-supplied allocator for the per-subtree `Node` array so downstream callers can pool the (often multi-GB) backing across uses. The existing `DeserializeFromReader` / `deserializeNodes` / `deserializeFromReaderMmap` paths are byte-for-byte unchanged; the new surface lives alongside them.
Motivation
For multi-hundred-million-tx blocks each subtree allocates ~1M `Node` entries (48 B each) inside `deserializeNodes`. At 600+ subtrees per block that is ~29 GB allocated then freed per block. Pooling that backing across blocks is the goal of the downstream teranode change; this PR adds the minimum surface needed.
New surface
```go
type NodeAllocator func(numLeaves int) []Node
func (st *Subtree) DeserializeFromReaderWithAllocator(reader io.Reader, alloc NodeAllocator) error
func (st *Subtree) ReleaseNodes() []Node
```
Risk
Zero existing call sites change. New paths are purely additive.
Test plan
Downstream
Teranode block validation depends on this: https://github.com/bsv-blockchain/teranode (perf(blockvalidation) PR — to be opened after this merges).