ridiculous performance string and serialization performance improvements.

This commit is contained in:
fiatjaf 2025-03-07 21:45:47 -03:00
parent 4431f58df2
commit 9e94310604
6 changed files with 18 additions and 13 deletions

View File

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

View File

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

View File

@ -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(']')
}

1
go.mod
View File

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

2
go.sum
View File

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

View File

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