go-json/internal/decoder/slice.go

302 lines
7.2 KiB
Go
Raw Permalink Normal View History

2021-06-03 12:49:01 +03:00
package decoder
2020-04-25 13:55:05 +03:00
import (
2021-02-09 15:37:18 +03:00
"reflect"
2020-05-07 15:30:36 +03:00
"sync"
2020-04-25 13:55:05 +03:00
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
2020-04-25 13:55:05 +03:00
)
2021-08-13 09:49:53 +03:00
var (
sliceType = runtime.Type2RType(
reflect.TypeOf((*sliceHeader)(nil)).Elem(),
)
nilSlice = unsafe.Pointer(&sliceHeader{})
)
2020-04-25 13:55:05 +03:00
type sliceDecoder struct {
2021-06-03 12:49:01 +03:00
elemType *runtime.Type
2021-05-07 15:31:35 +03:00
isElemPointerType bool
2021-06-03 13:10:17 +03:00
valueDecoder Decoder
2021-05-07 15:31:35 +03:00
size uintptr
arrayPool sync.Pool
structName string
fieldName string
2020-04-25 13:55:05 +03:00
}
// If use reflect.SliceHeader, data type is uintptr.
// In this case, Go compiler cannot trace reference created by newArray().
// So, define using unsafe.Pointer as data type
type sliceHeader struct {
data unsafe.Pointer
len int
cap int
}
2021-02-03 17:50:02 +03:00
const (
defaultSliceCapacity = 2
)
2021-06-03 13:10:17 +03:00
func newSliceDecoder(dec Decoder, elemType *runtime.Type, size uintptr, structName, fieldName string) *sliceDecoder {
2020-04-25 13:55:05 +03:00
return &sliceDecoder{
2021-05-07 15:31:35 +03:00
valueDecoder: dec,
elemType: elemType,
isElemPointerType: elemType.Kind() == reflect.Ptr || elemType.Kind() == reflect.Map,
size: size,
2020-05-07 15:30:36 +03:00
arrayPool: sync.Pool{
New: func() interface{} {
return &sliceHeader{
2021-02-03 17:50:02 +03:00
data: newArray(elemType, defaultSliceCapacity),
len: 0,
2021-02-03 17:50:02 +03:00
cap: defaultSliceCapacity,
2020-05-07 15:30:36 +03:00
}
},
},
structName: structName,
fieldName: fieldName,
2020-04-25 13:55:05 +03:00
}
}
func (d *sliceDecoder) newSlice(src *sliceHeader) *sliceHeader {
slice := d.arrayPool.Get().(*sliceHeader)
if src.len > 0 {
// copy original elem
if slice.cap < src.cap {
data := newArray(d.elemType, src.cap)
slice = &sliceHeader{data: data, len: src.len, cap: src.cap}
} else {
slice.len = src.len
}
copySlice(d.elemType, *slice, *src)
} else {
slice.len = 0
}
2020-05-07 15:30:36 +03:00
return slice
}
func (d *sliceDecoder) releaseSlice(p *sliceHeader) {
2020-05-07 15:30:36 +03:00
d.arrayPool.Put(p)
}
2020-04-25 13:55:05 +03:00
//go:linkname copySlice reflect.typedslicecopy
2021-06-03 12:49:01 +03:00
func copySlice(elemType *runtime.Type, dst, src sliceHeader) int
2020-04-25 13:55:05 +03:00
//go:linkname newArray reflect.unsafe_NewArray
2021-06-03 12:49:01 +03:00
func newArray(*runtime.Type, int) unsafe.Pointer
2020-04-25 13:55:05 +03:00
//go:linkname typedmemmove reflect.typedmemmove
2021-06-03 12:49:01 +03:00
func typedmemmove(t *runtime.Type, dst, src unsafe.Pointer)
2021-06-03 12:49:01 +03:00
func (d *sliceDecoder) errNumber(offset int64) *errors.UnmarshalTypeError {
return &errors.UnmarshalTypeError{
2021-02-09 15:37:18 +03:00
Value: "number",
2021-06-03 12:49:01 +03:00
Type: reflect.SliceOf(runtime.RType2Type(d.elemType)),
2021-02-09 15:37:18 +03:00
Struct: d.structName,
Field: d.fieldName,
Offset: offset,
}
}
2021-06-03 12:49:01 +03:00
func (d *sliceDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
2021-06-03 12:49:01 +03:00
return errors.ErrExceededMaxDepth(s.char(), s.cursor)
}
2020-07-30 16:41:53 +03:00
for {
switch s.char() {
case ' ', '\n', '\t', '\r':
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
continue
2020-08-08 07:20:42 +03:00
case 'n':
if err := nullBytes(s); err != nil {
return err
}
2021-08-13 09:49:53 +03:00
typedmemmove(sliceType, p, nilSlice)
2020-08-08 07:20:42 +03:00
return nil
2020-07-30 16:41:53 +03:00
case '[':
2020-08-12 10:54:15 +03:00
s.cursor++
2021-06-04 08:59:43 +03:00
if s.skipWhiteSpace() == ']' {
dst := (*sliceHeader)(p)
if dst.data == nil {
dst.data = newArray(d.elemType, 0)
} else {
dst.len = 0
2020-08-12 10:54:15 +03:00
}
s.cursor++
return nil
}
2020-07-30 16:41:53 +03:00
idx := 0
slice := d.newSlice((*sliceHeader)(p))
srcLen := slice.len
2021-02-03 17:50:02 +03:00
capacity := slice.cap
2020-07-31 12:52:22 +03:00
data := slice.data
2020-07-31 11:10:03 +03:00
for {
2021-02-03 17:50:02 +03:00
if capacity <= idx {
src := sliceHeader{data: data, len: idx, cap: capacity}
capacity *= 2
data = newArray(d.elemType, capacity)
dst := sliceHeader{data: data, len: idx, cap: capacity}
2020-07-30 16:41:53 +03:00
copySlice(d.elemType, dst, src)
}
2021-03-28 20:28:04 +03:00
ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size)
// if srcLen is greater than idx, keep the original reference
if srcLen <= idx {
if d.isElemPointerType {
**(**unsafe.Pointer)(unsafe.Pointer(&ep)) = nil // initialize elem pointer
} else {
// assign new element to the slice
typedmemmove(d.elemType, ep, unsafe_New(d.elemType))
}
2021-04-30 17:49:04 +03:00
}
2021-06-03 12:49:01 +03:00
if err := d.valueDecoder.DecodeStream(s, depth, ep); err != nil {
2021-04-30 17:49:04 +03:00
return err
2020-07-30 16:41:53 +03:00
}
s.skipWhiteSpace()
2020-07-31 11:10:03 +03:00
RETRY:
2020-07-30 16:41:53 +03:00
switch s.char() {
case ']':
2021-02-03 17:50:02 +03:00
slice.cap = capacity
2020-07-31 12:52:22 +03:00
slice.len = idx + 1
slice.data = data
2021-05-03 10:55:17 +03:00
dst := (*sliceHeader)(p)
dst.len = idx + 1
2021-04-30 20:07:44 +03:00
if dst.len > dst.cap {
dst.data = newArray(d.elemType, dst.len)
dst.cap = dst.len
2020-07-30 16:41:53 +03:00
}
2021-05-03 10:55:17 +03:00
copySlice(d.elemType, *dst, *slice)
2020-07-30 16:41:53 +03:00
d.releaseSlice(slice)
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
case ',':
idx++
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
goto RETRY
}
2021-02-03 17:50:02 +03:00
slice.cap = capacity
2020-07-31 12:52:22 +03:00
slice.data = data
2020-07-31 11:10:03 +03:00
d.releaseSlice(slice)
goto ERROR
2020-07-30 16:41:53 +03:00
default:
2021-02-03 17:50:02 +03:00
slice.cap = capacity
2020-07-31 12:52:22 +03:00
slice.data = data
2020-07-30 16:41:53 +03:00
d.releaseSlice(slice)
2020-07-31 11:10:03 +03:00
goto ERROR
2020-07-30 16:41:53 +03:00
}
2020-08-12 10:54:15 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
}
2021-02-09 15:37:18 +03:00
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
2021-02-09 16:20:23 +03:00
return d.errNumber(s.totalOffset())
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
continue
}
goto ERROR
2021-02-09 15:37:18 +03:00
default:
goto ERROR
2020-07-30 16:41:53 +03:00
}
}
2020-07-31 11:10:03 +03:00
ERROR:
2021-06-03 12:49:01 +03:00
return errors.ErrUnexpectedEndOfJSON("slice", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
2021-06-04 19:08:27 +03:00
func (d *sliceDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
buf := ctx.Buf
depth++
if depth > maxDecodeNestingDepth {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor)
}
2021-05-08 21:05:36 +03:00
for {
2020-04-25 13:55:05 +03:00
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
2021-05-08 21:05:36 +03:00
cursor++
2020-04-25 13:55:05 +03:00
continue
2020-08-08 07:20:42 +03:00
case 'n':
2021-05-08 21:05:36 +03:00
if err := validateNull(buf, cursor); err != nil {
return 0, err
2020-08-08 07:20:42 +03:00
}
cursor += 4
2021-08-13 09:49:53 +03:00
typedmemmove(sliceType, p, nilSlice)
2020-08-08 07:20:42 +03:00
return cursor, nil
2020-04-25 13:55:05 +03:00
case '[':
2020-08-12 10:54:15 +03:00
cursor++
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == ']' {
dst := (*sliceHeader)(p)
if dst.data == nil {
dst.data = newArray(d.elemType, 0)
} else {
dst.len = 0
2020-08-12 10:54:15 +03:00
}
cursor++
return cursor, nil
}
2020-04-25 13:55:05 +03:00
idx := 0
slice := d.newSlice((*sliceHeader)(p))
srcLen := slice.len
2021-02-03 17:50:02 +03:00
capacity := slice.cap
data := slice.data
2020-04-25 13:55:05 +03:00
for {
2021-02-03 17:50:02 +03:00
if capacity <= idx {
src := sliceHeader{data: data, len: idx, cap: capacity}
capacity *= 2
data = newArray(d.elemType, capacity)
dst := sliceHeader{data: data, len: idx, cap: capacity}
2020-04-25 13:55:05 +03:00
copySlice(d.elemType, dst, src)
}
2021-03-28 20:28:04 +03:00
ep := unsafe.Pointer(uintptr(data) + uintptr(idx)*d.size)
// if srcLen is greater than idx, keep the original reference
if srcLen <= idx {
if d.isElemPointerType {
**(**unsafe.Pointer)(unsafe.Pointer(&ep)) = nil // initialize elem pointer
} else {
// assign new element to the slice
typedmemmove(d.elemType, ep, unsafe_New(d.elemType))
}
2021-04-30 17:49:04 +03:00
}
2021-06-04 19:08:27 +03:00
c, err := d.valueDecoder.Decode(ctx, cursor, depth, ep)
2021-04-30 17:49:04 +03:00
if err != nil {
return 0, err
2021-03-28 20:44:49 +03:00
}
2021-04-30 17:49:04 +03:00
cursor = c
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, cursor)
2020-04-25 13:55:05 +03:00
switch buf[cursor] {
case ']':
2021-02-03 17:50:02 +03:00
slice.cap = capacity
slice.len = idx + 1
slice.data = data
2021-05-03 10:55:17 +03:00
dst := (*sliceHeader)(p)
dst.len = idx + 1
2021-04-30 20:07:44 +03:00
if dst.len > dst.cap {
dst.data = newArray(d.elemType, dst.len)
dst.cap = dst.len
2020-04-25 13:55:05 +03:00
}
2021-05-03 10:55:17 +03:00
copySlice(d.elemType, *dst, *slice)
2020-05-07 15:30:36 +03:00
d.releaseSlice(slice)
2020-05-06 20:37:29 +03:00
cursor++
return cursor, nil
2020-04-25 13:55:05 +03:00
case ',':
idx++
default:
2021-02-03 17:50:02 +03:00
slice.cap = capacity
slice.data = data
2020-05-07 15:30:36 +03:00
d.releaseSlice(slice)
2021-06-03 12:49:01 +03:00
return 0, errors.ErrInvalidCharacter(buf[cursor], "slice", cursor)
2020-04-25 13:55:05 +03:00
}
2020-08-12 10:54:15 +03:00
cursor++
2020-04-25 13:55:05 +03:00
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
2021-02-09 16:20:23 +03:00
return 0, d.errNumber(cursor)
default:
2021-06-03 12:49:01 +03:00
return 0, errors.ErrUnexpectedEndOfJSON("slice", cursor)
2020-04-25 13:55:05 +03:00
}
}
}