go-json/decode_slice.go

119 lines
2.7 KiB
Go
Raw Normal View History

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
}
// 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 reflect.SliceHeader) int
//go:linkname newArray reflect.unsafe_NewArray
func newArray(*rtype, int) unsafe.Pointer
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: uintptr(data), Len: idx, Cap: cap}
2020-04-25 13:55:05 +03:00
cap *= 2
data = newArray(d.elemType, cap)
dst := reflect.SliceHeader{Data: uintptr(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, 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 ']':
slice.cap = cap
slice.len = idx + 1
slice.data = data
2020-05-07 15:30:36 +03:00
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
}
copySlice(d.elemType, dst, reflect.SliceHeader{
Data: uintptr(slice.data),
Len: slice.len,
Cap: slice.cap,
})
2020-05-07 15:30:36 +03:00
*(*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:
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-05-23 06:51:09 +03:00
return 0, errUnexpectedEndOfJSON("slice", cursor)
2020-04-25 13:55:05 +03:00
}