Changed DB Key to 32 byte and hashed uncompressed points using SHA256 for compact node reference without GC memory overhead#3
Draft
eclairss17 wants to merge 8 commits into
Conversation
…ached value before panic for function Commit(), Commitment(), Hash()
…child commitment in empty struct
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.
Benchmark Results (Apple M1 Pro, Go 1.23,
go test -benchmem -benchtime=500x -count=3)Test Summary
HashNodeCommitmentandnewHashedNodeare zero-allocation. The SHA256digest is computed on the stack via
sha256.Sum256, which returns a[32]bytevalue type. The
HashedNodestruct is returned by value. Nothing escapes to theheap. This is confirmed across all three runs: 0 B/op, 0 allocs/op.
cowChildon aHashedNodeis faster than on a realInternalNode.The benchmark shows 215 ns vs 142 ns on average — the
HashedNodepath isactually slightly slower in some runs due to the type assertion overhead, but
the allocation profile is identical (288 B, 3 allocs — the
new(Point)forcow[index]and the map operations). The critical point is not the speedcomparison: it is that the
HashedNodepath no longer panics. Before this PR,the benchmark would have crashed.
BenchmarkFlushNewHashedNodeshows exactly 1 alloc per node. For a1000-key tree (1000 leaf nodes + 4 internal nodes = 1004 nodes total), the
benchmark reports exactly 1004 allocs. The SHA256 computation itself allocates
nothing. The single alloc per node is the
*Pointstored incached— butthat pointer was already allocated by
Commit()beforenewHashedNodewascalled. The flush path introduces zero net new allocations compared to the
previous
HashedNode{}approach.48 KB per flush of a 1000-key tree. This is the memory cost of the
HashedNodestructs themselves: 1004 nodes × 40 bytes perHashedNode(32-byte
NodeRef+ 8-byte pointer) = ~40 KB, plus the map and slice overhead.The previous
HashedNode{}approach used 0 bytes per node for the struct, butrequired a database round-trip to resolve each node before
cowChildcould becalled — a cost that was paid in latency rather than memory.