mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 13:22:56 +01:00
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:
parent
6f778f85ba
commit
72d9aac9b1
@ -2,7 +2,6 @@ package nostr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -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
|
||||
|
@ -1,7 +1,6 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"testing"
|
||||
|
@ -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()
|
||||
|
@ -1,7 +1,6 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"slices"
|
||||
|
||||
"github.com/mailru/easyjson"
|
||||
|
@ -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
|
||||
|
@ -1,7 +1,6 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
|
3
go.mod
3
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
|
||||
|
8
go.sum
8
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=
|
||||
|
@ -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
|
||||
|
@ -3,7 +3,6 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
package nip46
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
|
@ -2,7 +2,6 @@ package nip46
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
|
@ -2,7 +2,6 @@ package nip46
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
"sync"
|
||||
|
@ -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"`
|
||||
|
@ -2,7 +2,6 @@ package nip46
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
"sync"
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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) {
|
||||
|
@ -2,7 +2,6 @@ package sdk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
1
tags.go
1
tags.go
@ -1,7 +1,6 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"iter"
|
||||
"slices"
|
||||
|
Loading…
x
Reference in New Issue
Block a user