From 9e94310604a0f627610e49c3ea306b112e7c95d8 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 7 Mar 2025 21:45:47 -0300 Subject: [PATCH] ridiculous performance string and serialization performance improvements. --- envelopes_sonic.go | 3 ++- event.go | 16 ++++++++-------- event_easyjson.go | 4 ++-- go.mod | 1 + go.sum | 2 ++ helpers.go | 5 +++-- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/envelopes_sonic.go b/envelopes_sonic.go index db0db25..3851f31 100644 --- a/envelopes_sonic.go +++ b/envelopes_sonic.go @@ -7,6 +7,7 @@ import ( "unsafe" "github.com/bytedance/sonic/ast" + "github.com/colduction/nocopy" ) type sonicVisitorPosition int @@ -549,7 +550,7 @@ func (smp sonicMessageParser) ParseMessage(message []byte) (Envelope, error) { sv := &sonicVisitor{smp: &smp} sv.whereWeAre = inEnvelope - err := ast.Preorder(string(message), sv, nil) + err := ast.Preorder(nocopy.ByteSliceToString(message), sv, nil) return sv.mainEnvelope, err } diff --git a/event.go b/event.go index e702626..7303bf6 100644 --- a/event.go +++ b/event.go @@ -3,7 +3,7 @@ package nostr import ( "crypto/sha256" "encoding/hex" - "fmt" + "strconv" "github.com/mailru/easyjson" ) @@ -60,13 +60,13 @@ func (evt *Event) Serialize() []byte { // the header portion is easy to serialize // [0,"pubkey",created_at,kind,[ - dst = append(dst, []byte( - fmt.Sprintf( - "[0,\"%s\",%d,%d,", - evt.PubKey, - evt.CreatedAt, - evt.Kind, - ))...) + dst = append(dst, "[0,\""...) + dst = append(dst, evt.PubKey...) + dst = append(dst, "\","...) + dst = append(dst, strconv.FormatInt(int64(evt.CreatedAt), 10)...) + dst = append(dst, ',') + dst = append(dst, strconv.Itoa(evt.Kind)...) + dst = append(dst, ',') // tags dst = evt.Tags.marshalTo(dst) diff --git a/event_easyjson.go b/event_easyjson.go index 8888b9c..a805222 100644 --- a/event_easyjson.go +++ b/event_easyjson.go @@ -69,7 +69,7 @@ func easyjsonF642ad3eDecodeGithubComNbdWtfGoNostr(in *jlexer.Lexer, out *Event) } for !in.IsDelim(']') { var v2 string - v2 = string(in.String()) + v2 = in.String() v1 = append(v1, v2) in.WantComma() } @@ -134,7 +134,7 @@ func easyjsonF642ad3eEncodeGithubComNbdWtfGoNostr(out *jwriter.Writer, in Event) if v5 > 0 { out.RawByte(',') } - out.String(string(v6)) + out.String(v6) } out.RawByte(']') } diff --git a/go.mod b/go.mod index 8c960c9..8841361 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/bytedance/sonic v1.13.1 github.com/cloudwego/base64x v0.1.5 github.com/coder/websocket v1.8.12 + github.com/colduction/nocopy v0.2.0 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 github.com/dgraph-io/badger/v4 v4.5.0 github.com/dgraph-io/ristretto v1.0.0 diff --git a/go.sum b/go.sum index e904b42..7f6fa09 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ github.com/cmars/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:P13beTBKr5Q1 github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo= github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs= +github.com/colduction/nocopy v0.2.0 h1:9jMLCmIP/wnAWO0FfSXJ4h5HBRe6cBqIqacWw/5sRXY= +github.com/colduction/nocopy v0.2.0/go.mod h1:MO+QBkEnsZYE7QukMAcAq4b0rHpSxOTlVqD3fI34YJs= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/helpers.go b/helpers.go index 5370827..b9c55ce 100644 --- a/helpers.go +++ b/helpers.go @@ -7,6 +7,7 @@ import ( "sync" "unsafe" + "github.com/colduction/nocopy" jsoniter "github.com/json-iterator/go" "golang.org/x/exp/constraints" ) @@ -134,7 +135,7 @@ func extractSubID(jsonStr []byte) string { end := bytes.Index(jsonStr[start:], []byte{'"'}) // get the contents - return string(jsonStr[start : start+end]) + return nocopy.ByteSliceToString(jsonStr[start : start+end]) } func extractEventID(jsonStr []byte) string { @@ -149,5 +150,5 @@ func extractEventID(jsonStr []byte) string { start += 4 + offset + 1 // get 64 characters of the id - return string(jsonStr[start : start+64]) + return nocopy.ByteSliceToString(jsonStr[start : start+64]) }