go-json/decode_slice.go

235 lines
5.1 KiB
Go
Raw 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
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
}
2020-04-25 13:55:05 +03:00
func newSliceDecoder(dec decoder, elemType *rtype, size uintptr) *sliceDecoder {
return &sliceDecoder{
valueDecoder: dec,
elemType: elemType,
size: size,
2020-05-07 15:30:36 +03:00
arrayPool: sync.Pool{
New: func() interface{} {
cap := 2
return &sliceHeader{
data: newArray(elemType, cap),
len: 0,
cap: cap,
2020-05-07 15:30:36 +03:00
}
},
},
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() == ']' {
**(**sliceHeader)(unsafe.Pointer(&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()
2020-07-31 12:52:22 +03:00
cap := slice.cap
data := slice.data
2020-07-31 11:10:03 +03:00
for {
2020-07-30 16:41:53 +03:00
if cap <= idx {
src := sliceHeader{data: data, len: idx, cap: cap}
2020-07-30 16:41:53 +03:00
cap *= 2
2020-07-31 12:52:22 +03:00
data = newArray(d.elemType, cap)
dst := sliceHeader{data: data, len: idx, cap: cap}
2020-07-30 16:41:53 +03:00
copySlice(d.elemType, dst, src)
}
2020-11-19 06:47:42 +03:00
addr := uintptr(data) + uintptr(idx)*d.size
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(addr)); 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 ']':
2020-07-31 12:52:22 +03:00
slice.cap = cap
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
})
**(**sliceHeader)(unsafe.Pointer(&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
}
2020-07-31 12:52:22 +03:00
slice.cap = cap
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:
2020-07-31 12:52:22 +03:00
slice.cap = cap
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()
cap := slice.cap
data := slice.data
2020-04-25 13:55:05 +03:00
for {
if cap <= idx {
src := sliceHeader{data: data, len: idx, cap: cap}
2020-04-25 13:55:05 +03:00
cap *= 2
data = newArray(d.elemType, cap)
dst := sliceHeader{data: data, len: idx, cap: cap}
2020-04-25 13:55:05 +03:00
copySlice(d.elemType, dst, src)
}
2020-11-19 06:47:42 +03:00
addr := uintptr(data) + uintptr(idx)*d.size
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(addr))
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 ']':
slice.cap = cap
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:
slice.cap = cap
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
}