Merge pull request #101 from goccy/feature/fix-marshaler-performance

Fix encoding of type which implemented MarshalJSON
This commit is contained in:
Masaaki Goshima 2021-01-30 01:55:40 +09:00 committed by GitHub
commit 73dac85d26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 253 additions and 101 deletions

View File

@ -603,3 +603,60 @@ func Benchmark_Encode_Int_GoJson(b *testing.B) {
}
}
}
type marshaler struct{}
func (*marshaler) MarshalJSON() ([]byte, error) {
return []byte(`"hello"`), nil
}
func Benchmark_Encode_MarshalJSON_EncodingJson(b *testing.B) {
v := &marshaler{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MarshalJSON_JsonIter(b *testing.B) {
v := &marshaler{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := json.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MarshalJSON_Jettison(b *testing.B) {
v := &marshaler{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := jettison.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MarshalJSON_SegmentioJson(b *testing.B) {
v := &marshaler{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := segmentiojson.Marshal(v); err != nil {
b.Fatal(err)
}
}
}
func Benchmark_Encode_MarshalJSON_GoJson(b *testing.B) {
v := &marshaler{}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := gojson.Marshal(v); err != nil {
b.Fatal(err)
}
}
}

View File

@ -268,11 +268,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
0,
)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(append(b, buf.Bytes()...), ',')
b = buf.Bytes()
b = encodeComma(b)
code = code.next
case opMarshalText:
ptr := load(ctxptr, code.idx)
@ -6632,11 +6634,12 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
0,
)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
}
@ -6667,11 +6670,12 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
0,
)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
}
@ -6864,12 +6868,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
}
code = code.nextField
} else {
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
b = append(b, code.key...)
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(b, code.key...)
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
}
@ -6909,12 +6914,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
}
code = code.nextField
} else {
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
b = append(b, code.key...)
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(b, code.key...)
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
}
@ -7131,9 +7137,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
code = code.nextField
} else {
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
if err := compact(&buf, bb, false); err != nil {
return nil, err
}
b = append(b, code.key...)
b = encodeNoEscapedString(b, buf.String())
b = encodeComma(b)
code = code.next
@ -7174,7 +7181,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
code = code.nextField
} else {
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
if err := compact(&buf, bb, false); err != nil {
return nil, err
}
b = append(b, code.key...)
@ -7818,11 +7825,12 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
case opStructFieldStringTagMarshalJSON:
@ -7834,7 +7842,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
if err := compact(&buf, bb, false); err != nil {
return nil, err
}
b = append(b, code.key...)
@ -7853,12 +7861,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
b = append(b, code.key...)
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(b, code.key...)
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
}
code = code.next
@ -9258,11 +9267,12 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = appendStructEnd(b)
code = code.next
case opStructEndOmitEmptyMarshalJSON:
@ -9274,12 +9284,13 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
b = append(b, code.key...)
buf := bytes.NewBuffer(b)
//TODO: we should validate buffer with `compact`
if err := compact(buf, bb, false); err != nil {
return nil, err
}
b = append(b, code.key...)
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = appendStructEnd(b)
} else {
last := len(b) - 1
@ -9300,7 +9311,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) (
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
if err := compact(&buf, bb, false); err != nil {
return nil, err
}
b = append(b, code.key...)

View File

@ -231,11 +231,12 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
0,
)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(append(b, buf.Bytes()...), ',')
b = buf.Bytes()
b = encodeComma(b)
code = code.next
case opMarshalText:
ptr := load(ctxptr, code.idx)
@ -6599,11 +6600,11 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
0,
)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
}
@ -6634,11 +6635,11 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
0,
)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
}
@ -6831,12 +6832,12 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
}
code = code.nextField
} else {
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
b = append(b, code.escapedKey...)
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(b, code.escapedKey...)
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
}
@ -6876,12 +6877,12 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
}
code = code.nextField
} else {
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
b = append(b, code.escapedKey...)
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(b, code.escapedKey...)
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
}
@ -7804,11 +7805,11 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
code = code.next
case opStructFieldOmitEmptyMarshalJSON:
@ -7823,12 +7824,12 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
b = append(b, code.escapedKey...)
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(b, code.escapedKey...)
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = encodeComma(b)
}
code = code.next
@ -9244,11 +9245,11 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = appendStructEnd(b)
code = code.next
case opStructEndOmitEmptyMarshalJSON:
@ -9260,12 +9261,12 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcod
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
b = append(b, code.escapedKey...)
buf := bytes.NewBuffer(b)
if err := compact(buf, bb, true); err != nil {
return nil, err
}
b = append(b, code.escapedKey...)
b = append(b, buf.Bytes()...)
b = buf.Bytes()
b = appendStructEnd(b)
} else {
last := len(b) - 1

View File

@ -8,6 +8,7 @@ import (
"reflect"
"runtime"
"sort"
"strings"
"unsafe"
)
@ -232,16 +233,20 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
0,
)
}
var buf bytes.Buffer
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, true); err != nil {
return nil, err
}
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&buf,
bb,
string(e.prefix)+string(bytes.Repeat(e.indentStr, e.baseIndent+code.indent)),
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = append(b, indentBuf.Bytes()...)
b = encodeIndentComma(b)
code = code.next
case opMarshalText:
@ -7619,11 +7624,20 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, true); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = append(b, indentBuf.Bytes()...)
b = encodeIndentComma(b)
code = code.next
case opStructFieldStringTagMarshalJSON:
@ -7637,11 +7651,21 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, true); err != nil {
return nil, err
}
b = encodeEscapedString(b, buf.String())
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = append(b, indentBuf.Bytes()...)
b = encodeEscapedString(b, indentBuf.String())
b = encodeIndentComma(b)
code = code.next
case opStructFieldStringTagMarshalText:
@ -9189,11 +9213,20 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, true); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = append(b, indentBuf.Bytes()...)
b = e.appendStructEndIndent(b, code.indent-1)
code = code.next
case opStructEndStringTagMarshalJSON:
@ -9207,11 +9240,20 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, true); err != nil {
return nil, err
}
b = encodeEscapedString(b, buf.String())
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = encodeEscapedString(b, indentBuf.String())
b = e.appendStructEndIndent(b, code.indent-1)
code = code.next
case opStructEndStringTagMarshalText:

View File

@ -8,6 +8,7 @@ import (
"reflect"
"runtime"
"sort"
"strings"
"unsafe"
)
@ -232,16 +233,20 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
0,
)
}
var buf bytes.Buffer
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, false); err != nil {
return nil, err
}
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&buf,
bb,
string(e.prefix)+string(bytes.Repeat(e.indentStr, e.baseIndent+code.indent)),
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
b = append(b, indentBuf.Bytes()...)
b = encodeIndentComma(b)
code = code.next
case opMarshalText:
@ -7619,11 +7624,20 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, false); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = append(b, indentBuf.Bytes()...)
b = encodeIndentComma(b)
code = code.next
case opStructFieldStringTagMarshalJSON:
@ -7637,11 +7651,20 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, false); err != nil {
return nil, err
}
b = encodeNoEscapedString(b, buf.String())
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = encodeNoEscapedString(b, indentBuf.String())
b = encodeIndentComma(b)
code = code.next
case opStructFieldStringTagMarshalText:
@ -9189,11 +9212,20 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, false); err != nil {
return nil, err
}
b = append(b, buf.Bytes()...)
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = append(b, indentBuf.Bytes()...)
b = e.appendStructEndIndent(b, code.indent-1)
code = code.next
case opStructEndStringTagMarshalJSON:
@ -9207,11 +9239,20 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode
if err != nil {
return nil, errMarshaler(code, err)
}
var buf bytes.Buffer
if err := compact(&buf, bb, e.enabledHTMLEscape); err != nil {
var compactBuf bytes.Buffer
if err := compact(&compactBuf, bb, false); err != nil {
return nil, err
}
b = encodeNoEscapedString(b, buf.String())
var indentBuf bytes.Buffer
if err := encodeWithIndent(
&indentBuf,
compactBuf.Bytes(),
string(e.prefix)+strings.Repeat(string(e.indentStr), e.baseIndent+code.indent),
string(e.indentStr),
); err != nil {
return nil, err
}
b = encodeNoEscapedString(b, indentBuf.String())
b = e.appendStructEndIndent(b, code.indent-1)
code = code.next
case opStructEndStringTagMarshalText:

View File

@ -370,7 +370,7 @@ func (m *RawMessage) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
*m = append((*m)[0:0], data...)
*m = data
return nil
}