Refactor event encoder to handle nil tags gracefully and optimize buffer allocation; update tests for improved coverage and data consistency.

This commit is contained in:
2025-08-22 21:47:19 +01:00
parent bf178eae4e
commit 225f949540
2 changed files with 35 additions and 23 deletions

View File

@@ -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)

View File

@@ -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) {
}