From 1fa1bdca4cd8e649e8c56b7f95f3da970d8f8ed1 Mon Sep 17 00:00:00 2001 From: Goran Rojovic Date: Tue, 7 Nov 2023 14:40:46 +0100 Subject: [PATCH] Introduce json-iterator in dispatcher for faster work with json --- .github/dependabot.yml | 4 ++-- go.mod | 3 +++ go.sum | 2 ++ jsonrpc/dispatcher.go | 25 +++++++++++++++---------- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 6a2929c9a3..2d4f711a31 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -4,11 +4,11 @@ updates: directory: / target-branch: "develop" schedule: - interval: weekly + interval: monthly ignore: - dependency-name: "github.com/aws/aws-sdk-go" update-types: [ "version-update:semver-patch" ] - open-pull-requests-limit: 10 + open-pull-requests-limit: 20 pull-request-branch-name: separator: "-" reviewers: diff --git a/go.mod b/go.mod index 8e527d455e..5a469a5187 100644 --- a/go.mod +++ b/go.mod @@ -65,6 +65,7 @@ require ( ) require ( + github.com/json-iterator/go v1.1.12 github.com/quasilyte/go-ruleguard v0.4.0 github.com/quasilyte/go-ruleguard/dsl v0.3.22 github.com/sethvargo/go-retry v0.2.4 @@ -87,6 +88,8 @@ require ( github.com/hashicorp/golang-lru/v2 v2.0.3 // indirect github.com/ipfs/boxo v0.8.1 // indirect github.com/libp2p/go-yamux/v4 v4.0.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/onsi/ginkgo/v2 v2.9.2 // indirect github.com/outcaste-io/ristretto v0.2.3 // indirect github.com/quic-go/qpack v0.4.0 // indirect diff --git a/go.sum b/go.sum index 6ae83bf3ce..57b2067daa 100644 --- a/go.sum +++ b/go.sum @@ -378,6 +378,7 @@ github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlT github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 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/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= @@ -481,6 +482,7 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 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/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= diff --git a/jsonrpc/dispatcher.go b/jsonrpc/dispatcher.go index 6d234ccd91..bee6f38dc9 100644 --- a/jsonrpc/dispatcher.go +++ b/jsonrpc/dispatcher.go @@ -2,7 +2,6 @@ package jsonrpc import ( "bytes" - "encoding/json" "errors" "fmt" "math" @@ -14,6 +13,12 @@ import ( "github.com/armon/go-metrics" "github.com/hashicorp/go-hclog" + jsonIter "github.com/json-iterator/go" +) + +var ( + jsonIt = jsonIter.ConfigCompatibleWithStandardLibrary + fastJsonIt = jsonIter.ConfigFastest ) type serviceData struct { @@ -186,7 +191,7 @@ func formatID(id interface{}) (interface{}, Error) { func (d *Dispatcher) handleSubscribe(req Request, conn wsConn) (string, Error) { var params []interface{} - if err := json.Unmarshal(req.Params, ¶ms); err != nil { + if err := jsonIt.Unmarshal(req.Params, ¶ms); err != nil { return "", NewInvalidRequestError("Invalid json request") } @@ -219,7 +224,7 @@ func (d *Dispatcher) handleSubscribe(req Request, conn wsConn) (string, Error) { func (d *Dispatcher) handleUnsubscribe(req Request) (bool, Error) { var params []interface{} - if err := json.Unmarshal(req.Params, ¶ms); err != nil { + if err := jsonIt.Unmarshal(req.Params, ¶ms); err != nil { return false, NewInvalidRequestError("Invalid json request") } @@ -252,7 +257,7 @@ func (d *Dispatcher) HandleWs(reqBody []byte, conn wsConn) ([]byte, error) { if len(reqBody) > 0 && reqBody[0] == openSquareBracket { var batchReq BatchRequest - err := json.Unmarshal(reqBody, &batchReq) + err := jsonIt.Unmarshal(reqBody, &batchReq) if err != nil { return NewRPCResponse(nil, "2.0", nil, NewInvalidRequestError("Invalid json batch request")).Bytes() @@ -289,7 +294,7 @@ func (d *Dispatcher) HandleWs(reqBody []byte, conn wsConn) ([]byte, error) { } var req Request - if err := json.Unmarshal(reqBody, &req); err != nil { + if err := jsonIt.Unmarshal(reqBody, &req); err != nil { return NewRPCResponse(req.ID, "2.0", nil, NewInvalidRequestError("Invalid json request")).Bytes() } @@ -334,7 +339,7 @@ func (d *Dispatcher) Handle(reqBody []byte) ([]byte, error) { if x[0] == '{' { var req Request - if err := json.Unmarshal(reqBody, &req); err != nil { + if err := jsonIt.Unmarshal(reqBody, &req); err != nil { return NewRPCResponse(nil, "2.0", nil, NewInvalidRequestError("Invalid json request")).Bytes() } @@ -349,7 +354,7 @@ func (d *Dispatcher) Handle(reqBody []byte) ([]byte, error) { // handle batch requests var requests BatchRequest - if err := json.Unmarshal(reqBody, &requests); err != nil { + if err := jsonIt.Unmarshal(reqBody, &requests); err != nil { return NewRPCResponse( nil, "2.0", @@ -383,7 +388,7 @@ func (d *Dispatcher) Handle(reqBody []byte) ([]byte, error) { responses = append(responses, resp) } - respBytes, err := json.Marshal(responses) + respBytes, err := jsonIt.Marshal(responses) if err != nil { return NewRPCResponse(nil, "2.0", nil, NewInternalError("Internal error")).Bytes() } @@ -411,7 +416,7 @@ func (d *Dispatcher) handleReq(req Request) ([]byte, Error) { } if fd.numParams() > 0 { - if err := json.Unmarshal(req.Params, &inputs); err != nil { + if err := jsonIt.Unmarshal(req.Params, &inputs); err != nil { return nil, NewInvalidParamsError("Invalid Params") } } @@ -444,7 +449,7 @@ func (d *Dispatcher) handleReq(req Request) ([]byte, Error) { } if res := output[0].Interface(); res != nil { - data, err = json.Marshal(res) + data, err = fastJsonIt.Marshal(res) if err != nil { d.logInternalError(req.Method, err)