go-json/decode_slice.go

237 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
}
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{} {
cap := 2
return &sliceHeader{
data: newArray(elemType, cap),
len: 0,
cap: cap,
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()
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)
}
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 ']':
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
})
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
}
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())
}
2021-02-05 18:54:10 +03:00
func (d *sliceDecoder) decode(buf *sliceHeader, cursor int64, p unsafe.Pointer) (int64, error) {
buflen := int64(buf.len)
2020-04-25 13:55:05 +03:00
for ; cursor < buflen; cursor++ {
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
2020-04-25 13:55:05 +03:00
case ' ', '\n', '\t', '\r':
continue
2020-08-08 07:20:42 +03:00
case 'n':
2021-02-05 18:54:10 +03:00
buflen := int64(buf.len)
2020-08-08 07:20:42 +03:00
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+1) != 'u' {
return 0, errInvalidCharacter(char(buf.data, cursor+1), "null", cursor)
2020-08-08 07:20:42 +03:00
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+2) != 'l' {
return 0, errInvalidCharacter(char(buf.data, cursor+2), "null", cursor)
2020-08-08 07:20:42 +03:00
}
2021-02-05 18:54:10 +03:00
if char(buf.data, cursor+3) != 'l' {
return 0, errInvalidCharacter(char(buf.data, cursor+3), "null", cursor)
2020-08-08 07:20:42 +03:00
}
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)
2021-02-05 18:54:10 +03:00
if char(buf.data, 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)
}
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)
2021-02-05 18:54:10 +03:00
switch char(buf.data, cursor) {
2020-04-25 13:55:05 +03:00
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)
2021-02-05 18:54:10 +03:00
return 0, errInvalidCharacter(char(buf.data, 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
}