brontide: use tiered buffer pool for encrypted message bodies#10949
brontide: use tiered buffer pool for encrypted message bodies#10949kpshukla wants to merge 2 commits into
Conversation
bodyBufferPool previously always handed out a max-size (65,551 byte) buffer for every outgoing message, even though the vast majority of wire messages (gossip updates, HTLC messages, etc.) are well under that size. Replace it with a tiered pool (256B / 1KB / 4KB / 16KB / max) so WriteMessage requests a buffer sized to the actual message instead of always paying for the worst case. headerBufferPool is left as-is since encrypted headers are a fixed 18 bytes and gain nothing from tiering.
🔴 PR Severity: CRITICAL
🔴 Critical (1 file)
🟢 Low (2 files)
AnalysisThe only substantive (non-test, non-doc) file touched is To override, add a |
Summary
Fixes #10246.
bodyBufferPool(added in #10183) always allocates a max-size (65,551byte) buffer for every outgoing message body, to avoid reallocation.
In practice, most LN wire messages (gossip updates, HTLC messages,
etc.) are well under that size, so the max-size allocation is wasted
memory for the common case.
This PR replaces the single pool with a
tieredBufferPool: fivesync.Pools bucketed at 256B / 1KB / 4KB / 16KB / max(
maxMessageSize).WriteMessagealready knows the plaintext lengthup front, so it now requests a buffer sized to
len(p) + macSizeinstead of always taking the largest tier. Buffers are returned to the
pool matching their exact capacity; a buffer whose capacity doesn't
match any tier (shouldn't happen in practice) is discarded rather than
risk polluting a tier with an oddly-sized buffer.
headerBufferPoolis untouched — encrypted headers are a fixed 18bytes (
encHeaderSize), so there's no benefit to tiering it.Test plan
go build ./...— clean, no downstream breakage in packages thatdepend on
brontide.go test ./brontide/...— full existing suite passes unchanged,including:
TestConnectionCorrectness,TestConcurrentHandshakesTestMaxPayloadLength(boundary atmath.MaxUint16)TestWriteMessageChunking,TestFlush(partial-write / resume paths)TestBolt0008TestVectors(BOLT#8 spec vectors)Fuzz*seed corpus cases (FuzzRandomActOne/Two/Three,FuzzStatic*,FuzzRandom*EncDec, etc.)TestTieredBufferPoolcovering tier selection atboundaries (exact match on a tier, rounds up to next tier, falls
back to largest tier when the request exceeds every tier) and the
discard-on-capacity-mismatch path.
gofmt -landgo vet ./brontide/...— clean.