From 225f949540a537e55f45323aa50fccc3e20f709d Mon Sep 17 00:00:00 2001 From: mleku Date: Fri, 22 Aug 2025 21:47:19 +0100 Subject: [PATCH] Refactor event encoder to handle nil tags gracefully and optimize buffer allocation; update tests for improved coverage and data consistency. --- pkg/encoders/event/event.go | 34 +++++++++++++++++++------------- pkg/encoders/event/event_test.go | 24 +++++++++++++--------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/pkg/encoders/event/event.go b/pkg/encoders/event/event.go index 14b1cea..90b6a98 100644 --- a/pkg/encoders/event/event.go +++ b/pkg/encoders/event/event.go @@ -112,22 +112,24 @@ func (ev *E) MarshalJSON() (b []byte, err error) { b = append(b, `,"`...) b = append(b, jTags...) b = append(b, `":[`...) - lts := len(*ev.Tags) - 1 - for i, tt := range *ev.Tags { - b = append(b, '[') - lt := len(tt.T) - 1 - for j, t := range tt.T { - b = append(b, '"') - b = append(b, t...) - b = append(b, '"') - if j < lt { + if ev.Tags != nil { + lts := len(*ev.Tags) - 1 + for i, tt := range *ev.Tags { + b = append(b, '[') + lt := len(tt.T) - 1 + for j, t := range tt.T { + b = append(b, '"') + b = append(b, t...) + b = append(b, '"') + if j < lt { + b = append(b, ',') + } + } + b = append(b, ']') + if i < lts { b = append(b, ',') } } - b = append(b, ']') - if i < lts { - b = append(b, ',') - } } b = append(b, `],"`...) b = append(b, jContent...) @@ -135,7 +137,11 @@ func (ev *E) MarshalJSON() (b []byte, err error) { // 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. + // 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+7+256+2) copy(b2, b) diff --git a/pkg/encoders/event/event_test.go b/pkg/encoders/event/event_test.go index d2a5abc..6a77e6e 100644 --- a/pkg/encoders/event/event_test.go +++ b/pkg/encoders/event/event_test.go @@ -1,10 +1,10 @@ package event import ( + "encoding/json" "testing" "time" - "github.com/pkg/profile" "lol.mleku.dev/chk" "lukechampine.com/frand" "next.orly.dev/pkg/encoders/hex" @@ -13,11 +13,8 @@ import ( "next.orly.dev/pkg/utils/bufpool" ) -func TestMarshalJSON(t *testing.T) { - // lol.SetLogLevel("trace") - prof := profile.Start(profile.MemProfile) - defer prof.Stop() - for range 1000000 { +func TestMarshalJSONUnmarshalJSON(t *testing.T) { + for range 10000 { ev := New() ev.ID = frand.Bytes(32) ev.Pubkey = frand.Bytes(32) @@ -32,18 +29,23 @@ func TestMarshalJSON(t *testing.T) { }, }, } - ev.Content = frand.Bytes(frand.Intn(1024) + 1) + ev.Content = []byte(`some text content + + with line breaks and tabs and other stuff +`) ev.Sig = frand.Bytes(64) // log.I.S(ev) b, err := ev.MarshalJSON() + if b, err = json.Marshal(ev); chk.E(err) { + t.Fatal(err) + } if err != nil { t.Fatal(err) } var bc []byte bc = append(bc, b...) - // log.I.F("%s", bc) ev2 := New() - if err = ev2.UnmarshalJSON(b); chk.E(err) { + if err = json.Unmarshal(b, ev2); chk.E(err) { t.Fatal(err) } var b2 []byte @@ -61,3 +63,7 @@ func TestMarshalJSON(t *testing.T) { bufpool.PutBytes(bc) } } + +func TestExamplesCache(t *testing.T) { + +}