replace all ocurrences of encoding/json with json-iterator so we get rid of HTML escaping and hopefully get faster too.

This commit is contained in:
fiatjaf
2024-12-03 00:49:27 -03:00
parent 6f778f85ba
commit 72d9aac9b1
24 changed files with 39 additions and 35 deletions

View File

@@ -2,7 +2,6 @@ package nostr
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"strconv" "strconv"

View File

@@ -1,7 +1,6 @@
package nostr package nostr
import ( import (
"encoding/json"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"

View File

@@ -1,8 +1,6 @@
package nostr package nostr
import ( import (
json "encoding/json"
easyjson "github.com/mailru/easyjson" easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer" jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter" jwriter "github.com/mailru/easyjson/jwriter"
@@ -10,7 +8,6 @@ import (
// suppress unused package warning // suppress unused package warning
var ( var (
_ *json.RawMessage
_ *jlexer.Lexer _ *jlexer.Lexer
_ *jwriter.Writer _ *jwriter.Writer
_ easyjson.Marshaler _ easyjson.Marshaler

View File

@@ -1,7 +1,6 @@
package nostr package nostr
import ( import (
"encoding/json"
"fmt" "fmt"
"math/rand/v2" "math/rand/v2"
"testing" "testing"

View File

@@ -2,13 +2,13 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io" "io"
"os" "os"
"strings" "strings"
"time" "time"
jsoniter "github.com/json-iterator/go"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
) )
@@ -69,7 +69,7 @@ func main() {
if f, err := os.Create(filename); err == nil { if f, err := os.Create(filename); err == nil {
fmt.Fprintf(os.Stderr, "returned events saved to %s\n", filename) fmt.Fprintf(os.Stderr, "returned events saved to %s\n", filename)
// encode the returned events in a file // encode the returned events in a file
enc := json.NewEncoder(f) enc := jsoniter.NewEncoder(f)
enc.SetIndent("", " ") enc.SetIndent("", " ")
enc.Encode(evs) enc.Encode(evs)
f.Close() f.Close()

View File

@@ -1,7 +1,6 @@
package nostr package nostr
import ( import (
"encoding/json"
"slices" "slices"
"github.com/mailru/easyjson" "github.com/mailru/easyjson"

View File

@@ -1,8 +1,6 @@
package nostr package nostr
import ( import (
json "encoding/json"
easyjson "github.com/mailru/easyjson" easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer" jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter" jwriter "github.com/mailru/easyjson/jwriter"
@@ -10,7 +8,6 @@ import (
// suppress unused package warning // suppress unused package warning
var ( var (
_ *json.RawMessage
_ *jlexer.Lexer _ *jlexer.Lexer
_ *jwriter.Writer _ *jwriter.Writer
_ easyjson.Marshaler _ easyjson.Marshaler

View File

@@ -1,7 +1,6 @@
package nostr package nostr
import ( import (
"encoding/json"
"slices" "slices"
"testing" "testing"

3
go.mod
View File

@@ -13,6 +13,7 @@ require (
github.com/gobwas/ws v1.4.0 github.com/gobwas/ws v1.4.0
github.com/graph-gophers/dataloader/v7 v7.1.0 github.com/graph-gophers/dataloader/v7 v7.1.0
github.com/jmoiron/sqlx v1.3.5 github.com/jmoiron/sqlx v1.3.5
github.com/json-iterator/go v1.1.12
github.com/mailru/easyjson v0.7.7 github.com/mailru/easyjson v0.7.7
github.com/mattn/go-sqlite3 v1.14.18 github.com/mattn/go-sqlite3 v1.14.18
github.com/ncruces/go-sqlite3 v0.18.3 github.com/ncruces/go-sqlite3 v0.18.3
@@ -45,6 +46,8 @@ require (
github.com/kr/pretty v0.3.1 // indirect github.com/kr/pretty v0.3.1 // indirect
github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 // indirect github.com/libsql/sqlite-antlr4-parser v0.0.0-20240327125255-dbf53b6cbf06 // indirect
github.com/mattn/go-isatty v0.0.20 // 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/go-strftime v0.1.9 // indirect
github.com/ncruces/julianday v1.0.0 // indirect github.com/ncruces/julianday v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect

8
go.sum
View File

@@ -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.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 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 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 h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= 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= 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 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= 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/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/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 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 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.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 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI=
github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= 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 h1:tyMa75uh7LcINcfo0WrzOvcTkfz8Hqu0TEPX+KVyes4=
github.com/ncruces/go-sqlite3 v0.18.3/go.mod h1:HAwOtA+cyEX3iN6YmkpQwfT4vMMgCB7rQRFUdOgEFik= github.com/ncruces/go-sqlite3 v0.18.3/go.mod h1:HAwOtA+cyEX3iN6YmkpQwfT4vMMgCB7rQRFUdOgEFik=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= 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/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/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.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.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 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=

View File

@@ -6,12 +6,16 @@ import (
"sync" "sync"
"unsafe" "unsafe"
jsoniter "github.com/json-iterator/go"
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
) )
const MAX_LOCKS = 50 const MAX_LOCKS = 50
var namedMutexPool = make([]sync.Mutex, MAX_LOCKS) var (
namedMutexPool = make([]sync.Mutex, MAX_LOCKS)
json = jsoniter.ConfigFastest
)
//go:noescape //go:noescape
//go:linkname memhash runtime.memhash //go:linkname memhash runtime.memhash

View File

@@ -3,7 +3,6 @@
package nostr package nostr
import ( import (
"encoding/json"
"fmt" "fmt"
) )

View File

@@ -2,12 +2,12 @@ package nip05
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
jsoniter "github.com/json-iterator/go"
"github.com/nbd-wtf/go-nostr" "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() defer res.Body.Close()
var result WellKnownResponse 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) return resp, name, fmt.Errorf("failed to decode json response: %w", err)
} }

View File

@@ -2,11 +2,11 @@ package nip11
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
jsoniter "github.com/json-iterator/go"
"github.com/nbd-wtf/go-nostr" "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() 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) return info, fmt.Errorf("invalid json: %w", err)
} }

View File

@@ -1,7 +1,6 @@
package nip46 package nip46
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"

View File

@@ -2,7 +2,6 @@ package nip46
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
"net/url" "net/url"

View File

@@ -2,7 +2,6 @@ package nip46
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"slices" "slices"
"sync" "sync"

View File

@@ -2,13 +2,16 @@ package nip46
import ( import (
"context" "context"
"encoding/json"
"regexp" "regexp"
jsoniter "github.com/json-iterator/go"
"github.com/nbd-wtf/go-nostr" "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 { type Request struct {
ID string `json:"id"` ID string `json:"id"`

View File

@@ -2,7 +2,6 @@ package nip46
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"slices" "slices"
"sync" "sync"

View File

@@ -6,7 +6,6 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"hash" "hash"
"io" "io"
@@ -14,6 +13,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
jsoniter "github.com/json-iterator/go"
"github.com/nbd-wtf/go-nostr" "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: case http.StatusOK, http.StatusCreated, http.StatusAccepted:
var uploadResp UploadResponse 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 nil, fmt.Errorf("Error decoding JSON: %w", err)
} }
return &uploadResp, nil return &uploadResp, nil
@@ -126,7 +126,7 @@ func generateAuthHeader(sk, host string, fileHash hash.Hash) (string, error) {
} }
event.Sign(sk) event.Sign(sk)
b, err := json.Marshal(event) b, err := jsoniter.ConfigFastest.Marshal(event)
if err != nil { if err != nil {
return "", fmt.Errorf("json.Marshal: %w", err) return "", fmt.Errorf("json.Marshal: %w", err)
} }

View File

@@ -3,7 +3,7 @@ package nostr
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json" stdjson "encoding/json"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@@ -37,7 +37,7 @@ func TestPublish(t *testing.T) {
published = true published = true
mu.Unlock() mu.Unlock()
// verify the client sent exactly the textNote // verify the client sent exactly the textNote
var raw []json.RawMessage var raw []stdjson.RawMessage
err := websocket.JSON.Receive(conn, &raw) err := websocket.JSON.Receive(conn, &raw)
assert.NoError(t, err) assert.NoError(t, err)
@@ -67,7 +67,7 @@ func TestPublishBlocked(t *testing.T) {
// fake relay server // fake relay server
ws := newWebsocketServer(func(conn *websocket.Conn) { ws := newWebsocketServer(func(conn *websocket.Conn) {
// discard received message; not interested // discard received message; not interested
var raw []json.RawMessage var raw []stdjson.RawMessage
err := websocket.JSON.Receive(conn, &raw) err := websocket.JSON.Receive(conn, &raw)
assert.NoError(t, err) assert.NoError(t, err)
@@ -192,7 +192,7 @@ func mustRelayConnect(t *testing.T, url string) *Relay {
return rl return rl
} }
func parseEventMessage(t *testing.T, raw []json.RawMessage) Event { func parseEventMessage(t *testing.T, raw []stdjson.RawMessage) Event {
t.Helper() t.Helper()
assert.Condition(t, func() (success bool) { assert.Condition(t, func() (success bool) {
@@ -211,7 +211,7 @@ func parseEventMessage(t *testing.T, raw []json.RawMessage) Event {
return 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() t.Helper()
assert.Greater(t, len(raw), 3) assert.Greater(t, len(raw), 3)

View File

@@ -3,8 +3,12 @@ package sdk
import ( import (
"slices" "slices"
"time" "time"
jsoniter "github.com/json-iterator/go"
) )
var json = jsoniter.ConfigFastest
func appendUnique[I comparable](arr []I, item ...I) []I { func appendUnique[I comparable](arr []I, item ...I) []I {
for _, item := range item { for _, item := range item {
if slices.Contains(arr, item) { if slices.Contains(arr, item) {

View File

@@ -2,7 +2,6 @@ package sdk
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"sync" "sync"

View File

@@ -1,7 +1,6 @@
package nostr package nostr
import ( import (
"encoding/json"
"errors" "errors"
"iter" "iter"
"slices" "slices"