perf(rsema1d): reduce allocations in merkle proof and RLC computation#7060
perf(rsema1d): reduce allocations in merkle proof and RLC computation#7060
Conversation
…C computation Replace sha256.New() with sha256.Sum256 using a stack-allocated 65-byte buffer in hashPair, eliminating heap allocations on every internal node hash. Inline extractSymbols into computeRLC to avoid make([]field.GF16, 32) allocation per chunk. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.
Tip: disable this comment in your organization's Code Review settings.
|
|
||
| for c := range numChunks { | ||
| chunk := row[c*chunkSize : (c+1)*chunkSize] | ||
| symbols := extractSymbols(chunk) |
There was a problem hiding this comment.
The extractSymbols is not used anymor and it can be removed together with tests.
| product := field.Mul128(sym, coeffs[symbolIndex]) | ||
| coeffBase := c * 32 | ||
| for j := range 32 { | ||
| sym := field.GF16(chunk[32+j])<<8 | field.GF16(chunk[j]) |
There was a problem hiding this comment.
Let's move the comment from the old func here to explain what's going on
|
|
||
| // hashPair hashes two nodes with the inner prefix, writing result directly to dst | ||
| func hashPair(left, right *[32]byte, dst *[32]byte) { | ||
| h := sha256.New() |
There was a problem hiding this comment.
Surprising that it escaped to the heap tho. Its only used within this func.
I don't remember this escaping when I did https://github.com/celestiaorg/rsema1d-private/pull/21
|
Closing this — after verifying with escape analysis, both changes have zero measurable impact: hashPair: extractSymbols: When inlined into Credit to @Wondertan for catching this in review. |
Summary
hashPairusingsha256.Sum256with stack-allocated buffer instead ofsha256.New()heap allocationcomputeRLCto eliminate per-chunkmake([]field.GF16, 32)allocationChanges
pkg/rsema1d/merkle/merkle.go:hashPairuses[65]bytestack buffer +sha256.Sum256pkg/rsema1d/commitment.go:computeRLCinlines the Leopard symbol extraction loopBenchmarks
BenchmarkComputeRootFromProof: 0 allocs/op across all tree depths (was allocating per hash)BenchmarkVerifyRowWithContext/1MB: 32 B/op, 1 alloc/op (per-chunk allocs eliminated)Test plan
go test ./pkg/rsema1d/...passesgo test ./pkg/rsema1d/merkle/...passesCloses https://linear.app/celestia/issue/PROTOCO-1493/rsema1d-reduce-allocations-in-merkle-proof-and-rlc-computation
🤖 Generated with Claude Code