Merge pull request #343 from crosbymichael/marshal-performance

Improve json encoding performance for specs-go
This commit is contained in:
Vincent Batts 2016-03-16 21:35:28 -04:00
commit a306c5841f
4 changed files with 12567 additions and 0 deletions

View File

@ -56,6 +56,11 @@ test: .govet .golint .gitvalidation
.gitvalidation:
git-validation -q -run DCO,short-subject -v -range $(EPOCH_TEST_COMMIT)..HEAD
# `go get https://github.com/pquerna/ffjson`
ffjson:
cd specs-go/ && ffjson config.go
cd specs-go/ && ffjson state.go
clean:
rm -rf output/ *~

12062
specs-go/config_ffjson.go Normal file

File diff suppressed because it is too large Load Diff

149
specs-go/serialize_test.go Normal file
View File

@ -0,0 +1,149 @@
package specs
import (
"encoding/json"
"runtime"
"testing"
"github.com/pquerna/ffjson/ffjson"
)
func init() {
data, err := json.Marshal(spec)
if err != nil {
panic(err)
}
encoded = data
}
var encoded []byte
var rwm = "rwm"
// sample spec for testing marshaling performance
var spec = &Spec{
Version: Version,
Platform: Platform{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
},
Root: Root{
Path: "rootfs",
Readonly: true,
},
Process: Process{
Terminal: true,
User: User{},
Args: []string{
"sh",
},
Env: []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TERM=xterm",
},
Cwd: "/",
NoNewPrivileges: true,
Capabilities: []string{
"CAP_AUDIT_WRITE",
"CAP_KILL",
"CAP_NET_BIND_SERVICE",
},
Rlimits: []Rlimit{
{
Type: "RLIMIT_NOFILE",
Hard: uint64(1024),
Soft: uint64(1024),
},
},
},
Hostname: "runc",
Mounts: []Mount{
{
Destination: "/proc",
Type: "proc",
Source: "proc",
Options: nil,
},
{
Destination: "/dev",
Type: "tmpfs",
Source: "tmpfs",
Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
},
{
Destination: "/dev/pts",
Type: "devpts",
Source: "devpts",
Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
},
{
Destination: "/dev/shm",
Type: "tmpfs",
Source: "shm",
Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
},
{
Destination: "/dev/mqueue",
Type: "mqueue",
Source: "mqueue",
Options: []string{"nosuid", "noexec", "nodev"},
},
{
Destination: "/sys",
Type: "sysfs",
Source: "sysfs",
Options: []string{"nosuid", "noexec", "nodev", "ro"},
},
{
Destination: "/sys/fs/cgroup",
Type: "cgroup",
Source: "cgroup",
Options: []string{"nosuid", "noexec", "nodev", "relatime", "ro"},
},
},
Linux: Linux{
Resources: &Resources{
Devices: []DeviceCgroup{
{
Allow: false,
Access: &rwm,
},
},
},
Namespaces: []Namespace{
{
Type: "pid",
},
{
Type: "network",
},
{
Type: "ipc",
},
{
Type: "uts",
},
{
Type: "mount",
},
},
},
}
func BenchmarkMarshalSpec(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ffjson.Marshal(spec)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkUnmarshal(b *testing.B) {
for i := 0; i < b.N; i++ {
var temp Spec
if err := ffjson.Unmarshal(encoded, &temp); err != nil {
b.Fatal(err)
}
}
}

351
specs-go/state_ffjson.go Normal file
View File

@ -0,0 +1,351 @@
// DO NOT EDIT!
// Code generated by ffjson <https://github.com/pquerna/ffjson>
// source: state.go
// DO NOT EDIT!
package specs
import (
"bytes"
"fmt"
fflib "github.com/pquerna/ffjson/fflib/v1"
)
func (mj *State) MarshalJSON() ([]byte, error) {
var buf fflib.Buffer
if mj == nil {
buf.WriteString("null")
return buf.Bytes(), nil
}
err := mj.MarshalJSONBuf(&buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (mj *State) MarshalJSONBuf(buf fflib.EncodingBuffer) error {
if mj == nil {
buf.WriteString("null")
return nil
}
var err error
var obj []byte
_ = obj
_ = err
buf.WriteString(`{"version":`)
fflib.WriteJsonString(buf, string(mj.Version))
buf.WriteString(`,"id":`)
fflib.WriteJsonString(buf, string(mj.ID))
buf.WriteString(`,"pid":`)
fflib.FormatBits2(buf, uint64(mj.Pid), 10, mj.Pid < 0)
buf.WriteString(`,"bundlePath":`)
fflib.WriteJsonString(buf, string(mj.BundlePath))
buf.WriteByte('}')
return nil
}
const (
ffj_t_Statebase = iota
ffj_t_Stateno_such_key
ffj_t_State_Version
ffj_t_State_ID
ffj_t_State_Pid
ffj_t_State_BundlePath
)
var ffj_key_State_Version = []byte("version")
var ffj_key_State_ID = []byte("id")
var ffj_key_State_Pid = []byte("pid")
var ffj_key_State_BundlePath = []byte("bundlePath")
func (uj *State) UnmarshalJSON(input []byte) error {
fs := fflib.NewFFLexer(input)
return uj.UnmarshalJSONFFLexer(fs, fflib.FFParse_map_start)
}
func (uj *State) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error {
var err error = nil
currentKey := ffj_t_Statebase
_ = currentKey
tok := fflib.FFTok_init
wantedTok := fflib.FFTok_init
mainparse:
for {
tok = fs.Scan()
// println(fmt.Sprintf("debug: tok: %v state: %v", tok, state))
if tok == fflib.FFTok_error {
goto tokerror
}
switch state {
case fflib.FFParse_map_start:
if tok != fflib.FFTok_left_bracket {
wantedTok = fflib.FFTok_left_bracket
goto wrongtokenerror
}
state = fflib.FFParse_want_key
continue
case fflib.FFParse_after_value:
if tok == fflib.FFTok_comma {
state = fflib.FFParse_want_key
} else if tok == fflib.FFTok_right_bracket {
goto done
} else {
wantedTok = fflib.FFTok_comma
goto wrongtokenerror
}
case fflib.FFParse_want_key:
// json {} ended. goto exit. woo.
if tok == fflib.FFTok_right_bracket {
goto done
}
if tok != fflib.FFTok_string {
wantedTok = fflib.FFTok_string
goto wrongtokenerror
}
kn := fs.Output.Bytes()
if len(kn) <= 0 {
// "" case. hrm.
currentKey = ffj_t_Stateno_such_key
state = fflib.FFParse_want_colon
goto mainparse
} else {
switch kn[0] {
case 'b':
if bytes.Equal(ffj_key_State_BundlePath, kn) {
currentKey = ffj_t_State_BundlePath
state = fflib.FFParse_want_colon
goto mainparse
}
case 'i':
if bytes.Equal(ffj_key_State_ID, kn) {
currentKey = ffj_t_State_ID
state = fflib.FFParse_want_colon
goto mainparse
}
case 'p':
if bytes.Equal(ffj_key_State_Pid, kn) {
currentKey = ffj_t_State_Pid
state = fflib.FFParse_want_colon
goto mainparse
}
case 'v':
if bytes.Equal(ffj_key_State_Version, kn) {
currentKey = ffj_t_State_Version
state = fflib.FFParse_want_colon
goto mainparse
}
}
if fflib.SimpleLetterEqualFold(ffj_key_State_BundlePath, kn) {
currentKey = ffj_t_State_BundlePath
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffj_key_State_Pid, kn) {
currentKey = ffj_t_State_Pid
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffj_key_State_ID, kn) {
currentKey = ffj_t_State_ID
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.EqualFoldRight(ffj_key_State_Version, kn) {
currentKey = ffj_t_State_Version
state = fflib.FFParse_want_colon
goto mainparse
}
currentKey = ffj_t_Stateno_such_key
state = fflib.FFParse_want_colon
goto mainparse
}
case fflib.FFParse_want_colon:
if tok != fflib.FFTok_colon {
wantedTok = fflib.FFTok_colon
goto wrongtokenerror
}
state = fflib.FFParse_want_value
continue
case fflib.FFParse_want_value:
if tok == fflib.FFTok_left_brace || tok == fflib.FFTok_left_bracket || tok == fflib.FFTok_integer || tok == fflib.FFTok_double || tok == fflib.FFTok_string || tok == fflib.FFTok_bool || tok == fflib.FFTok_null {
switch currentKey {
case ffj_t_State_Version:
goto handle_Version
case ffj_t_State_ID:
goto handle_ID
case ffj_t_State_Pid:
goto handle_Pid
case ffj_t_State_BundlePath:
goto handle_BundlePath
case ffj_t_Stateno_such_key:
err = fs.SkipField(tok)
if err != nil {
return fs.WrapErr(err)
}
state = fflib.FFParse_after_value
goto mainparse
}
} else {
goto wantedvalue
}
}
}
handle_Version:
/* handler: uj.Version type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
uj.Version = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_ID:
/* handler: uj.ID type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
uj.ID = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Pid:
/* handler: uj.Pid type=int kind=int quoted=false*/
{
if tok != fflib.FFTok_integer && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok))
}
}
{
if tok == fflib.FFTok_null {
} else {
tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64)
if err != nil {
return fs.WrapErr(err)
}
uj.Pid = int(tval)
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_BundlePath:
/* handler: uj.BundlePath type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
uj.BundlePath = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
wantedvalue:
return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok))
wrongtokenerror:
return fs.WrapErr(fmt.Errorf("ffjson: wanted token: %v, but got token: %v output=%s", wantedTok, tok, fs.Output.String()))
tokerror:
if fs.BigError != nil {
return fs.WrapErr(fs.BigError)
}
err = fs.Error.ToError()
if err != nil {
return fs.WrapErr(err)
}
panic("ffjson-generated: unreachable, please report bug.")
done:
return nil
}