go-json/decode_slice.go

240 lines
5.3 KiB
Go
Raw Permalink Normal View History

2020-04-25 13:55:05 +03:00
package json
import (
2020-05-07 15:30:36 +03:00
"sync"
2020-04-25 13:55:05 +03:00
"unsafe"
)
type sliceDecoder struct {
elemType *rtype
valueDecoder decoder
size uintptr
2020-05-07 15:30:36 +03:00
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{
valueDecoder: dec,
elemType: elemType,
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() *sliceHeader {
slice := d.arrayPool.Get().(*sliceHeader)
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
2020-11-19 06:47:42 +03:00
func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
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
}
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() == ']' {
2020-12-05 16:27:33 +03:00
*(*sliceHeader)(p) = sliceHeader{
data: newArray(d.elemType, 0),
len: 0,
cap: 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()
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)
}
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size)); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
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
2020-07-30 16:41:53 +03:00
dstCap := idx + 1
dst := sliceHeader{
data: newArray(d.elemType, dstCap),
len: idx + 1,
cap: dstCap,
2020-07-30 16:41:53 +03:00
}
copySlice(d.elemType, dst, sliceHeader{
data: slice.data,
len: slice.len,
cap: slice.cap,
2020-07-31 12:52:22 +03:00
})
2020-12-05 16:27:33 +03:00
*(*sliceHeader)(p) = dst
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
}
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
continue
}
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())
}
2020-11-19 06:47:42 +03:00
func (d *sliceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
2020-05-23 06:51:09 +03:00
buflen := int64(len(buf))
2020-04-25 13:55:05 +03:00
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
2020-08-08 07:20:42 +03:00
case 'n':
buflen := int64(len(buf))
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
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] == ']' {
**(**sliceHeader)(unsafe.Pointer(&p)) = sliceHeader{
data: newArray(d.elemType, 0),
len: 0,
cap: 0,
2020-08-12 10:54:15 +03:00
}
cursor++
return cursor, nil
}
2020-04-25 13:55:05 +03:00
idx := 0
2020-05-07 15:30:36 +03:00
slice := d.newSlice()
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)
}
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size))
2020-05-06 20:37:29 +03:00
if err != nil {
return 0, err
2020-04-25 13:55:05 +03:00
}
2020-05-06 20:37:29 +03:00
cursor = c
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
2020-05-07 15:30:36 +03:00
dstCap := idx + 1
dst := sliceHeader{
data: newArray(d.elemType, dstCap),
len: idx + 1,
cap: dstCap,
2020-04-25 13:55:05 +03:00
}
copySlice(d.elemType, dst, sliceHeader{
data: slice.data,
len: slice.len,
cap: slice.cap,
})
**(**sliceHeader)(unsafe.Pointer(&p)) = dst
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
}
}
}
2020-05-23 06:51:09 +03:00
return 0, errUnexpectedEndOfJSON("slice", cursor)
2020-04-25 13:55:05 +03:00
}