Use jsoniteer for faster json encoding/decoding

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2018-01-26 16:31:54 -05:00
parent 43a9473a53
commit 4233b87b89
11 changed files with 33 additions and 11 deletions

View File

@ -2,7 +2,6 @@ package containerd
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
@ -14,6 +13,7 @@ import (
"github.com/containerd/containerd/errdefs"
"github.com/containerd/typeurl"
prototypes "github.com/gogo/protobuf/types"
jsoniter "github.com/json-iterator/go"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
@ -115,6 +115,7 @@ func (c *container) Spec(ctx context.Context) (*specs.Spec, error) {
if err != nil {
return nil, err
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
var s specs.Spec
if err := json.Unmarshal(r.Spec.Value, &s); err != nil {
return nil, err

View File

@ -4,7 +4,6 @@ package containerd
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
@ -20,6 +19,7 @@ import (
"github.com/containerd/containerd/platforms"
"github.com/gogo/protobuf/proto"
protobuf "github.com/gogo/protobuf/types"
jsoniter "github.com/json-iterator/go"
digest "github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/identity"
"github.com/opencontainers/image-spec/specs-go/v1"
@ -121,6 +121,7 @@ func decodeIndex(ctx context.Context, store content.Store, id digest.Digest) (*v
if err != nil {
return nil, err
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.Unmarshal(p, &index); err != nil {
return nil, err
}

View File

@ -4,12 +4,12 @@ package seccomp
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
jsoniter "github.com/json-iterator/go"
"github.com/opencontainers/runtime-spec/specs-go"
)
@ -23,6 +23,7 @@ func WithProfile(profile string) oci.SpecOpts {
if err != nil {
return fmt.Errorf("Cannot load seccomp profile %q: %v", profile, err)
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.Unmarshal(f, s.Linux.Seccomp); err != nil {
return fmt.Errorf("Decoding seccomp profile failed %q: %v", profile, err)
}

View File

@ -2,7 +2,6 @@ package images
import (
"context"
"encoding/json"
"strings"
"time"
@ -10,6 +9,7 @@ import (
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/platforms"
jsoniter "github.com/json-iterator/go"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
@ -123,6 +123,7 @@ func Manifest(ctx context.Context, provider content.Provider, image ocispec.Desc
return ocispec.Manifest{}, err
}
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
if err := Walk(ctx, HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
switch desc.MediaType {
@ -216,6 +217,7 @@ func Config(ctx context.Context, provider content.Provider, image ocispec.Descri
// Platforms returns one or more platforms supported by the image.
func Platforms(ctx context.Context, provider content.Provider, image ocispec.Descriptor) ([]ocispec.Platform, error) {
var platformSpecs []ocispec.Platform
json := jsoniter.ConfigCompatibleWithStandardLibrary
return platformSpecs, Walk(ctx, Handlers(HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if desc.Platform != nil {
platformSpecs = append(platformSpecs, *desc.Platform)
@ -286,6 +288,7 @@ func Check(ctx context.Context, provider content.Provider, image ocispec.Descrip
// Children returns the immediate children of content described by the descriptor.
func Children(ctx context.Context, provider content.Provider, desc ocispec.Descriptor, platform string) ([]ocispec.Descriptor, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
var descs []ocispec.Descriptor
switch desc.MediaType {
case MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:
@ -354,6 +357,7 @@ func RootFS(ctx context.Context, provider content.Provider, configDesc ocispec.D
return nil, err
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
var config ocispec.Image
if err := json.Unmarshal(p, &config); err != nil {
return nil, err

View File

@ -3,13 +3,13 @@ package oci
import (
"archive/tar"
"context"
"encoding/json"
"io"
"sort"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms"
jsoniter "github.com/json-iterator/go"
ocispecs "github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
@ -116,6 +116,8 @@ func ociLayoutFile(version string) tarRecord {
Version: version,
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
b, err := json.Marshal(layout)
if err != nil {
panic(err)
@ -144,6 +146,7 @@ func ociIndexRecord(manifests ...ocispec.Descriptor) tarRecord {
Manifests: manifests,
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
b, err := json.Marshal(index)
if err != nil {
panic(err)

View File

@ -4,7 +4,6 @@ package oci
import (
"archive/tar"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
@ -14,6 +13,7 @@ import (
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
jsoniter "github.com/json-iterator/go"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
@ -83,6 +83,7 @@ func onUntarIndexJSON(r io.Reader, imageName string) ([]images.Image, error) {
if err != nil {
return nil, err
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
var idx ocispec.Index
if err := json.Unmarshal(b, &idx); err != nil {
return nil, err
@ -131,6 +132,7 @@ func onUntarBlob(ctx context.Context, r io.Reader, store content.Store, name str
// - images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest
// - images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex
func GetChildrenDescriptors(r io.Reader, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
switch desc.MediaType {
case images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:
var manifest ocispec.Manifest

View File

@ -4,7 +4,6 @@ package proc
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
@ -22,6 +21,7 @@ import (
runc "github.com/containerd/go-runc"
"github.com/containerd/typeurl"
google_protobuf "github.com/gogo/protobuf/types"
jsoniter "github.com/json-iterator/go"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
@ -349,6 +349,7 @@ func (p *Init) Runtime() *runc.Runc {
// exec returns a new exec'd process
func (p *Init) exec(context context.Context, path string, r *ExecConfig) (Process, error) {
// process exec request
json := jsoniter.ConfigCompatibleWithStandardLibrary
var spec specs.Process
if err := json.Unmarshal(r.Spec.Value, &spec); err != nil {
return nil, err
@ -406,6 +407,7 @@ func (p *Init) checkpoint(context context.Context, r *CheckpointConfig) error {
}
func (p *Init) update(context context.Context, r *google_protobuf.Any) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
var resources specs.LinuxResources
if err := json.Unmarshal(r.Value, &resources); err != nil {
return err

View File

@ -1,11 +1,11 @@
package mount
import (
"encoding/json"
"path/filepath"
"strings"
"github.com/Microsoft/hcsshim"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
@ -49,6 +49,7 @@ const ParentLayerPathsFlag = "parentLayerPaths="
// GetParentPaths of the mount
func (m *Mount) GetParentPaths() ([]string, error) {
var parentLayerPaths []string
json := jsoniter.ConfigCompatibleWithStandardLibrary
for _, option := range m.Options {
if strings.HasPrefix(option, ParentLayerPathsFlag) {
err := json.Unmarshal([]byte(option[len(ParentLayerPathsFlag):]), &parentLayerPaths)

View File

@ -4,7 +4,6 @@ package oci
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
@ -17,6 +16,7 @@ import (
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/namespaces"
jsoniter "github.com/json-iterator/go"
"github.com/opencontainers/image-spec/specs-go/v1"
"github.com/opencontainers/runc/libcontainer/user"
specs "github.com/opencontainers/runtime-spec/specs-go"
@ -66,6 +66,7 @@ func WithLinuxNamespace(ns specs.LinuxNamespace) SpecOpts {
// WithImageConfig configures the spec to from the configuration of an Image
func WithImageConfig(image Image) SpecOpts {
return func(ctx context.Context, client Client, c *containers.Container, s *specs.Spec) error {
json := jsoniter.ConfigCompatibleWithStandardLibrary
ic, err := image.Config(ctx)
if err != nil {
return err

View File

@ -4,12 +4,12 @@ package oci
import (
"context"
"encoding/json"
"fmt"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
jsoniter "github.com/json-iterator/go"
"github.com/opencontainers/image-spec/specs-go/v1"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
@ -24,6 +24,7 @@ func WithImageConfig(image Image) SpecOpts {
var (
ociimage v1.Image
config v1.ImageConfig
json = jsoniter.ConfigCompatibleWithStandardLibrary
)
switch ic.MediaType {
case v1.MediaTypeImageConfig, images.MediaTypeDockerSchema2Config:

View File

@ -5,7 +5,6 @@ import (
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
@ -21,6 +20,7 @@ import (
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/remotes"
jsoniter "github.com/json-iterator/go"
digest "github.com/opencontainers/go-digest"
specs "github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@ -110,6 +110,7 @@ func (c *Converter) Convert(ctx context.Context) (ocispec.Descriptor, error) {
return ocispec.Descriptor{}, errors.Wrap(err, "schema 1 conversion failed")
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
var img ocispec.Image
if err := json.Unmarshal([]byte(c.pulledManifest.History[0].V1Compatibility), &img); err != nil {
return ocispec.Descriptor{}, errors.Wrap(err, "failed to unmarshal image from schema 1 history")
@ -194,6 +195,7 @@ func (c *Converter) fetchManifest(ctx context.Context, desc ocispec.Descriptor)
return err
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
var m manifest
if err := json.Unmarshal(b, &m); err != nil {
return err
@ -316,6 +318,7 @@ func (c *Converter) schema1ManifestHistory() ([]ocispec.History, []digest.Digest
return nil, nil, errors.New("no history")
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
history := make([]ocispec.History, len(m.History))
diffIDs := []digest.Digest{}
for i := range m.History {
@ -373,6 +376,7 @@ type v1History struct {
// empty layer. A return value of true indicates the layer is empty,
// however false does not indicate non-empty.
func isEmptyLayer(compatHistory []byte) (bool, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
var h v1History
if err := json.Unmarshal(compatHistory, &h); err != nil {
return false, err
@ -422,6 +426,7 @@ func joseBase64UrlDecode(s string) ([]byte, error) {
}
func stripSignature(b []byte) ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
var sig signature
if err := json.Unmarshal(b, &sig); err != nil {
return nil, err