From 29cf3c7c6e163107a8f84e1796e177732be3ea83 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 15 May 2025 15:01:51 -0300 Subject: [PATCH] simplify easyjson even more, rename functions, assume ids and pubkeys will always be safe hex strings that do not need to be escaped and eliminate unnecessary variables that would probably be eliminated by the compiler anyway. --- event_easyjson.go | 93 +++++++++++++++++++--------------------------- filter_easyjson.go | 33 ++++++++-------- 2 files changed, 55 insertions(+), 71 deletions(-) diff --git a/event_easyjson.go b/event_easyjson.go index 190257c..de1bc40 100644 --- a/event_easyjson.go +++ b/event_easyjson.go @@ -7,7 +7,7 @@ import ( jwriter "github.com/mailru/easyjson/jwriter" ) -func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event) { +func easyjsonDecodeEvent(in *jlexer.Lexer, out *Event) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -75,89 +75,74 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event) } } -func easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Event) { +func easyjsonEncodeEvent(out *jwriter.Writer, in Event) { out.RawByte('{') - first := true - _ = first - { - const prefix string = "\"kind\":" - out.RawString(prefix) - out.Int(int(in.Kind)) + + out.RawString("\"kind\":") + out.Int(int(in.Kind)) + + if in.ID != ZeroID { + out.RawString(",\"id\":\"") + out.RawString(hex.EncodeToString(in.ID[:])) } - { - if in.ID != [32]byte{} { - const prefix string = ",\"id\":" - out.RawString(prefix) - out.String(hex.EncodeToString(in.ID[:])) + + if in.PubKey != ZeroPK { + out.RawString("\",\"pubkey\":\"") + out.RawString(hex.EncodeToString(in.PubKey[:])) + } + + out.RawString("\",\"created_at\":") + out.Int64(int64(in.CreatedAt)) + + out.RawString(",\"tags\":") + out.RawByte('[') + for v3, v4 := range in.Tags { + if v3 > 0 { + out.RawByte(',') } - } - { - if in.PubKey != [32]byte{} { - const prefix string = ",\"pubkey\":" - out.RawString(prefix) - out.String(hex.EncodeToString(in.PubKey[:])) - } - } - { - const prefix string = ",\"created_at\":" - out.RawString(prefix) - out.Int64(int64(in.CreatedAt)) - } - { - const prefix string = ",\"tags\":" - out.RawString(prefix) out.RawByte('[') - for v3, v4 := range in.Tags { - if v3 > 0 { + for v5, v6 := range v4 { + if v5 > 0 { out.RawByte(',') } - out.RawByte('[') - for v5, v6 := range v4 { - if v5 > 0 { - out.RawByte(',') - } - out.String(v6) - } - out.RawByte(']') + out.String(v6) } out.RawByte(']') } - { - const prefix string = ",\"content\":" - out.RawString(prefix) - out.String(in.Content) - } - { - if in.Sig != [64]byte{} { - const prefix string = ",\"sig\":" - out.RawString(prefix) - out.String(hex.EncodeToString(in.Sig[:])) - } + out.RawByte(']') + + out.RawString(",\"content\":") + out.String(in.Content) + + if in.Sig != [64]byte{} { + out.RawString(",\"sig\":\"") + out.RawString(hex.EncodeToString(in.Sig[:]) + "\"") } + out.RawByte('}') } // MarshalJSON supports json.Marshaler interface func (v Event) MarshalJSON() ([]byte, error) { w := jwriter.Writer{NoEscapeHTML: true} - easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(&w, v) + easyjsonEncodeEvent(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v Event) MarshalEasyJSON(w *jwriter.Writer) { w.NoEscapeHTML = true - easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(w, v) + easyjsonEncodeEvent(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *Event) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(&r, v) + easyjsonDecodeEvent(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *Event) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(l, v) + easyjsonDecodeEvent(l, v) } diff --git a/filter_easyjson.go b/filter_easyjson.go index 72b194a..3abe811 100644 --- a/filter_easyjson.go +++ b/filter_easyjson.go @@ -7,7 +7,7 @@ import ( jwriter "github.com/mailru/easyjson/jwriter" ) -func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter) { +func easyjsonDecodeFilter(in *jlexer.Lexer, out *Filter) { isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { @@ -121,21 +121,20 @@ func easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Filter) } } -func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter) { +func easyjsonEncodeFilter(out *jwriter.Writer, in Filter) { out.RawByte('{') first := true _ = first if len(in.IDs) != 0 { - const prefix string = ",\"ids\":" first = false - out.RawString(prefix[1:]) + out.RawString("\"ids\":") { out.RawByte('[') - for v4, v5 := range in.IDs { - if v4 > 0 { + for i, id := range in.IDs { + if i > 0 { out.RawByte(',') } - out.String(hex.EncodeToString(v5[:])) + out.RawString("\"" + hex.EncodeToString(id[:]) + "\"") } out.RawByte(']') } @@ -150,11 +149,11 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter } { out.RawByte('[') - for v6, v7 := range in.Kinds { - if v6 > 0 { + for i, kind := range in.Kinds { + if i > 0 { out.RawByte(',') } - out.Int(int(v7)) + out.Int(int(kind)) } out.RawByte(']') } @@ -169,11 +168,11 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter } { out.RawByte('[') - for v8, v9 := range in.Authors { - if v8 > 0 { + for i, pk := range in.Authors { + if i > 0 { out.RawByte(',') } - out.String(hex.EncodeToString(v9[:])) + out.RawString("\"" + hex.EncodeToString(pk[:]) + "\"") } out.RawByte(']') } @@ -243,24 +242,24 @@ func easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Filter // MarshalJSON supports json.Marshaler interface func (v Filter) MarshalJSON() ([]byte, error) { w := jwriter.Writer{NoEscapeHTML: true} - easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(&w, v) + easyjsonEncodeFilter(&w, v) return w.Buffer.BuildBytes(), w.Error } // MarshalEasyJSON supports easyjson.Marshaler interface func (v Filter) MarshalEasyJSON(w *jwriter.Writer) { w.NoEscapeHTML = true - easyjson4d398eaaEncodeGithubComNbdWtfGoNostr(w, v) + easyjsonEncodeFilter(w, v) } // UnmarshalJSON supports json.Unmarshaler interface func (v *Filter) UnmarshalJSON(data []byte) error { r := jlexer.Lexer{Data: data} - easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(&r, v) + easyjsonDecodeFilter(&r, v) return r.Error() } // UnmarshalEasyJSON supports easyjson.Unmarshaler interface func (v *Filter) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson4d398eaaDecodeGithubComNbdWtfGoNostr(l, v) + easyjsonDecodeFilter(l, v) }