2020-04-25 13:55:05 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 &reflect.SliceHeader{
|
|
|
|
Data: uintptr(newArray(elemType, cap)),
|
|
|
|
Len: 0,
|
|
|
|
Cap: cap,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 15:30:36 +03:00
|
|
|
func (d *sliceDecoder) newSlice() *reflect.SliceHeader {
|
|
|
|
slice := d.arrayPool.Get().(*reflect.SliceHeader)
|
|
|
|
slice.Len = 0
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sliceDecoder) releaseSlice(p *reflect.SliceHeader) {
|
|
|
|
d.arrayPool.Put(p)
|
|
|
|
}
|
|
|
|
|
2020-04-25 13:55:05 +03:00
|
|
|
//go:linkname copySlice reflect.typedslicecopy
|
|
|
|
func copySlice(elemType *rtype, dst, src reflect.SliceHeader) int
|
|
|
|
|
|
|
|
//go:linkname newArray reflect.unsafe_NewArray
|
|
|
|
func newArray(*rtype, int) unsafe.Pointer
|
|
|
|
|
2020-07-30 16:41:53 +03:00
|
|
|
func (d *sliceDecoder) decodeStream(s *stream, p uintptr) error {
|
|
|
|
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
|
|
|
|
case '[':
|
|
|
|
idx := 0
|
|
|
|
slice := d.newSlice()
|
|
|
|
cap := slice.Cap
|
|
|
|
data := slice.Data
|
2020-07-31 11:10:03 +03:00
|
|
|
for {
|
|
|
|
s.cursor++
|
2020-07-30 16:41:53 +03:00
|
|
|
if cap <= idx {
|
|
|
|
src := reflect.SliceHeader{Data: data, Len: idx, Cap: cap}
|
|
|
|
cap *= 2
|
|
|
|
data = uintptr(newArray(d.elemType, cap))
|
|
|
|
dst := reflect.SliceHeader{Data: data, Len: idx, Cap: cap}
|
|
|
|
copySlice(d.elemType, dst, src)
|
|
|
|
}
|
|
|
|
if err := d.valueDecoder.decodeStream(s, data+uintptr(idx)*d.size); err != nil {
|
|
|
|
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 ']':
|
|
|
|
slice.Cap = cap
|
|
|
|
slice.Len = idx + 1
|
|
|
|
slice.Data = data
|
|
|
|
dstCap := idx + 1
|
|
|
|
dst := reflect.SliceHeader{
|
|
|
|
Data: uintptr(newArray(d.elemType, dstCap)),
|
|
|
|
Len: idx + 1,
|
|
|
|
Cap: dstCap,
|
|
|
|
}
|
|
|
|
copySlice(d.elemType, dst, *slice)
|
|
|
|
*(*reflect.SliceHeader)(unsafe.Pointer(p)) = dst
|
|
|
|
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++
|
|
|
|
continue
|
2020-07-31 11:10:03 +03:00
|
|
|
case nul:
|
|
|
|
if s.read() {
|
|
|
|
goto RETRY
|
|
|
|
}
|
|
|
|
slice.Cap = cap
|
|
|
|
slice.Data = data
|
|
|
|
d.releaseSlice(slice)
|
|
|
|
goto ERROR
|
2020-07-30 16:41:53 +03:00
|
|
|
default:
|
|
|
|
slice.Cap = cap
|
|
|
|
slice.Data = data
|
|
|
|
d.releaseSlice(slice)
|
2020-07-31 11:10:03 +03:00
|
|
|
goto ERROR
|
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-05-23 06:51:09 +03:00
|
|
|
func (d *sliceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) {
|
|
|
|
buflen := int64(len(buf))
|
2020-04-25 13:55:05 +03:00
|
|
|
for ; cursor < buflen; cursor++ {
|
|
|
|
switch buf[cursor] {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
continue
|
|
|
|
case '[':
|
|
|
|
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 {
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor++
|
2020-04-25 13:55:05 +03:00
|
|
|
if cap <= idx {
|
|
|
|
src := reflect.SliceHeader{Data: data, Len: idx, Cap: cap}
|
|
|
|
cap *= 2
|
|
|
|
data = uintptr(newArray(d.elemType, cap))
|
|
|
|
dst := reflect.SliceHeader{Data: data, Len: idx, Cap: cap}
|
|
|
|
copySlice(d.elemType, dst, src)
|
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
c, err := d.valueDecoder.decode(buf, cursor, data+uintptr(idx)*d.size)
|
|
|
|
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 ']':
|
2020-05-07 15:30:36 +03:00
|
|
|
slice.Cap = cap
|
|
|
|
slice.Len = idx + 1
|
|
|
|
slice.Data = data
|
|
|
|
dstCap := idx + 1
|
|
|
|
dst := reflect.SliceHeader{
|
|
|
|
Data: uintptr(newArray(d.elemType, dstCap)),
|
2020-04-25 13:55:05 +03:00
|
|
|
Len: idx + 1,
|
2020-05-07 15:30:36 +03:00
|
|
|
Cap: dstCap,
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
2020-05-07 15:30:36 +03:00
|
|
|
copySlice(d.elemType, dst, *slice)
|
|
|
|
*(*reflect.SliceHeader)(unsafe.Pointer(p)) = dst
|
|
|
|
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++
|
|
|
|
continue
|
|
|
|
default:
|
2020-05-07 15:30:36 +03:00
|
|
|
slice.Cap = cap
|
|
|
|
slice.Data = data
|
|
|
|
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-05-23 06:51:09 +03:00
|
|
|
return 0, errUnexpectedEndOfJSON("slice", cursor)
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|