go-json/decode_slice.go

292 lines
6.8 KiB
Go
Raw Normal View History

2020-04-25 13:55:05 +03:00
package json
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"
)
type sliceDecoder struct {
2021-05-07 15:31:35 +03:00
elemType *rtype
isElemPointerType bool
valueDecoder decoder
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
)
func newSliceDecoder(dec decoder, elemType *rtype, 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
func copySlice(elemType *rtype, dst, src sliceHeader) int
2020-04-25 13:55:05 +03:00
//go:linkname newArray reflect.unsafe_NewArray
func newArray(*rtype, int) unsafe.Pointer
//go:linkname typedmemmove reflect.typedmemmove
func typedmemmove(t *rtype, dst, src unsafe.Pointer)
2021-02-09 16:20:23 +03:00
func (d *sliceDecoder) errNumber(offset int64) *UnmarshalTypeError {
2021-02-09 15:37:18 +03:00
return &UnmarshalTypeError{
Value: "number",
Type: reflect.SliceOf(rtype2type(d.elemType)),
Struct: d.structName,
Field: d.fieldName,
Offset: offset,
}
}
func (d *sliceDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
return 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-02-16 19:51:42 +03:00
*(*unsafe.Pointer)(p) = nil
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++
s.skipWhiteSpace()
if s.char() == ']' {
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-04-30 17:49:04 +03:00
if err := d.valueDecoder.decodeStream(s, depth, ep); err != nil {
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:
2020-07-30 16:41:53 +03:00
return errUnexpectedEndOfJSON("slice", s.totalOffset())
}
func (d *sliceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
depth++
if depth > maxDecodeNestingDepth {
return 0, 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-02-16 19:51:42 +03:00
*(*unsafe.Pointer)(p) = nil
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
}
c, err := d.valueDecoder.decode(buf, cursor, depth, ep)
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)
2020-05-23 06:51:09 +03:00
return 0, 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-05-08 21:05:36 +03:00
return 0, errUnexpectedEndOfJSON("slice", cursor)
2020-04-25 13:55:05 +03:00
}
}
}