Use jsoniter whenever applicable instead of encoding/json (#8766)

This PR adds jsoniter package to replace encoding/json
in places where faster json unmarshal is necessary
whenever input JSON is large enough.

Some benchmarking comparison between jsoniter and enconding/json

benchmark                            old MB/s     new MB/s     speedup
BenchmarkParseUnmarshal/N10-4        110.02       331.17       3.01x
BenchmarkParseUnmarshal/N100-4       125.74       524.09       4.17x
BenchmarkParseUnmarshal/N500-4       131.68       542.60       4.12x
BenchmarkParseUnmarshal/N1000-4      133.93       514.88       3.84x
BenchmarkParseUnmarshal/N5000-4      122.10       415.36       3.40x
BenchmarkParseUnmarshal/N10000-4     132.13       403.90       3.06x
master
Harshavardhana 5 years ago committed by Nitish Tiwari
parent 60813bef29
commit aa2e89bfe3
  1. 2
      cmd/config.go
  2. 2
      cmd/data-usage.go
  3. 3
      cmd/format-disk-cache.go
  4. 1
      cmd/fs-v1-metadata.go
  5. 3
      cmd/policy.go
  6. 2
      cmd/xl-v1-metadata.go
  7. 3
      cmd/xl-v1-utils.go

@ -26,6 +26,7 @@ import (
"strings" "strings"
"time" "time"
jsoniter "github.com/json-iterator/go"
"github.com/minio/minio/cmd/config" "github.com/minio/minio/cmd/config"
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/minio/pkg/madmin" "github.com/minio/minio/pkg/madmin"
@ -170,6 +171,7 @@ func readServerConfig(ctx context.Context, objAPI ObjectLayer) (config.Config, e
} }
var config = config.New() var config = config.New()
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if err = json.Unmarshal(configData, &config); err != nil { if err = json.Unmarshal(configData, &config); err != nil {
return nil, err return nil, err
} }

@ -22,6 +22,7 @@ import (
"encoding/json" "encoding/json"
"time" "time"
jsoniter "github.com/json-iterator/go"
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/minio/pkg/hash" "github.com/minio/minio/pkg/hash"
) )
@ -133,6 +134,7 @@ func loadDataUsageFromBackend(ctx context.Context, objAPI ObjectLayer) (DataUsag
} }
var dataUsageInfo DataUsageInfo var dataUsageInfo DataUsageInfo
var json = jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal(dataUsageInfoJSON.Bytes(), &dataUsageInfo) err = json.Unmarshal(dataUsageInfoJSON.Bytes(), &dataUsageInfo)
if err != nil { if err != nil {
return DataUsageInfo{}, err return DataUsageInfo{}, err

@ -18,7 +18,6 @@ package cmd
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -29,6 +28,7 @@ import (
"reflect" "reflect"
"strings" "strings"
jsoniter "github.com/json-iterator/go"
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/sio" "github.com/minio/sio"
) )
@ -444,6 +444,7 @@ func migrateOldCache(ctx context.Context, c *diskCache) error {
} }
// marshal cache metadata after adding version and stat info // marshal cache metadata after adding version and stat info
meta := &cacheMeta{} meta := &cacheMeta{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if err = json.Unmarshal(metaBytes, &meta); err != nil { if err = json.Unmarshal(metaBytes, &meta); err != nil {
return err return err
} }

@ -90,6 +90,7 @@ func (c *FSChecksumInfoV1) UnmarshalJSON(data []byte) error {
} }
var info checksuminfo var info checksuminfo
var json = jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(data, &info) err := json.Unmarshal(data, &info)
if err != nil { if err != nil {
return err return err

@ -26,6 +26,7 @@ import (
"strings" "strings"
"sync" "sync"
jsoniter "github.com/json-iterator/go"
miniogopolicy "github.com/minio/minio-go/v6/pkg/policy" miniogopolicy "github.com/minio/minio-go/v6/pkg/policy"
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/minio/pkg/event" "github.com/minio/minio/pkg/event"
@ -275,6 +276,7 @@ func PolicyToBucketAccessPolicy(bucketPolicy *policy.Policy) (*miniogopolicy.Buc
} }
var policyInfo miniogopolicy.BucketAccessPolicy var policyInfo miniogopolicy.BucketAccessPolicy
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if err = json.Unmarshal(data, &policyInfo); err != nil { if err = json.Unmarshal(data, &policyInfo); err != nil {
// This should not happen because data is valid to JSON data. // This should not happen because data is valid to JSON data.
return nil, err return nil, err
@ -292,6 +294,7 @@ func BucketAccessPolicyToPolicy(policyInfo *miniogopolicy.BucketAccessPolicy) (*
} }
var bucketPolicy policy.Policy var bucketPolicy policy.Policy
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if err = json.Unmarshal(data, &bucketPolicy); err != nil { if err = json.Unmarshal(data, &bucketPolicy); err != nil {
// This should not happen because data is valid to JSON data. // This should not happen because data is valid to JSON data.
return nil, err return nil, err

@ -26,6 +26,7 @@ import (
"sort" "sort"
"time" "time"
jsoniter "github.com/json-iterator/go"
xhttp "github.com/minio/minio/cmd/http" xhttp "github.com/minio/minio/cmd/http"
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/minio/pkg/sync/errgroup" "github.com/minio/minio/pkg/sync/errgroup"
@ -77,6 +78,7 @@ func (c ChecksumInfo) MarshalJSON() ([]byte, error) {
// UnmarshalJSON - should never be called, instead xlMetaV1UnmarshalJSON() should be used. // UnmarshalJSON - should never be called, instead xlMetaV1UnmarshalJSON() should be used.
func (c *ChecksumInfo) UnmarshalJSON(data []byte) error { func (c *ChecksumInfo) UnmarshalJSON(data []byte) error {
var info checksumInfoJSON var info checksumInfoJSON
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.Unmarshal(data, &info); err != nil { if err := json.Unmarshal(data, &info); err != nil {
return err return err
} }

@ -18,11 +18,11 @@ package cmd
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"hash/crc32" "hash/crc32"
"path" "path"
jsoniter "github.com/json-iterator/go"
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/minio/pkg/sync/errgroup" "github.com/minio/minio/pkg/sync/errgroup"
) )
@ -117,6 +117,7 @@ func hashOrder(key string, cardinality int) []int {
// Constructs xlMetaV1 using `jsoniter` lib. // Constructs xlMetaV1 using `jsoniter` lib.
func xlMetaV1UnmarshalJSON(ctx context.Context, xlMetaBuf []byte) (xlMeta xlMetaV1, err error) { func xlMetaV1UnmarshalJSON(ctx context.Context, xlMetaBuf []byte) (xlMeta xlMetaV1, err error) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
err = json.Unmarshal(xlMetaBuf, &xlMeta) err = json.Unmarshal(xlMetaBuf, &xlMeta)
return xlMeta, err return xlMeta, err
} }

Loading…
Cancel
Save