-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
decode_slice.go
259 lines (247 loc) · 5.81 KB
/
decode_slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package json
import (
"reflect"
"sync"
"unsafe"
)
type sliceDecoder struct {
elemType *rtype
valueDecoder decoder
size uintptr
arrayPool sync.Pool
structName string
fieldName string
}
// 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
}
const (
defaultSliceCapacity = 2
)
func newSliceDecoder(dec decoder, elemType *rtype, size uintptr, structName, fieldName string) *sliceDecoder {
return &sliceDecoder{
valueDecoder: dec,
elemType: elemType,
size: size,
arrayPool: sync.Pool{
New: func() interface{} {
return &sliceHeader{
data: newArray(elemType, defaultSliceCapacity),
len: 0,
cap: defaultSliceCapacity,
}
},
},
structName: structName,
fieldName: fieldName,
}
}
func (d *sliceDecoder) newSlice() *sliceHeader {
slice := d.arrayPool.Get().(*sliceHeader)
slice.len = 0
return slice
}
func (d *sliceDecoder) releaseSlice(p *sliceHeader) {
d.arrayPool.Put(p)
}
//go:linkname copySlice reflect.typedslicecopy
func copySlice(elemType *rtype, dst, src sliceHeader) int
//go:linkname newArray reflect.unsafe_NewArray
func newArray(*rtype, int) unsafe.Pointer
func (d *sliceDecoder) errNumber(offset int64) *UnmarshalTypeError {
return &UnmarshalTypeError{
Value: "number",
Type: reflect.SliceOf(rtype2type(d.elemType)),
Struct: d.structName,
Field: d.fieldName,
Offset: offset,
}
}
func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
for {
switch s.char() {
case ' ', '\n', '\t', '\r':
s.cursor++
continue
case 'n':
if err := nullBytes(s); err != nil {
return err
}
return nil
case '[':
s.cursor++
s.skipWhiteSpace()
if s.char() == ']' {
*(*sliceHeader)(p) = sliceHeader{
data: newArray(d.elemType, 0),
len: 0,
cap: 0,
}
s.cursor++
return nil
}
idx := 0
slice := d.newSlice()
capacity := slice.cap
data := slice.data
for {
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}
copySlice(d.elemType, dst, src)
}
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size)); err != nil {
return err
}
s.skipWhiteSpace()
RETRY:
switch s.char() {
case ']':
slice.cap = capacity
slice.len = idx + 1
slice.data = data
dstCap := idx + 1
dst := sliceHeader{
data: newArray(d.elemType, dstCap),
len: idx + 1,
cap: dstCap,
}
copySlice(d.elemType, dst, sliceHeader{
data: slice.data,
len: slice.len,
cap: slice.cap,
})
*(*sliceHeader)(p) = dst
d.releaseSlice(slice)
s.cursor++
return nil
case ',':
idx++
case nul:
if s.read() {
goto RETRY
}
slice.cap = capacity
slice.data = data
d.releaseSlice(slice)
goto ERROR
default:
slice.cap = capacity
slice.data = data
d.releaseSlice(slice)
goto ERROR
}
s.cursor++
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return d.errNumber(s.totalOffset())
case nul:
if s.read() {
continue
}
goto ERROR
default:
goto ERROR
}
}
ERROR:
return errUnexpectedEndOfJSON("slice", s.totalOffset())
}
func (d *sliceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
buflen := int64(len(buf))
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
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
case '[':
cursor++
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == ']' {
**(**sliceHeader)(unsafe.Pointer(&p)) = sliceHeader{
data: newArray(d.elemType, 0),
len: 0,
cap: 0,
}
cursor++
return cursor, nil
}
idx := 0
slice := d.newSlice()
capacity := slice.cap
data := slice.data
for {
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}
copySlice(d.elemType, dst, src)
}
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size))
if err != nil {
return 0, err
}
cursor = c
cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] {
case ']':
slice.cap = capacity
slice.len = idx + 1
slice.data = data
dstCap := idx + 1
dst := sliceHeader{
data: newArray(d.elemType, dstCap),
len: idx + 1,
cap: dstCap,
}
copySlice(d.elemType, dst, sliceHeader{
data: slice.data,
len: slice.len,
cap: slice.cap,
})
**(**sliceHeader)(unsafe.Pointer(&p)) = dst
d.releaseSlice(slice)
cursor++
return cursor, nil
case ',':
idx++
default:
slice.cap = capacity
slice.data = data
d.releaseSlice(slice)
return 0, errInvalidCharacter(buf[cursor], "slice", cursor)
}
cursor++
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return 0, d.errNumber(cursor)
default:
goto ERROR
}
}
ERROR:
return 0, errUnexpectedEndOfJSON("slice", cursor)
}