Debian Codegraph @

codec/helper_unsafe.go

raw symbols SCIP
1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a MIT license found in the LICENSE file.
3
4 //go:build !safe && !codec.safe && !appengine && go1.9
5 // +build !safe,!codec.safe,!appengine,go1.9
6
7 // minimum of go 1.9 is needed, as that is the minimum for all features and linked functions we need
8 // - typedmemclr was introduced in go 1.8
9 // - mapassign_fastXXX was introduced in go 1.9
10 // etc
11
12 package codec
13
14 import (
15 "reflect"
16 _ "runtime" // needed for go linkname(s)
17 "sync/atomic"
18 "time"
19 "unsafe"
20 )
21
22 // This file has unsafe variants of some helper functions.
23 // MARKER: See helper_unsafe.go for the usage documentation.
24
25 // There are a number of helper_*unsafe*.go files.
26 //
27 // - helper_unsafe
28 // unsafe variants of dependent functions
29 // - helper_unsafe_compiler_gc (gc)
30 // unsafe variants of dependent functions which cannot be shared with gollvm or gccgo
31 // - helper_not_unsafe_not_gc (gccgo/gollvm or safe)
32 // safe variants of functions in helper_unsafe_compiler_gc
33 // - helper_not_unsafe (safe)
34 // safe variants of functions in helper_unsafe
35 // - helper_unsafe_compiler_not_gc (gccgo, gollvm)
36 // unsafe variants of functions/variables which non-standard compilers need
37 //
38 // This way, we can judiciously use build tags to include the right set of files
39 // for any compiler, and make it run optimally in unsafe mode.
40 //
41 // As of March 2021, we cannot differentiate whether running with gccgo or gollvm
42 // using a build constraint, as both satisfy 'gccgo' build tag.
43 // Consequently, we must use the lowest common denominator to support both.
44
45 // For reflect.Value code, we decided to do the following:
46 // - if we know the kind, we can elide conditional checks for
47 // - SetXXX (Int, Uint, String, Bool, etc)
48 // - SetLen
49 //
50 // We can also optimize
51 // - IsNil
52
53 // MARKER: Some functions here will not be hit during code coverage runs due to optimizations, e.g.
54 // - rvCopySlice: decode calls it if rvGrowSlice didn't set the new slice into the pointer to the orig slice.
55 // however, helper_unsafe sets it, so there's no need to call rvCopySlice later
56 // - rvSlice: same as above
57 // - rvGetArray4Bytes: only called within kArray for []byte, but that is now handled
58 // within the fast-path directly
59
60 const safeMode = false
61
62 // helperUnsafeDirectAssignMapEntry says that we should not copy the pointer in the map
63 // to another value during mapRange/iteration and mapGet calls, but directly assign it.
64 //
65 // The only callers of mapRange/iteration is encode.
66 // Here, we just walk through the values and encode them
67 //
68 // The only caller of mapGet is decode.
69 // Here, it does a Get if the underlying value is a pointer, and decodes into that.
70 //
71 // For both users, we are very careful NOT to modify or keep the pointers around.
72 // Consequently, it is ok for take advantage of the performance that the map is not modified
73 // during an iteration and we can just "peek" at the internal value" in the map and use it.
74 const helperUnsafeDirectAssignMapEntry = true
75
76 // MARKER: keep in sync with GO_ROOT/src/reflect/value.go
77 const (
78 unsafeFlagStickyRO = 1 << 5
79 unsafeFlagEmbedRO = 1 << 6
80 unsafeFlagIndir = 1 << 7
81 unsafeFlagAddr = 1 << 8
82 unsafeFlagRO = unsafeFlagStickyRO | unsafeFlagEmbedRO
83 // unsafeFlagKindMask = (1 << 5) - 1 // 5 bits for 27 kinds (up to 31)
84 // unsafeTypeKindDirectIface = 1 << 5
85 )
86
87 // transientSizeMax below is used in TransientAddr as the backing storage.
88 //
89 // Must be >= 16 as the maximum size is a complex128 (or string on 64-bit machines).
90 const transientSizeMax = 64
91
92 // should struct/array support internal strings and slices?
93 const transientValueHasStringSlice = false
94
95 type unsafeString struct {
96 Data unsafe.Pointer
97 Len int
98 }
99
100 type unsafeSlice struct {
101 Data unsafe.Pointer
102 Len int
103 Cap int
104 }
105
106 type unsafeIntf struct {
107 typ unsafe.Pointer
108 ptr unsafe.Pointer
109 }
110
111 type unsafeReflectValue struct {
112 unsafeIntf
113 flag uintptr
114 }
115
116 // keep in sync with stdlib runtime/type.go
117 type unsafeRuntimeType struct {
118 size uintptr
119 // ... many other fields here
120 }
121
122 // unsafeZeroAddr and unsafeZeroSlice points to a read-only block of memory
123 // used for setting a zero value for most types or creating a read-only
124 // zero value for a given type.
125 var (
126 unsafeZeroAddr = unsafe.Pointer(&unsafeZeroArr[0])
127 unsafeZeroSlice = unsafeSlice{unsafeZeroAddr, 0, 0}
128 )
129
130 // We use a scratch memory and an unsafeSlice for transient values:
131 //
132 // unsafeSlice is used for standalone strings and slices (outside an array or struct).
133 // scratch memory is used for other kinds, based on contract below:
134 // - numbers, bool are always transient
135 // - structs and arrays are transient iff they have no pointers i.e.
136 // no string, slice, chan, func, interface, map, etc only numbers and bools.
137 // - slices and strings are transient (using the unsafeSlice)
138
139 type unsafePerTypeElem struct {
140 arr [transientSizeMax]byte // for bool, number, struct, array kinds
141 slice unsafeSlice // for string and slice kinds
142 }
143
144 func (x *unsafePerTypeElem) addrFor(k reflect.Kind) unsafe.Pointer {
145 if k == reflect.String || k == reflect.Slice {
146 x.slice = unsafeSlice{} // memclr
147 return unsafe.Pointer(&x.slice)
148 }
149 x.arr = [transientSizeMax]byte{} // memclr
150 return unsafe.Pointer(&x.arr)
151 }
152
153 type perType struct {
154 elems [2]unsafePerTypeElem
155 }
156
157 type decPerType struct {
158 perType
159 }
160
161 type encPerType struct{}
162
163 // TransientAddrK is used for getting a *transient* value to be decoded into,
164 // which will right away be used for something else.
165 //
166 // See notes in helper.go about "Transient values during decoding"
167
168 func (x *perType) TransientAddrK(t reflect.Type, k reflect.Kind) reflect.Value {
169 return rvZeroAddrTransientAnyK(t, k, x.elems[0].addrFor(k))
170 }
171
172 func (x *perType) TransientAddr2K(t reflect.Type, k reflect.Kind) reflect.Value {
173 return rvZeroAddrTransientAnyK(t, k, x.elems[1].addrFor(k))
174 }
175
176 func (encPerType) AddressableRO(v reflect.Value) reflect.Value {
177 return rvAddressableReadonly(v)
178 }
179
180 // stringView returns a view of the []byte as a string.
181 // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
182 // In regular safe mode, it is an allocation and copy.
183 func stringView(v []byte) string {
184 return *(*string)(unsafe.Pointer(&v))
185 }
186
187 // bytesView returns a view of the string as a []byte.
188 // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
189 // In regular safe mode, it is an allocation and copy.
190 func bytesView(v string) (b []byte) {
191 sx := (*unsafeString)(unsafe.Pointer(&v))
192 bx := (*unsafeSlice)(unsafe.Pointer(&b))
193 bx.Data, bx.Len, bx.Cap = sx.Data, sx.Len, sx.Len
194 return
195 }
196
197 func byteSliceSameData(v1 []byte, v2 []byte) bool {
198 return (*unsafeSlice)(unsafe.Pointer(&v1)).Data == (*unsafeSlice)(unsafe.Pointer(&v2)).Data
199 }
200
201 // MARKER: okBytesN functions will copy N bytes into the top slots of the return array.
202 // These functions expect that the bounds are valid, and have been checked before this is called.
203 // copy(...) does a number of checks which are unnecessary in this situation when in bounds.
204
205 func okBytes3(b []byte) (v [4]byte) {
206 *(*[3]byte)(unsafe.Pointer(&v[1])) = *((*[3]byte)(((*unsafeSlice)(unsafe.Pointer(&b))).Data))
207 return
208 }
209
210 func okBytes4(b []byte) [4]byte {
211 return *((*[4]byte)(((*unsafeSlice)(unsafe.Pointer(&b))).Data))
212 }
213
214 func okBytes8(b []byte) [8]byte {
215 return *((*[8]byte)(((*unsafeSlice)(unsafe.Pointer(&b))).Data))
216 }
217
218 // isNil says whether the value v is nil.
219 // This applies to references like map/ptr/unsafepointer/chan/func,
220 // and non-reference values like interface/slice.
221 func isNil(v interface{}) (rv reflect.Value, isnil bool) {
222 var ui = (*unsafeIntf)(unsafe.Pointer(&v))
223 isnil = ui.ptr == nil
224 if !isnil {
225 rv, isnil = unsafeIsNilIntfOrSlice(ui, v)
226 }
227 return
228 }
229
230 func unsafeIsNilIntfOrSlice(ui *unsafeIntf, v interface{}) (rv reflect.Value, isnil bool) {
231 rv = reflect.ValueOf(v) // reflect.ValueOf is currently not inline'able - so call it directly
232 tk := rv.Kind()
233 isnil = (tk == reflect.Interface || tk == reflect.Slice) && *(*unsafe.Pointer)(ui.ptr) == nil
234 return
235 }
236
237 // return the pointer for a reference (map/chan/func/pointer/unsafe.Pointer).
238 // true references (map, func, chan, ptr - NOT slice) may be double-referenced? as flagIndir
239 //
240 // Assumes that v is a reference (map/func/chan/ptr/func)
241 func rvRefPtr(v *unsafeReflectValue) unsafe.Pointer {
242 if v.flag&unsafeFlagIndir != 0 {
243 return *(*unsafe.Pointer)(v.ptr)
244 }
245 return v.ptr
246 }
247
248 func eq4i(i0, i1 interface{}) bool {
249 v0 := (*unsafeIntf)(unsafe.Pointer(&i0))
250 v1 := (*unsafeIntf)(unsafe.Pointer(&i1))
251 return v0.typ == v1.typ && v0.ptr == v1.ptr
252 }
253
254 func rv4iptr(i interface{}) (v reflect.Value) {
255 // Main advantage here is that it is inlined, nothing escapes to heap, i is never nil
256 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
257 uv.unsafeIntf = *(*unsafeIntf)(unsafe.Pointer(&i))
258 uv.flag = uintptr(rkindPtr)
259 return
260 }
261
262 func rv4istr(i interface{}) (v reflect.Value) {
263 // Main advantage here is that it is inlined, nothing escapes to heap, i is never nil
264 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
265 uv.unsafeIntf = *(*unsafeIntf)(unsafe.Pointer(&i))
266 uv.flag = uintptr(rkindString) | unsafeFlagIndir
267 return
268 }
269
270 func rv2i(rv reflect.Value) (i interface{}) {
271 // We tap into implememtation details from
272 // the source go stdlib reflect/value.go, and trims the implementation.
273 //
274 // e.g.
275 // - a map/ptr is a reference, thus flagIndir is not set on it
276 // - an int/slice is not a reference, thus flagIndir is set on it
277
278 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
279 if refBitset.isset(byte(rv.Kind())) && urv.flag&unsafeFlagIndir != 0 {
280 urv.ptr = *(*unsafe.Pointer)(urv.ptr)
281 }
282 return *(*interface{})(unsafe.Pointer(&urv.unsafeIntf))
283 }
284
285 func rvAddr(rv reflect.Value, ptrType reflect.Type) reflect.Value {
286 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
287 urv.flag = (urv.flag & unsafeFlagRO) | uintptr(reflect.Ptr)
288 urv.typ = ((*unsafeIntf)(unsafe.Pointer(&ptrType))).ptr
289 return rv
290 }
291
292 func rvIsNil(rv reflect.Value) bool {
293 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
294 if urv.flag&unsafeFlagIndir != 0 {
295 return *(*unsafe.Pointer)(urv.ptr) == nil
296 }
297 return urv.ptr == nil
298 }
299
300 func rvSetSliceLen(rv reflect.Value, length int) {
301 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
302 (*unsafeString)(urv.ptr).Len = length
303 }
304
305 func rvZeroAddrK(t reflect.Type, k reflect.Kind) (rv reflect.Value) {
306 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
307 urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
308 urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
309 urv.ptr = unsafeNew(urv.typ)
310 return
311 }
312
313 func rvZeroAddrTransientAnyK(t reflect.Type, k reflect.Kind, addr unsafe.Pointer) (rv reflect.Value) {
314 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
315 urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
316 urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
317 urv.ptr = addr
318 return
319 }
320
321 func rvZeroK(t reflect.Type, k reflect.Kind) (rv reflect.Value) {
322 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
323 urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
324 if refBitset.isset(byte(k)) {
325 urv.flag = uintptr(k)
326 } else if rtsize2(urv.typ) <= uintptr(len(unsafeZeroArr)) {
327 urv.flag = uintptr(k) | unsafeFlagIndir
328 urv.ptr = unsafeZeroAddr
329 } else { // meaning struct or array
330 urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
331 urv.ptr = unsafeNew(urv.typ)
332 }
333 return
334 }
335
336 // rvConvert will convert a value to a different type directly,
337 // ensuring that they still point to the same underlying value.
338 func rvConvert(v reflect.Value, t reflect.Type) reflect.Value {
339 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
340 uv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
341 return v
342 }
343
344 // rvAddressableReadonly returns an addressable reflect.Value.
345 //
346 // Use it within encode calls, when you just want to "read" the underlying ptr
347 // without modifying the value.
348 //
349 // Note that it cannot be used for r/w use, as those non-addressable values
350 // may have been stored in read-only memory, and trying to write the pointer
351 // may cause a segfault.
352 func rvAddressableReadonly(v reflect.Value) reflect.Value {
353 // hack to make an addressable value out of a non-addressable one.
354 // Assume folks calling it are passing a value that can be addressable, but isn't.
355 // This assumes that the flagIndir is already set on it.
356 // so we just set the flagAddr bit on the flag (and do not set the flagIndir).
357
358 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
359 uv.flag = uv.flag | unsafeFlagAddr // | unsafeFlagIndir
360
361 return v
362 }
363
364 func rtsize2(rt unsafe.Pointer) uintptr {
365 return ((*unsafeRuntimeType)(rt)).size
366 }
367
368 func rt2id(rt reflect.Type) uintptr {
369 return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).ptr)
370 }
371
372 func i2rtid(i interface{}) uintptr {
373 return uintptr(((*unsafeIntf)(unsafe.Pointer(&i))).typ)
374 }
375
376 // --------------------------
377
378 func unsafeCmpZero(ptr unsafe.Pointer, size int) bool {
379 // verified that size is always within right range, so no chance of OOM
380 var s1 = unsafeString{ptr, size}
381 var s2 = unsafeString{unsafeZeroAddr, size}
382 if size > len(unsafeZeroArr) {
383 arr := make([]byte, size)
384 s2.Data = unsafe.Pointer(&arr[0])
385 }
386 return *(*string)(unsafe.Pointer(&s1)) == *(*string)(unsafe.Pointer(&s2)) // memcmp
387 }
388
389 func isEmptyValue(v reflect.Value, tinfos *TypeInfos, recursive bool) bool {
390 urv := (*unsafeReflectValue)(unsafe.Pointer(&v))
391 if urv.flag == 0 {
392 return true
393 }
394 if recursive {
395 return isEmptyValueFallbackRecur(urv, v, tinfos)
396 }
397 return unsafeCmpZero(urv.ptr, int(rtsize2(urv.typ)))
398 }
399
400 func isEmptyValueFallbackRecur(urv *unsafeReflectValue, v reflect.Value, tinfos *TypeInfos) bool {
401 const recursive = true
402
403 switch v.Kind() {
404 case reflect.Invalid:
405 return true
406 case reflect.String:
407 return (*unsafeString)(urv.ptr).Len == 0
408 case reflect.Slice:
409 return (*unsafeSlice)(urv.ptr).Len == 0
410 case reflect.Bool:
411 return !*(*bool)(urv.ptr)
412 case reflect.Int:
413 return *(*int)(urv.ptr) == 0
414 case reflect.Int8:
415 return *(*int8)(urv.ptr) == 0
416 case reflect.Int16:
417 return *(*int16)(urv.ptr) == 0
418 case reflect.Int32:
419 return *(*int32)(urv.ptr) == 0
420 case reflect.Int64:
421 return *(*int64)(urv.ptr) == 0
422 case reflect.Uint:
423 return *(*uint)(urv.ptr) == 0
424 case reflect.Uint8:
425 return *(*uint8)(urv.ptr) == 0
426 case reflect.Uint16:
427 return *(*uint16)(urv.ptr) == 0
428 case reflect.Uint32:
429 return *(*uint32)(urv.ptr) == 0
430 case reflect.Uint64:
431 return *(*uint64)(urv.ptr) == 0
432 case reflect.Uintptr:
433 return *(*uintptr)(urv.ptr) == 0
434 case reflect.Float32:
435 return *(*float32)(urv.ptr) == 0
436 case reflect.Float64:
437 return *(*float64)(urv.ptr) == 0
438 case reflect.Complex64:
439 return unsafeCmpZero(urv.ptr, 8)
440 case reflect.Complex128:
441 return unsafeCmpZero(urv.ptr, 16)
442 case reflect.Struct:
443 // return isEmptyStruct(v, tinfos, recursive)
444 if tinfos == nil {
445 tinfos = defTypeInfos
446 }
447 ti := tinfos.find(uintptr(urv.typ))
448 if ti == nil {
449 ti = tinfos.load(rvType(v))
450 }
451 return unsafeCmpZero(urv.ptr, int(ti.size))
452 case reflect.Interface, reflect.Ptr:
453 // isnil := urv.ptr == nil // (not sufficient, as a pointer value encodes the type)
454 isnil := urv.ptr == nil || *(*unsafe.Pointer)(urv.ptr) == nil
455 if recursive && !isnil {
456 return isEmptyValue(v.Elem(), tinfos, recursive)
457 }
458 return isnil
459 case reflect.UnsafePointer:
460 return urv.ptr == nil || *(*unsafe.Pointer)(urv.ptr) == nil
461 case reflect.Chan:
462 return urv.ptr == nil || len_chan(rvRefPtr(urv)) == 0
463 case reflect.Map:
464 return urv.ptr == nil || len_map(rvRefPtr(urv)) == 0
465 case reflect.Array:
466 return v.Len() == 0 ||
467 urv.ptr == nil ||
468 urv.typ == nil ||
469 rtsize2(urv.typ) == 0 ||
470 unsafeCmpZero(urv.ptr, int(rtsize2(urv.typ)))
471 }
472 return false
473 }
474
475 // --------------------------
476
477 type structFieldInfos struct {
478 c unsafe.Pointer // source
479 s unsafe.Pointer // sorted
480 length int
481 }
482
483 func (x *structFieldInfos) load(source, sorted []*structFieldInfo) {
484 s := (*unsafeSlice)(unsafe.Pointer(&sorted))
485 x.s = s.Data
486 x.length = s.Len
487 s = (*unsafeSlice)(unsafe.Pointer(&source))
488 x.c = s.Data
489 }
490
491 func (x *structFieldInfos) sorted() (v []*structFieldInfo) {
492 *(*unsafeSlice)(unsafe.Pointer(&v)) = unsafeSlice{x.s, x.length, x.length}
493 // s := (*unsafeSlice)(unsafe.Pointer(&v))
494 // s.Data = x.sorted0
495 // s.Len = x.length
496 // s.Cap = s.Len
497 return
498 }
499
500 func (x *structFieldInfos) source() (v []*structFieldInfo) {
501 *(*unsafeSlice)(unsafe.Pointer(&v)) = unsafeSlice{x.c, x.length, x.length}
502 return
503 }
504
505 // atomicXXX is expected to be 2 words (for symmetry with atomic.Value)
506 //
507 // Note that we do not atomically load/store length and data pointer separately,
508 // as this could lead to some races. Instead, we atomically load/store cappedSlice.
509 //
510 // Note: with atomic.(Load|Store)Pointer, we MUST work with an unsafe.Pointer directly.
511
512 // ----------------------
513 type atomicTypeInfoSlice struct {
514 v unsafe.Pointer // *[]rtid2ti
515 }
516
517 func (x *atomicTypeInfoSlice) load() (s []rtid2ti) {
518 x2 := atomic.LoadPointer(&x.v)
519 if x2 != nil {
520 s = *(*[]rtid2ti)(x2)
521 }
522 return
523 }
524
525 func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
526 atomic.StorePointer(&x.v, unsafe.Pointer(&p))
527 }
528
529 // MARKER: in safe mode, atomicXXX are atomic.Value, which contains an interface{}.
530 // This is 2 words.
531 // consider padding atomicXXX here with a uintptr, so they fit into 2 words also.
532
533 // --------------------------
534 type atomicRtidFnSlice struct {
535 v unsafe.Pointer // *[]codecRtidFn
536 }
537
538 func (x *atomicRtidFnSlice) load() (s []codecRtidFn) {
539 x2 := atomic.LoadPointer(&x.v)
540 if x2 != nil {
541 s = *(*[]codecRtidFn)(x2)
542 }
543 return
544 }
545
546 func (x *atomicRtidFnSlice) store(p []codecRtidFn) {
547 atomic.StorePointer(&x.v, unsafe.Pointer(&p))
548 }
549
550 // --------------------------
551 type atomicClsErr struct {
552 v unsafe.Pointer // *clsErr
553 }
554
555 func (x *atomicClsErr) load() (e clsErr) {
556 x2 := (*clsErr)(atomic.LoadPointer(&x.v))
557 if x2 != nil {
558 e = *x2
559 }
560 return
561 }
562
563 func (x *atomicClsErr) store(p clsErr) {
564 atomic.StorePointer(&x.v, unsafe.Pointer(&p))
565 }
566
567 // --------------------------
568
569 // to create a reflect.Value for each member field of fauxUnion,
570 // we first create a global fauxUnion, and create reflect.Value
571 // for them all.
572 // This way, we have the flags and type in the reflect.Value.
573 // Then, when a reflect.Value is called, we just copy it,
574 // update the ptr to the fauxUnion's, and return it.
575
576 type unsafeDecNakedWrapper struct {
577 fauxUnion
578 ru, ri, rf, rl, rs, rb, rt reflect.Value // mapping to the primitives above
579 }
580
581 func (n *unsafeDecNakedWrapper) init() {
582 n.ru = rv4iptr(&n.u).Elem()
583 n.ri = rv4iptr(&n.i).Elem()
584 n.rf = rv4iptr(&n.f).Elem()
585 n.rl = rv4iptr(&n.l).Elem()
586 n.rs = rv4iptr(&n.s).Elem()
587 n.rt = rv4iptr(&n.t).Elem()
588 n.rb = rv4iptr(&n.b).Elem()
589 // n.rr[] = reflect.ValueOf(&n.)
590 }
591
592 var defUnsafeDecNakedWrapper unsafeDecNakedWrapper
593
594 func init() {
595 defUnsafeDecNakedWrapper.init()
596 }
597
598 func (n *fauxUnion) ru() (v reflect.Value) {
599 v = defUnsafeDecNakedWrapper.ru
600 ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.u)
601 return
602 }
603 func (n *fauxUnion) ri() (v reflect.Value) {
604 v = defUnsafeDecNakedWrapper.ri
605 ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.i)
606 return
607 }
608 func (n *fauxUnion) rf() (v reflect.Value) {
609 v = defUnsafeDecNakedWrapper.rf
610 ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.f)
611 return
612 }
613 func (n *fauxUnion) rl() (v reflect.Value) {
614 v = defUnsafeDecNakedWrapper.rl
615 ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.l)
616 return
617 }
618 func (n *fauxUnion) rs() (v reflect.Value) {
619 v = defUnsafeDecNakedWrapper.rs
620 ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.s)
621 return
622 }
623 func (n *fauxUnion) rt() (v reflect.Value) {
624 v = defUnsafeDecNakedWrapper.rt
625 ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.t)
626 return
627 }
628 func (n *fauxUnion) rb() (v reflect.Value) {
629 v = defUnsafeDecNakedWrapper.rb
630 ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.b)
631 return
632 }
633
634 // --------------------------
635 func rvSetBytes(rv reflect.Value, v []byte) {
636 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
637 *(*[]byte)(urv.ptr) = v
638 }
639
640 func rvSetString(rv reflect.Value, v string) {
641 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
642 *(*string)(urv.ptr) = v
643 }
644
645 func rvSetBool(rv reflect.Value, v bool) {
646 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
647 *(*bool)(urv.ptr) = v
648 }
649
650 func rvSetTime(rv reflect.Value, v time.Time) {
651 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
652 *(*time.Time)(urv.ptr) = v
653 }
654
655 func rvSetFloat32(rv reflect.Value, v float32) {
656 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
657 *(*float32)(urv.ptr) = v
658 }
659
660 func rvSetFloat64(rv reflect.Value, v float64) {
661 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
662 *(*float64)(urv.ptr) = v
663 }
664
665 func rvSetComplex64(rv reflect.Value, v complex64) {
666 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
667 *(*complex64)(urv.ptr) = v
668 }
669
670 func rvSetComplex128(rv reflect.Value, v complex128) {
671 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
672 *(*complex128)(urv.ptr) = v
673 }
674
675 func rvSetInt(rv reflect.Value, v int) {
676 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
677 *(*int)(urv.ptr) = v
678 }
679
680 func rvSetInt8(rv reflect.Value, v int8) {
681 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
682 *(*int8)(urv.ptr) = v
683 }
684
685 func rvSetInt16(rv reflect.Value, v int16) {
686 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
687 *(*int16)(urv.ptr) = v
688 }
689
690 func rvSetInt32(rv reflect.Value, v int32) {
691 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
692 *(*int32)(urv.ptr) = v
693 }
694
695 func rvSetInt64(rv reflect.Value, v int64) {
696 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
697 *(*int64)(urv.ptr) = v
698 }
699
700 func rvSetUint(rv reflect.Value, v uint) {
701 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
702 *(*uint)(urv.ptr) = v
703 }
704
705 func rvSetUintptr(rv reflect.Value, v uintptr) {
706 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
707 *(*uintptr)(urv.ptr) = v
708 }
709
710 func rvSetUint8(rv reflect.Value, v uint8) {
711 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
712 *(*uint8)(urv.ptr) = v
713 }
714
715 func rvSetUint16(rv reflect.Value, v uint16) {
716 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
717 *(*uint16)(urv.ptr) = v
718 }
719
720 func rvSetUint32(rv reflect.Value, v uint32) {
721 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
722 *(*uint32)(urv.ptr) = v
723 }
724
725 func rvSetUint64(rv reflect.Value, v uint64) {
726 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
727 *(*uint64)(urv.ptr) = v
728 }
729
730 // ----------------
731
732 // rvSetZero is rv.Set(reflect.Zero(rv.Type()) for all kinds (including reflect.Interface).
733 func rvSetZero(rv reflect.Value) {
734 rvSetDirectZero(rv)
735 }
736
737 func rvSetIntf(rv reflect.Value, v reflect.Value) {
738 rv.Set(v)
739 }
740
741 // rvSetDirect is rv.Set for all kinds except reflect.Interface.
742 //
743 // Callers MUST not pass a value of kind reflect.Interface, as it may cause unexpected segfaults.
744 func rvSetDirect(rv reflect.Value, v reflect.Value) {
745 // MARKER: rv.Set for kind reflect.Interface may do a separate allocation if a scalar value.
746 // The book-keeping is onerous, so we just do the simple ones where a memmove is sufficient.
747 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
748 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
749 if uv.flag&unsafeFlagIndir == 0 {
750 *(*unsafe.Pointer)(urv.ptr) = uv.ptr
751 } else if uv.ptr == unsafeZeroAddr {
752 if urv.ptr != unsafeZeroAddr {
753 typedmemclr(urv.typ, urv.ptr)
754 }
755 } else {
756 typedmemmove(urv.typ, urv.ptr, uv.ptr)
757 }
758 }
759
760 // rvSetDirectZero is rv.Set(reflect.Zero(rv.Type()) for all kinds except reflect.Interface.
761 func rvSetDirectZero(rv reflect.Value) {
762 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
763 if urv.ptr != unsafeZeroAddr {
764 typedmemclr(urv.typ, urv.ptr)
765 }
766 }
767
768 // rvMakeSlice updates the slice to point to a new array.
769 // It copies data from old slice to new slice.
770 // It returns set=true iff it updates it, else it just returns a new slice pointing to a newly made array.
771 func rvMakeSlice(rv reflect.Value, ti *typeInfo, xlen, xcap int) (_ reflect.Value, set bool) {
772 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
773 ux := (*unsafeSlice)(urv.ptr)
774 t := ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).ptr
775 s := unsafeSlice{newarray(t, xcap), xlen, xcap}
776 if ux.Len > 0 {
777 typedslicecopy(t, s, *ux)
778 }
779 *ux = s
780 return rv, true
781 }
782
783 // rvSlice returns a sub-slice of the slice given new lenth,
784 // without modifying passed in value.
785 // It is typically called when we know that SetLen(...) cannot be done.
786 func rvSlice(rv reflect.Value, length int) reflect.Value {
787 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
788 var x []struct{}
789 ux := (*unsafeSlice)(unsafe.Pointer(&x))
790 *ux = *(*unsafeSlice)(urv.ptr)
791 ux.Len = length
792 urv.ptr = unsafe.Pointer(ux)
793 return rv
794 }
795
796 // rcGrowSlice updates the slice to point to a new array with the cap incremented, and len set to the new cap value.
797 // It copies data from old slice to new slice.
798 // It returns set=true iff it updates it, else it just returns a new slice pointing to a newly made array.
799 func rvGrowSlice(rv reflect.Value, ti *typeInfo, cap, incr int) (v reflect.Value, newcap int, set bool) {
800 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
801 ux := (*unsafeSlice)(urv.ptr)
802 t := ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).ptr
803 *ux = unsafeGrowslice(t, *ux, cap, incr)
804 ux.Len = ux.Cap
805 return rv, ux.Cap, true
806 }
807
808 // ------------
809
810 func rvSliceIndex(rv reflect.Value, i int, ti *typeInfo) (v reflect.Value) {
811 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
812 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
813 uv.ptr = unsafe.Pointer(uintptr(((*unsafeSlice)(urv.ptr)).Data) + uintptr(int(ti.elemsize)*i))
814 uv.typ = ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).ptr
815 uv.flag = uintptr(ti.elemkind) | unsafeFlagIndir | unsafeFlagAddr
816 return
817 }
818
819 func rvSliceZeroCap(t reflect.Type) (v reflect.Value) {
820 urv := (*unsafeReflectValue)(unsafe.Pointer(&v))
821 urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
822 urv.flag = uintptr(reflect.Slice) | unsafeFlagIndir
823 urv.ptr = unsafe.Pointer(&unsafeZeroSlice)
824 return
825 }
826
827 func rvLenSlice(rv reflect.Value) int {
828 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
829 return (*unsafeSlice)(urv.ptr).Len
830 }
831
832 func rvCapSlice(rv reflect.Value) int {
833 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
834 return (*unsafeSlice)(urv.ptr).Cap
835 }
836
837 func rvArrayIndex(rv reflect.Value, i int, ti *typeInfo) (v reflect.Value) {
838 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
839 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
840 uv.ptr = unsafe.Pointer(uintptr(urv.ptr) + uintptr(int(ti.elemsize)*i))
841 uv.typ = ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).ptr
842 uv.flag = uintptr(ti.elemkind) | unsafeFlagIndir | unsafeFlagAddr
843 return
844 }
845
846 // if scratch is nil, then return a writable view (assuming canAddr=true)
847 func rvGetArrayBytes(rv reflect.Value, scratch []byte) (bs []byte) {
848 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
849 bx := (*unsafeSlice)(unsafe.Pointer(&bs))
850 bx.Data = urv.ptr
851 bx.Len = rv.Len()
852 bx.Cap = bx.Len
853 return
854 }
855
856 func rvGetArray4Slice(rv reflect.Value) (v reflect.Value) {
857 // It is possible that this slice is based off an array with a larger
858 // len that we want (where array len == slice cap).
859 // However, it is ok to create an array type that is a subset of the full
860 // e.g. full slice is based off a *[16]byte, but we can create a *[4]byte
861 // off of it. That is ok.
862 //
863 // Consequently, we use rvLenSlice, not rvCapSlice.
864
865 t := reflectArrayOf(rvLenSlice(rv), rvType(rv).Elem())
866 // v = rvZeroAddrK(t, reflect.Array)
867
868 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
869 uv.flag = uintptr(reflect.Array) | unsafeFlagIndir | unsafeFlagAddr
870 uv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
871
872 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
873 uv.ptr = *(*unsafe.Pointer)(urv.ptr) // slice rv has a ptr to the slice.
874
875 return
876 }
877
878 func rvGetSlice4Array(rv reflect.Value, v interface{}) {
879 // v is a pointer to a slice to be populated
880 uv := (*unsafeIntf)(unsafe.Pointer(&v))
881 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
882
883 s := (*unsafeSlice)(uv.ptr)
884 s.Data = urv.ptr
885 s.Len = rv.Len()
886 s.Cap = s.Len
887 }
888
889 func rvCopySlice(dest, src reflect.Value, elemType reflect.Type) {
890 typedslicecopy((*unsafeIntf)(unsafe.Pointer(&elemType)).ptr,
891 *(*unsafeSlice)((*unsafeReflectValue)(unsafe.Pointer(&dest)).ptr),
892 *(*unsafeSlice)((*unsafeReflectValue)(unsafe.Pointer(&src)).ptr))
893 }
894
895 // ------------
896
897 func rvGetBool(rv reflect.Value) bool {
898 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
899 return *(*bool)(v.ptr)
900 }
901
902 func rvGetBytes(rv reflect.Value) []byte {
903 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
904 return *(*[]byte)(v.ptr)
905 }
906
907 func rvGetTime(rv reflect.Value) time.Time {
908 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
909 return *(*time.Time)(v.ptr)
910 }
911
912 func rvGetString(rv reflect.Value) string {
913 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
914 return *(*string)(v.ptr)
915 }
916
917 func rvGetFloat64(rv reflect.Value) float64 {
918 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
919 return *(*float64)(v.ptr)
920 }
921
922 func rvGetFloat32(rv reflect.Value) float32 {
923 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
924 return *(*float32)(v.ptr)
925 }
926
927 func rvGetComplex64(rv reflect.Value) complex64 {
928 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
929 return *(*complex64)(v.ptr)
930 }
931
932 func rvGetComplex128(rv reflect.Value) complex128 {
933 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
934 return *(*complex128)(v.ptr)
935 }
936
937 func rvGetInt(rv reflect.Value) int {
938 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
939 return *(*int)(v.ptr)
940 }
941
942 func rvGetInt8(rv reflect.Value) int8 {
943 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
944 return *(*int8)(v.ptr)
945 }
946
947 func rvGetInt16(rv reflect.Value) int16 {
948 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
949 return *(*int16)(v.ptr)
950 }
951
952 func rvGetInt32(rv reflect.Value) int32 {
953 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
954 return *(*int32)(v.ptr)
955 }
956
957 func rvGetInt64(rv reflect.Value) int64 {
958 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
959 return *(*int64)(v.ptr)
960 }
961
962 func rvGetUint(rv reflect.Value) uint {
963 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
964 return *(*uint)(v.ptr)
965 }
966
967 func rvGetUint8(rv reflect.Value) uint8 {
968 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
969 return *(*uint8)(v.ptr)
970 }
971
972 func rvGetUint16(rv reflect.Value) uint16 {
973 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
974 return *(*uint16)(v.ptr)
975 }
976
977 func rvGetUint32(rv reflect.Value) uint32 {
978 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
979 return *(*uint32)(v.ptr)
980 }
981
982 func rvGetUint64(rv reflect.Value) uint64 {
983 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
984 return *(*uint64)(v.ptr)
985 }
986
987 func rvGetUintptr(rv reflect.Value) uintptr {
988 v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
989 return *(*uintptr)(v.ptr)
990 }
991
992 func rvLenMap(rv reflect.Value) int {
993 // maplen is not inlined, because as of go1.16beta, go:linkname's are not inlined.
994 // thus, faster to call rv.Len() directly.
995 //
996 // MARKER: review after https://github.com/golang/go/issues/20019 fixed.
997
998 // return rv.Len()
999
1000 return len_map(rvRefPtr((*unsafeReflectValue)(unsafe.Pointer(&rv))))
1001 }
1002
1003 // Note: it is hard to find len(...) of an array type,
1004 // as that is a field in the arrayType representing the array, and hard to introspect.
1005 //
1006 // func rvLenArray(rv reflect.Value) int { return rv.Len() }
1007
1008 // ------------ map range and map indexing ----------
1009
1010 // regular calls to map via reflection: MapKeys, MapIndex, MapRange/MapIter etc
1011 // will always allocate for each map key or value.
1012 //
1013 // It is more performant to provide a value that the map entry is set into,
1014 // and that elides the allocation.
1015
1016 // go 1.4+ has runtime/hashmap.go or runtime/map.go which has a
1017 // hIter struct with the first 2 values being key and value
1018 // of the current iteration.
1019 //
1020 // This *hIter is passed to mapiterinit, mapiternext, mapiterkey, mapiterelem.
1021 // We bypass the reflect wrapper functions and just use the *hIter directly.
1022 //
1023 // Though *hIter has many fields, we only care about the first 2.
1024 //
1025 // We directly embed this in unsafeMapIter below
1026 //
1027 // hiter is typically about 12 words, but we just fill up unsafeMapIter to 32 words,
1028 // so it fills multiple cache lines and can give some extra space to accomodate small growth.
1029
1030 type unsafeMapIter struct {
1031 mtyp, mptr unsafe.Pointer
1032 k, v reflect.Value
1033 kisref bool
1034 visref bool
1035 mapvalues bool
1036 done bool
1037 started bool
1038 _ [3]byte // padding
1039 it struct {
1040 key unsafe.Pointer
1041 value unsafe.Pointer
1042 _ [20]uintptr // padding for other fields (to make up 32 words for enclosing struct)
1043 }
1044 }
1045
1046 func (t *unsafeMapIter) Next() (r bool) {
1047 if t == nil || t.done {
1048 return
1049 }
1050 if t.started {
1051 mapiternext((unsafe.Pointer)(&t.it))
1052 } else {
1053 t.started = true
1054 }
1055
1056 t.done = t.it.key == nil
1057 if t.done {
1058 return
1059 }
1060
1061 if helperUnsafeDirectAssignMapEntry || t.kisref {
1062 (*unsafeReflectValue)(unsafe.Pointer(&t.k)).ptr = t.it.key
1063 } else {
1064 k := (*unsafeReflectValue)(unsafe.Pointer(&t.k))
1065 typedmemmove(k.typ, k.ptr, t.it.key)
1066 }
1067
1068 if t.mapvalues {
1069 if helperUnsafeDirectAssignMapEntry || t.visref {
1070 (*unsafeReflectValue)(unsafe.Pointer(&t.v)).ptr = t.it.value
1071 } else {
1072 v := (*unsafeReflectValue)(unsafe.Pointer(&t.v))
1073 typedmemmove(v.typ, v.ptr, t.it.value)
1074 }
1075 }
1076
1077 return true
1078 }
1079
1080 func (t *unsafeMapIter) Key() (r reflect.Value) {
1081 return t.k
1082 }
1083
1084 func (t *unsafeMapIter) Value() (r reflect.Value) {
1085 return t.v
1086 }
1087
1088 func (t *unsafeMapIter) Done() {}
1089
1090 type mapIter struct {
1091 unsafeMapIter
1092 }
1093
1094 func mapRange(t *mapIter, m, k, v reflect.Value, mapvalues bool) {
1095 if rvIsNil(m) {
1096 t.done = true
1097 return
1098 }
1099 t.done = false
1100 t.started = false
1101 t.mapvalues = mapvalues
1102
1103 // var urv *unsafeReflectValue
1104
1105 urv := (*unsafeReflectValue)(unsafe.Pointer(&m))
1106 t.mtyp = urv.typ
1107 t.mptr = rvRefPtr(urv)
1108
1109 // t.it = (*unsafeMapHashIter)(reflect_mapiterinit(t.mtyp, t.mptr))
1110 mapiterinit(t.mtyp, t.mptr, unsafe.Pointer(&t.it))
1111
1112 t.k = k
1113 t.kisref = refBitset.isset(byte(k.Kind()))
1114
1115 if mapvalues {
1116 t.v = v
1117 t.visref = refBitset.isset(byte(v.Kind()))
1118 } else {
1119 t.v = reflect.Value{}
1120 }
1121 }
1122
1123 // unsafeMapKVPtr returns the pointer if flagIndir, else it returns a pointer to the pointer.
1124 // It is needed as maps always keep a reference to the underlying value.
1125 func unsafeMapKVPtr(urv *unsafeReflectValue) unsafe.Pointer {
1126 if urv.flag&unsafeFlagIndir == 0 {
1127 return unsafe.Pointer(&urv.ptr)
1128 }
1129 return urv.ptr
1130 }
1131
1132 // func mapDelete(m, k reflect.Value) {
1133 // var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
1134 // var kptr = unsafeMapKVPtr(urv)
1135 // urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
1136 // mapdelete(urv.typ, rv2ptr(urv), kptr)
1137 // }
1138
1139 // return an addressable reflect value that can be used in mapRange and mapGet operations.
1140 //
1141 // all calls to mapGet or mapRange will call here to get an addressable reflect.Value.
1142 func mapAddrLoopvarRV(t reflect.Type, k reflect.Kind) (rv reflect.Value) {
1143 // return rvZeroAddrK(t, k)
1144 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
1145 urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
1146 urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).ptr
1147 // since we always set the ptr when helperUnsafeDirectAssignMapEntry=true,
1148 // we should only allocate if it is not true
1149 if !helperUnsafeDirectAssignMapEntry {
1150 urv.ptr = unsafeNew(urv.typ)
1151 }
1152 return
1153 }
1154
1155 // ---------- ENCODER optimized ---------------
1156
1157 func (e *Encoder) jsondriver() *jsonEncDriver {
1158 return (*jsonEncDriver)((*unsafeIntf)(unsafe.Pointer(&e.e)).ptr)
1159 }
1160
1161 func (d *Decoder) zerocopystate() bool {
1162 return d.decByteState == decByteStateZerocopy && d.h.ZeroCopy
1163 }
1164
1165 func (d *Decoder) stringZC(v []byte) (s string) {
1166 if d.zerocopystate() {
1167 return stringView(v)
1168 }
1169 return d.string(v)
1170 }
1171
1172 func (d *Decoder) mapKeyString(callFnRvk *bool, kstrbs, kstr2bs *[]byte) string {
1173 if !d.zerocopystate() {
1174 *callFnRvk = true
1175 if d.decByteState == decByteStateReuseBuf {
1176 *kstrbs = append((*kstrbs)[:0], (*kstr2bs)...)
1177 *kstr2bs = *kstrbs
1178 }
1179 }
1180 return stringView(*kstr2bs)
1181 }
1182
1183 // ---------- DECODER optimized ---------------
1184
1185 func (d *Decoder) checkBreak() bool {
1186 // MARKER: jsonDecDriver.CheckBreak() costs over 80, and this isn't inlined.
1187 // Consequently, there's no benefit in incurring the cost of this
1188 // wrapping function checkBreak.
1189 //
1190 // It is faster to just call the interface method directly.
1191
1192 // if d.js {
1193 // return d.jsondriver().CheckBreak()
1194 // }
1195 // if d.cbor {
1196 // return d.cbordriver().CheckBreak()
1197 // }
1198 return d.d.CheckBreak()
1199 }
1200
1201 func (d *Decoder) jsondriver() *jsonDecDriver {
1202 return (*jsonDecDriver)((*unsafeIntf)(unsafe.Pointer(&d.d)).ptr)
1203 }
1204
1205 // ---------- structFieldInfo optimized ---------------
1206
1207 func (n *structFieldInfoPathNode) rvField(v reflect.Value) (rv reflect.Value) {
1208 // we already know this is exported, and maybe embedded (based on what si says)
1209 uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
1210 urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
1211 // clear flagEmbedRO if necessary, and inherit permission bits from v
1212 urv.flag = uv.flag&(unsafeFlagStickyRO|unsafeFlagIndir|unsafeFlagAddr) | uintptr(n.kind)
1213 urv.typ = ((*unsafeIntf)(unsafe.Pointer(&n.typ))).ptr
1214 urv.ptr = unsafe.Pointer(uintptr(uv.ptr) + uintptr(n.offset))
1215 return
1216 }
1217
1218 // runtime chan and map are designed such that the first field is the count.
1219 // len builtin uses this to get the length of a chan/map easily.
1220 // leverage this knowledge, since maplen and chanlen functions from runtime package
1221 // are go:linkname'd here, and thus not inlined as of go1.16beta
1222
1223 func len_map_chan(m unsafe.Pointer) int {
1224 if m == nil {
1225 return 0
1226 }
1227 return *((*int)(m))
1228 }
1229
1230 func len_map(m unsafe.Pointer) int {
1231 // return maplen(m)
1232 return len_map_chan(m)
1233 }
1234 func len_chan(m unsafe.Pointer) int {
1235 // return chanlen(m)
1236 return len_map_chan(m)
1237 }
1238
1239 func unsafeNew(typ unsafe.Pointer) unsafe.Pointer {
1240 return mallocgc(rtsize2(typ), typ, true)
1241 }
1242
1243 // ---------- go linknames (LINKED to runtime/reflect) ---------------
1244
1245 // MARKER: always check that these linknames match subsequent versions of go
1246 //
1247 // Note that as of Jan 2021 (go 1.16 release), go:linkname(s) are not inlined
1248 // outside of the standard library use (e.g. within sync, reflect, etc).
1249 // If these link'ed functions were normally inlined, calling them here would
1250 // not necessarily give a performance boost, due to function overhead.
1251 //
1252 // However, it seems most of these functions are not inlined anyway,
1253 // as only maplen, chanlen and mapaccess are small enough to get inlined.
1254 //
1255 // We checked this by going into $GOROOT/src/runtime and running:
1256 // $ go build -tags codec.notfastpath -gcflags "-m=2"
1257
1258 // reflect.{unsafe_New, unsafe_NewArray} are not supported in gollvm,
1259 // failing with "error: undefined reference" error.
1260 // however, runtime.{mallocgc, newarray} are supported, so use that instead.
1261
1262 //go:linkname mallocgc runtime.mallocgc
1263 //go:noescape
1264 func mallocgc(size uintptr, typ unsafe.Pointer, needzero bool) unsafe.Pointer
1265
1266 //go:linkname newarray runtime.newarray
1267 //go:noescape
1268 func newarray(typ unsafe.Pointer, n int) unsafe.Pointer
1269
1270 //go:linkname mapiterinit runtime.mapiterinit
1271 //go:noescape
1272 func mapiterinit(typ unsafe.Pointer, m unsafe.Pointer, it unsafe.Pointer)
1273
1274 //go:linkname mapiternext runtime.mapiternext
1275 //go:noescape
1276 func mapiternext(it unsafe.Pointer) (key unsafe.Pointer)
1277
1278 //go:linkname mapdelete runtime.mapdelete
1279 //go:noescape
1280 func mapdelete(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer)
1281
1282 //go:linkname mapassign runtime.mapassign
1283 //go:noescape
1284 func mapassign(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer) unsafe.Pointer
1285
1286 //go:linkname mapaccess2 runtime.mapaccess2
1287 //go:noescape
1288 func mapaccess2(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer) (val unsafe.Pointer, ok bool)
1289
1290 // reflect.typed{memmove, memclr, slicecopy} will handle checking if the type has pointers or not,
1291 // and if a writeBarrier is needed, before delegating to the right method in the runtime.
1292 //
1293 // This is why we use the functions in reflect, and not the ones in runtime directly.
1294 // Calling runtime.XXX here will lead to memory issues.
1295
1296 //go:linkname typedslicecopy reflect.typedslicecopy
1297 //go:noescape
1298 func typedslicecopy(elemType unsafe.Pointer, dst, src unsafeSlice) int
1299
1300 //go:linkname typedmemmove reflect.typedmemmove
1301 //go:noescape
1302 func typedmemmove(typ unsafe.Pointer, dst, src unsafe.Pointer)
1303
1304 //go:linkname typedmemclr reflect.typedmemclr
1305 //go:noescape
1306 func typedmemclr(typ unsafe.Pointer, dst unsafe.Pointer)