Skip to content

Commit

Permalink
replace all ocurrences of encoding/json with json-iterator so we get …
Browse files Browse the repository at this point in the history
…rid of HTML escaping and hopefully get faster too.
  • Loading branch information
fiatjaf committed Dec 3, 2024
1 parent 6f778f8 commit 72d9aac
Show file tree
Hide file tree
Showing 24 changed files with 39 additions and 35 deletions.
1 change: 0 additions & 1 deletion envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nostr

import (
"bytes"
"encoding/json"
"fmt"
"strconv"

Expand Down
1 change: 0 additions & 1 deletion envelopes_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nostr

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down
3 changes: 0 additions & 3 deletions event_easyjson.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package nostr

import (
json "encoding/json"

easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)

// suppress unused package warning
var (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler
Expand Down
1 change: 0 additions & 1 deletion event_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nostr

import (
"encoding/json"
"fmt"
"math/rand/v2"
"testing"
Expand Down
4 changes: 2 additions & 2 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nostr

import (
"encoding/json"
"slices"

"github.com/mailru/easyjson"
Expand Down
3 changes: 0 additions & 3 deletions filter_easyjson.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package nostr

import (
json "encoding/json"

easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)

// suppress unused package warning
var (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler
Expand Down
1 change: 0 additions & 1 deletion filter_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nostr

import (
"encoding/json"
"slices"
"testing"

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
6 changes: 5 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion log_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package nostr

import (
"encoding/json"
"fmt"
)

Expand Down
4 changes: 2 additions & 2 deletions nip05/nip05.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions nip11/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}

Expand Down
1 change: 0 additions & 1 deletion nip46/bunker_session.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nip46

import (
"encoding/json"
"fmt"

"github.com/nbd-wtf/go-nostr"
Expand Down
1 change: 0 additions & 1 deletion nip46/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nip46

import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net/url"
Expand Down
1 change: 0 additions & 1 deletion nip46/dynamic-signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nip46

import (
"context"
"encoding/json"
"fmt"
"slices"
"sync"
Expand Down
7 changes: 5 additions & 2 deletions nip46/nip46.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 0 additions & 1 deletion nip46/static-key-signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nip46

import (
"context"
"encoding/json"
"fmt"
"slices"
"sync"
Expand Down
6 changes: 3 additions & 3 deletions nip96/nip96.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"hash"
"io"
"mime/multipart"
"net/http"
"strconv"

jsoniter "github.com/json-iterator/go"
"github.com/nbd-wtf/go-nostr"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nostr
import (
"bytes"
"context"
"encoding/json"
stdjson "encoding/json"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions sdk/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion sdk/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sdk

import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
Expand Down
1 change: 0 additions & 1 deletion tags.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nostr

import (
"encoding/json"
"errors"
"iter"
"slices"
Expand Down

0 comments on commit 72d9aac

Please sign in to comment.