diff --git a/envelopes.go b/envelopes.go index eec494c..3081537 100644 --- a/envelopes.go +++ b/envelopes.go @@ -2,7 +2,6 @@ package nostr import ( "bytes" - "encoding/json" "fmt" "strconv" diff --git a/envelopes_test.go b/envelopes_test.go index 165d028..a2fc538 100644 --- a/envelopes_test.go +++ b/envelopes_test.go @@ -1,7 +1,6 @@ package nostr import ( - "encoding/json" "testing" "github.com/stretchr/testify/assert" diff --git a/event_easyjson.go b/event_easyjson.go index c1db59f..435583c 100644 --- a/event_easyjson.go +++ b/event_easyjson.go @@ -1,8 +1,6 @@ package nostr import ( - json "encoding/json" - easyjson "github.com/mailru/easyjson" jlexer "github.com/mailru/easyjson/jlexer" jwriter "github.com/mailru/easyjson/jwriter" @@ -10,7 +8,6 @@ import ( // suppress unused package warning var ( - _ *json.RawMessage _ *jlexer.Lexer _ *jwriter.Writer _ easyjson.Marshaler diff --git a/event_test.go b/event_test.go index 0cde38b..c05e534 100644 --- a/event_test.go +++ b/event_test.go @@ -1,7 +1,6 @@ package nostr import ( - "encoding/json" "fmt" "math/rand/v2" "testing" diff --git a/example/example.go b/example/example.go index 94c91f8..e5826d1 100644 --- a/example/example.go +++ b/example/example.go @@ -2,13 +2,13 @@ package main import ( "context" - "encoding/json" "fmt" "io" "os" "strings" "time" + jsoniter "github.com/json-iterator/go" "github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr/nip19" ) @@ -69,7 +69,7 @@ func main() { if f, err := os.Create(filename); err == nil { fmt.Fprintf(os.Stderr, "returned events saved to %s\n", filename) // encode the returned events in a file - enc := json.NewEncoder(f) + enc := jsoniter.NewEncoder(f) enc.SetIndent("", " ") enc.Encode(evs) f.Close() diff --git a/filter.go b/filter.go index 68e03e1..ed4b1f0 100644 --- a/filter.go +++ b/filter.go @@ -1,7 +1,6 @@ package nostr import ( - "encoding/json" "slices" "github.com/mailru/easyjson" diff --git a/filter_easyjson.go b/filter_easyjson.go index 2d30f51..d980045 100644 --- a/filter_easyjson.go +++ b/filter_easyjson.go @@ -1,8 +1,6 @@ package nostr import ( - json "encoding/json" - easyjson "github.com/mailru/easyjson" jlexer "github.com/mailru/easyjson/jlexer" jwriter "github.com/mailru/easyjson/jwriter" @@ -10,7 +8,6 @@ import ( // suppress unused package warning var ( - _ *json.RawMessage _ *jlexer.Lexer _ *jwriter.Writer _ easyjson.Marshaler diff --git a/filter_test.go b/filter_test.go index c9f6d56..5979eff 100644 --- a/filter_test.go +++ b/filter_test.go @@ -1,7 +1,6 @@ package nostr import ( - "encoding/json" "slices" "testing" diff --git a/go.mod b/go.mod index c208cde..ba3b94b 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/gobwas/ws v1.4.0 github.com/graph-gophers/dataloader/v7 v7.1.0 github.com/jmoiron/sqlx v1.3.5 + github.com/json-iterator/go v1.1.12 github.com/mailru/easyjson v0.7.7 github.com/mattn/go-sqlite3 v1.14.18 github.com/ncruces/go-sqlite3 v0.18.3 @@ -45,6 +46,8 @@ require ( github.com/kr/pretty v0.3.1 // indirect github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/ncruces/julianday v1.0.0 // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum index 1523547..8239e7a 100644 --- a/go.sum +++ b/go.sum @@ -79,6 +79,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo= github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -95,6 +96,8 @@ github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Cc github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -112,6 +115,10 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI= github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/ncruces/go-sqlite3 v0.18.3 h1:tyMa75uh7LcINcfo0WrzOvcTkfz8Hqu0TEPX+KVyes4= github.com/ncruces/go-sqlite3 v0.18.3/go.mod h1:HAwOtA+cyEX3iN6YmkpQwfT4vMMgCB7rQRFUdOgEFik= github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= @@ -141,6 +148,7 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.1.5-0.20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= diff --git a/helpers.go b/helpers.go index f4feeb4..9217e00 100644 --- a/helpers.go +++ b/helpers.go @@ -6,12 +6,16 @@ import ( "sync" "unsafe" + jsoniter "github.com/json-iterator/go" "golang.org/x/exp/constraints" ) const MAX_LOCKS = 50 -var namedMutexPool = make([]sync.Mutex, MAX_LOCKS) +var ( + namedMutexPool = make([]sync.Mutex, MAX_LOCKS) + json = jsoniter.ConfigFastest +) //go:noescape //go:linkname memhash runtime.memhash diff --git a/log_debug.go b/log_debug.go index 3fb34c8..16ef75d 100644 --- a/log_debug.go +++ b/log_debug.go @@ -3,7 +3,6 @@ package nostr import ( - "encoding/json" "fmt" ) diff --git a/nip05/nip05.go b/nip05/nip05.go index 6e48d54..2f7e82e 100644 --- a/nip05/nip05.go +++ b/nip05/nip05.go @@ -2,12 +2,12 @@ package nip05 import ( "context" - "encoding/json" "fmt" "net/http" "regexp" "strings" + jsoniter "github.com/json-iterator/go" "github.com/nbd-wtf/go-nostr" ) @@ -81,7 +81,7 @@ func Fetch(ctx context.Context, fullname string) (resp WellKnownResponse, name s defer res.Body.Close() var result WellKnownResponse - if err := json.NewDecoder(res.Body).Decode(&result); err != nil { + if err := jsoniter.NewDecoder(res.Body).Decode(&result); err != nil { return resp, name, fmt.Errorf("failed to decode json response: %w", err) } diff --git a/nip11/fetch.go b/nip11/fetch.go index b230a98..2b705ff 100644 --- a/nip11/fetch.go +++ b/nip11/fetch.go @@ -2,11 +2,11 @@ package nip11 import ( "context" - "encoding/json" "fmt" "net/http" "time" + jsoniter "github.com/json-iterator/go" "github.com/nbd-wtf/go-nostr" ) @@ -43,7 +43,7 @@ func Fetch(ctx context.Context, u string) (info RelayInformationDocument, err er } defer resp.Body.Close() - if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { + if err := jsoniter.NewDecoder(resp.Body).Decode(&info); err != nil { return info, fmt.Errorf("invalid json: %w", err) } diff --git a/nip46/bunker_session.go b/nip46/bunker_session.go index 74c1fd2..1023fe6 100644 --- a/nip46/bunker_session.go +++ b/nip46/bunker_session.go @@ -1,7 +1,6 @@ package nip46 import ( - "encoding/json" "fmt" "github.com/nbd-wtf/go-nostr" diff --git a/nip46/client.go b/nip46/client.go index 471bf6d..1291a6c 100644 --- a/nip46/client.go +++ b/nip46/client.go @@ -2,7 +2,6 @@ package nip46 import ( "context" - "encoding/json" "fmt" "math/rand" "net/url" diff --git a/nip46/dynamic-signer.go b/nip46/dynamic-signer.go index 9dda28a..5cc29a8 100644 --- a/nip46/dynamic-signer.go +++ b/nip46/dynamic-signer.go @@ -2,7 +2,6 @@ package nip46 import ( "context" - "encoding/json" "fmt" "slices" "sync" diff --git a/nip46/nip46.go b/nip46/nip46.go index c975e34..88f0414 100644 --- a/nip46/nip46.go +++ b/nip46/nip46.go @@ -2,13 +2,16 @@ package nip46 import ( "context" - "encoding/json" "regexp" + jsoniter "github.com/json-iterator/go" "github.com/nbd-wtf/go-nostr" ) -var BUNKER_REGEX = regexp.MustCompile(`^bunker:\/\/([0-9a-f]{64})\??([?\/\w:.=&%]*)$`) +var ( + BUNKER_REGEX = regexp.MustCompile(`^bunker:\/\/([0-9a-f]{64})\??([?\/\w:.=&%]*)$`) + json = jsoniter.ConfigFastest +) type Request struct { ID string `json:"id"` diff --git a/nip46/static-key-signer.go b/nip46/static-key-signer.go index 4fc471a..75e2dea 100644 --- a/nip46/static-key-signer.go +++ b/nip46/static-key-signer.go @@ -2,7 +2,6 @@ package nip46 import ( "context" - "encoding/json" "fmt" "slices" "sync" diff --git a/nip96/nip96.go b/nip96/nip96.go index f940b3f..eae7349 100644 --- a/nip96/nip96.go +++ b/nip96/nip96.go @@ -6,7 +6,6 @@ import ( "crypto/sha256" "encoding/base64" "encoding/hex" - "encoding/json" "fmt" "hash" "io" @@ -14,6 +13,7 @@ import ( "net/http" "strconv" + jsoniter "github.com/json-iterator/go" "github.com/nbd-wtf/go-nostr" ) @@ -96,7 +96,7 @@ func Upload(ctx context.Context, req UploadRequest) (*UploadResponse, error) { case http.StatusOK, http.StatusCreated, http.StatusAccepted: var uploadResp UploadResponse - if err := json.NewDecoder(resp.Body).Decode(&uploadResp); err != nil { + if err := jsoniter.NewDecoder(resp.Body).Decode(&uploadResp); err != nil { return nil, fmt.Errorf("Error decoding JSON: %w", err) } return &uploadResp, nil @@ -126,7 +126,7 @@ func generateAuthHeader(sk, host string, fileHash hash.Hash) (string, error) { } event.Sign(sk) - b, err := json.Marshal(event) + b, err := jsoniter.ConfigFastest.Marshal(event) if err != nil { return "", fmt.Errorf("json.Marshal: %w", err) } diff --git a/relay_test.go b/relay_test.go index 7d64bea..97f5bc1 100644 --- a/relay_test.go +++ b/relay_test.go @@ -3,7 +3,7 @@ package nostr import ( "bytes" "context" - "encoding/json" + stdjson "encoding/json" "io" "net/http" "net/http/httptest" @@ -37,7 +37,7 @@ func TestPublish(t *testing.T) { published = true mu.Unlock() // verify the client sent exactly the textNote - var raw []json.RawMessage + var raw []stdjson.RawMessage err := websocket.JSON.Receive(conn, &raw) assert.NoError(t, err) @@ -67,7 +67,7 @@ func TestPublishBlocked(t *testing.T) { // fake relay server ws := newWebsocketServer(func(conn *websocket.Conn) { // discard received message; not interested - var raw []json.RawMessage + var raw []stdjson.RawMessage err := websocket.JSON.Receive(conn, &raw) assert.NoError(t, err) @@ -192,7 +192,7 @@ func mustRelayConnect(t *testing.T, url string) *Relay { return rl } -func parseEventMessage(t *testing.T, raw []json.RawMessage) Event { +func parseEventMessage(t *testing.T, raw []stdjson.RawMessage) Event { t.Helper() assert.Condition(t, func() (success bool) { @@ -211,7 +211,7 @@ func parseEventMessage(t *testing.T, raw []json.RawMessage) Event { return event } -func parseSubscriptionMessage(t *testing.T, raw []json.RawMessage) (subid string, filters []Filter) { +func parseSubscriptionMessage(t *testing.T, raw []stdjson.RawMessage) (subid string, filters []Filter) { t.Helper() assert.Greater(t, len(raw), 3) diff --git a/sdk/helpers.go b/sdk/helpers.go index dbb36dd..45139d6 100644 --- a/sdk/helpers.go +++ b/sdk/helpers.go @@ -3,8 +3,12 @@ package sdk import ( "slices" "time" + + jsoniter "github.com/json-iterator/go" ) +var json = jsoniter.ConfigFastest + func appendUnique[I comparable](arr []I, item ...I) []I { for _, item := range item { if slices.Contains(arr, item) { diff --git a/sdk/metadata.go b/sdk/metadata.go index 92e46e0..4b73eee 100644 --- a/sdk/metadata.go +++ b/sdk/metadata.go @@ -2,7 +2,6 @@ package sdk import ( "context" - "encoding/json" "fmt" "strings" "sync" diff --git a/tags.go b/tags.go index 3070880..507ce82 100644 --- a/tags.go +++ b/tags.go @@ -1,7 +1,6 @@ package nostr import ( - "encoding/json" "errors" "iter" "slices"