Fix allocation strategy for Compact

This commit is contained in:
Masaaki Goshima 2021-04-19 18:58:09 +09:00
parent 4e0c525f9c
commit 7565171124
5 changed files with 41 additions and 37 deletions

View File

@ -7,7 +7,6 @@ import (
"unsafe" "unsafe"
"github.com/goccy/go-json/internal/errors" "github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
) )
var ( var (
@ -29,8 +28,11 @@ func Compact(buf *bytes.Buffer, src []byte, escape bool) error {
if len(src) == 0 { if len(src) == 0 {
return errors.ErrUnexpectedEndOfJSON("", 0) return errors.ErrUnexpectedEndOfJSON("", 0)
} }
dst := make([]byte, 0, len(src)) buf.Grow(len(src))
dst, err := compact(dst, src, escape) dst := buf.Bytes()
newSrc := make([]byte, len(src)+1) // append nul byte to the end
copy(newSrc, src)
dst, err := compact(dst, newSrc, escape)
if err != nil { if err != nil {
return err return err
} }
@ -41,12 +43,10 @@ func Compact(buf *bytes.Buffer, src []byte, escape bool) error {
} }
func compact(dst, src []byte, escape bool) ([]byte, error) { func compact(dst, src []byte, escape bool) ([]byte, error) {
src = append(src, nul)
buf, _, err := compactValue(dst, src, 0, escape) buf, _, err := compactValue(dst, src, 0, escape)
if err != nil { if err != nil {
return nil, err return nil, err
} }
(*runtime.SliceHeader)(unsafe.Pointer(&src)).Len--
return buf, nil return buf, nil
} }

View File

@ -306,6 +306,7 @@ func MapLen(m unsafe.Pointer) int
type RuntimeContext struct { type RuntimeContext struct {
Buf []byte Buf []byte
MarshalBuf []byte
Ptrs []uintptr Ptrs []uintptr
KeepRefs []unsafe.Pointer KeepRefs []unsafe.Pointer
SeenPtr []uintptr SeenPtr []uintptr
@ -413,7 +414,7 @@ func AppendNumber(b []byte, n json.Number) ([]byte, error) {
return b, nil return b, nil
} }
func AppendMarshalJSON(code *Opcode, b []byte, v interface{}, escape bool) ([]byte, error) { func AppendMarshalJSON(ctx *RuntimeContext, code *Opcode, b []byte, v interface{}, escape bool) ([]byte, error) {
rv := reflect.ValueOf(v) // convert by dynamic interface type rv := reflect.ValueOf(v) // convert by dynamic interface type
if code.AddrForMarshaler { if code.AddrForMarshaler {
if rv.CanAddr() { if rv.CanAddr() {
@ -433,10 +434,13 @@ func AppendMarshalJSON(code *Opcode, b []byte, v interface{}, escape bool) ([]by
if err != nil { if err != nil {
return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err} return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err}
} }
compactedBuf, err := compact(b, bb, escape) marshalBuf := ctx.MarshalBuf[:0]
marshalBuf = append(append(marshalBuf, bb...), nul)
compactedBuf, err := compact(b, marshalBuf, escape)
if err != nil { if err != nil {
return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err} return nil, &errors.MarshalerError{Type: reflect.TypeOf(v), Err: err}
} }
ctx.MarshalBuf = marshalBuf
return compactedBuf, nil return compactedBuf, nil
} }

View File

@ -273,7 +273,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if code.IsNilableType && code.Indirect { if code.IsNilableType && code.Indirect {
p = ptrToPtr(p) p = ptrToPtr(p)
} }
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2718,7 +2718,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2762,7 +2762,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2807,7 +2807,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
code = code.NextField code = code.NextField
} else { } else {
b = append(b, code.Key...) b = append(b, code.Key...)
bb, err := appendMarshalJSON(code, b, iface, false) bb, err := appendMarshalJSON(ctx, code, b, iface, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2847,7 +2847,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2887,7 +2887,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
code = code.NextField code = code.NextField
} else { } else {
b = append(b, code.Key...) b = append(b, code.Key...)
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3628,7 +3628,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3652,7 +3652,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
break break
} }
b = append(b, code.Key...) b = append(b, code.Key...)
bb, err := appendMarshalJSON(code, b, iface, false) bb, err := appendMarshalJSON(ctx, code, b, iface, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3665,7 +3665,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3678,7 +3678,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
p = ptrToNPtr(p+code.Offset, code.PtrNum) p = ptrToNPtr(p+code.Offset, code.PtrNum)
if p != 0 { if p != 0 {
b = append(b, code.Key...) b = append(b, code.Key...)
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -286,7 +286,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if code.IsNilableType && code.Indirect { if code.IsNilableType && code.Indirect {
p = ptrToPtr(p) p = ptrToPtr(p)
} }
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2731,7 +2731,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2775,7 +2775,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2820,7 +2820,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
code = code.NextField code = code.NextField
} else { } else {
b = append(b, code.Key...) b = append(b, code.Key...)
bb, err := appendMarshalJSON(code, b, iface, false) bb, err := appendMarshalJSON(ctx, code, b, iface, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2860,7 +2860,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2900,7 +2900,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
code = code.NextField code = code.NextField
} else { } else {
b = append(b, code.Key...) b = append(b, code.Key...)
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3641,7 +3641,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3665,7 +3665,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
break break
} }
b = append(b, code.Key...) b = append(b, code.Key...)
bb, err := appendMarshalJSON(code, b, iface, false) bb, err := appendMarshalJSON(ctx, code, b, iface, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3678,7 +3678,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3691,7 +3691,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
p = ptrToNPtr(p+code.Offset, code.PtrNum) p = ptrToNPtr(p+code.Offset, code.PtrNum)
if p != 0 { if p != 0 {
b = append(b, code.Key...) b = append(b, code.Key...)
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), false)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -273,7 +273,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if code.IsNilableType && code.Indirect { if code.IsNilableType && code.Indirect {
p = ptrToPtr(p) p = ptrToPtr(p)
} }
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2718,7 +2718,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2762,7 +2762,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2807,7 +2807,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
code = code.NextField code = code.NextField
} else { } else {
b = append(b, code.EscapedKey...) b = append(b, code.EscapedKey...)
bb, err := appendMarshalJSON(code, b, iface, true) bb, err := appendMarshalJSON(ctx, code, b, iface, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2847,7 +2847,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2887,7 +2887,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
code = code.NextField code = code.NextField
} else { } else {
b = append(b, code.EscapedKey...) b = append(b, code.EscapedKey...)
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3628,7 +3628,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 && code.Nilcheck { if p == 0 && code.Nilcheck {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3652,7 +3652,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
break break
} }
b = append(b, code.EscapedKey...) b = append(b, code.EscapedKey...)
bb, err := appendMarshalJSON(code, b, iface, true) bb, err := appendMarshalJSON(ctx, code, b, iface, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3665,7 +3665,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
if p == 0 { if p == 0 {
b = appendNull(b) b = appendNull(b)
} else { } else {
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3678,7 +3678,7 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt
p = ptrToNPtr(p+code.Offset, code.PtrNum) p = ptrToNPtr(p+code.Offset, code.PtrNum)
if p != 0 { if p != 0 {
b = append(b, code.EscapedKey...) b = append(b, code.EscapedKey...)
bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }