From 246591b60b153edf6c6a231a4f2d0fe14c17f62d Mon Sep 17 00:00:00 2001 From: mleku Date: Fri, 12 Sep 2025 16:59:39 +0100 Subject: [PATCH] fix issue with memory allocation when marshaling events --- pkg/encoders/event/event.go | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/pkg/encoders/event/event.go b/pkg/encoders/event/event.go index 105e2f1..9a610ef 100644 --- a/pkg/encoders/event/event.go +++ b/pkg/encoders/event/event.go @@ -112,23 +112,7 @@ func (ev *E) Marshal(dst []byte) (b []byte) { b = append(b, '"') b = append(b, jId...) b = append(b, `":"`...) - // it can happen the slice has insufficient capacity to hold the content AND - // the signature at this point, because the signature encoder must have - // sufficient capacity pre-allocated as it does not append to the buffer. - // unlike every other encoding function up to this point. This also ensures - // that since the bufpool defaults to 1kb, most events won't have a - // re-allocation required, but if they do, it will be this next one, and it - // integrates properly with the buffer pool, reducing GC pressure and - // avoiding new heap allocations. - if cap(b) < len(b)+len(ev.Content)+7+256+2 { - b2 := make([]byte, len(b)+len(ev.Content)*2+1024) - copy(b2, b) - b2 = b2[:len(b)] - // return the old buffer to the pool for reuse. - bufpool.PutBytes(b) - b = b2 - } - b = b[:len(b)+2*sha256.Size] + b = append(b, make([]byte, 2*sha256.Size)...) xhex.Encode(b[len(b)-2*sha256.Size:], ev.ID) b = append(b, `","`...) b = append(b, jPubkey...) @@ -156,7 +140,7 @@ func (ev *E) Marshal(dst []byte) (b []byte) { b = append(b, `","`...) b = append(b, jSig...) b = append(b, `":"`...) - b = b[:len(b)+2*schnorr.SignatureSize] + b = append(b, make([]byte, 2*schnorr.SignatureSize)...) xhex.Encode(b[len(b)-2*schnorr.SignatureSize:], ev.Sig) b = append(b, `"}`...) return