helper_unsafe.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. // +build !safe
  2. // +build !appengine
  3. // +build go1.7
  4. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  5. // Use of this source code is governed by a MIT license found in the LICENSE file.
  6. package codec
  7. import (
  8. "reflect"
  9. "sync/atomic"
  10. "time"
  11. "unsafe"
  12. )
  13. // This file has unsafe variants of some helper methods.
  14. // NOTE: See helper_not_unsafe.go for the usage information.
  15. // For reflect.Value code, we decided to do the following:
  16. // - if we know the kind, we can elide conditional checks for
  17. // - SetXXX (Int, Uint, String, Bool, etc)
  18. // - SetLen
  19. //
  20. // We can also optimize
  21. // - IsNil
  22. const safeMode = false
  23. // keep in sync with GO_ROOT/src/reflect/value.go
  24. const (
  25. unsafeFlagIndir = 1 << 7
  26. unsafeFlagAddr = 1 << 8
  27. unsafeFlagKindMask = (1 << 5) - 1 // 5 bits for 27 kinds (up to 31)
  28. // unsafeTypeKindDirectIface = 1 << 5
  29. )
  30. type unsafeString struct {
  31. Data unsafe.Pointer
  32. Len int
  33. }
  34. type unsafeSlice struct {
  35. Data unsafe.Pointer
  36. Len int
  37. Cap int
  38. }
  39. type unsafeIntf struct {
  40. typ unsafe.Pointer
  41. word unsafe.Pointer
  42. }
  43. type unsafeReflectValue struct {
  44. typ unsafe.Pointer
  45. ptr unsafe.Pointer
  46. flag uintptr
  47. }
  48. func stringView(v []byte) string {
  49. if len(v) == 0 {
  50. return ""
  51. }
  52. bx := (*unsafeSlice)(unsafe.Pointer(&v))
  53. return *(*string)(unsafe.Pointer(&unsafeString{bx.Data, bx.Len}))
  54. }
  55. func bytesView(v string) []byte {
  56. if len(v) == 0 {
  57. return zeroByteSlice
  58. }
  59. sx := (*unsafeString)(unsafe.Pointer(&v))
  60. return *(*[]byte)(unsafe.Pointer(&unsafeSlice{sx.Data, sx.Len, sx.Len}))
  61. }
  62. // // isNilRef says whether the interface is a nil reference or not.
  63. // //
  64. // // A reference here is a pointer-sized reference i.e. map, ptr, chan, func, unsafepointer.
  65. // // It is optional to extend this to also check if slices or interfaces are nil also.
  66. // //
  67. // // NOTE: There is no global way of checking if an interface is nil.
  68. // // For true references (map, ptr, func, chan), you can just look
  69. // // at the word of the interface.
  70. // // However, for slices, you have to dereference
  71. // // the word, and get a pointer to the 3-word interface value.
  72. // func isNilRef(v interface{}) (rv reflect.Value, isnil bool) {
  73. // isnil = ((*unsafeIntf)(unsafe.Pointer(&v))).word == nil
  74. // return
  75. // }
  76. func isNil(v interface{}) (rv reflect.Value, isnil bool) {
  77. var ui = (*unsafeIntf)(unsafe.Pointer(&v))
  78. if ui.word == nil {
  79. isnil = true
  80. return
  81. }
  82. rv = rv4i(v) // reflect.value is cheap and inline'able
  83. tk := rv.Kind()
  84. isnil = (tk == reflect.Interface || tk == reflect.Slice) && *(*unsafe.Pointer)(ui.word) == nil
  85. return
  86. }
  87. func rv2ptr(urv *unsafeReflectValue) (ptr unsafe.Pointer) {
  88. // true references (map, func, chan, ptr - NOT slice) may be double-referenced? as flagIndir
  89. if refBitset.isset(byte(urv.flag&unsafeFlagKindMask)) && urv.flag&unsafeFlagIndir != 0 {
  90. ptr = *(*unsafe.Pointer)(urv.ptr)
  91. } else {
  92. ptr = urv.ptr
  93. }
  94. return
  95. }
  96. func rv4i(i interface{}) (rv reflect.Value) {
  97. // Unfortunately, we cannot get the "kind" of the interface directly here.
  98. // We need the 'rtype', whose structure changes in different go versions.
  99. // Finally, it's not clear that there is benefit to reimplementing it,
  100. // as the "escapes(i)" is not clearly expensive since we want i to exist on the heap.
  101. return reflect.ValueOf(i)
  102. }
  103. func rv2i(rv reflect.Value) interface{} {
  104. // We tap into implememtation details from
  105. // the source go stdlib reflect/value.go, and trims the implementation.
  106. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  107. return *(*interface{})(unsafe.Pointer(&unsafeIntf{typ: urv.typ, word: rv2ptr(urv)}))
  108. }
  109. func rvIsNil(rv reflect.Value) bool {
  110. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  111. if urv.flag&unsafeFlagIndir != 0 {
  112. return *(*unsafe.Pointer)(urv.ptr) == nil
  113. }
  114. return urv.ptr == nil
  115. }
  116. func rvSetSliceLen(rv reflect.Value, length int) {
  117. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  118. (*unsafeString)(urv.ptr).Len = length
  119. }
  120. func rvZeroAddrK(t reflect.Type, k reflect.Kind) (rv reflect.Value) {
  121. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  122. urv.flag = uintptr(k) | unsafeFlagIndir | unsafeFlagAddr
  123. urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).word
  124. urv.ptr = unsafe_New(urv.typ)
  125. return
  126. }
  127. func rvConvert(v reflect.Value, t reflect.Type) (rv reflect.Value) {
  128. uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
  129. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  130. *urv = *uv
  131. urv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).word
  132. return
  133. }
  134. func rt2id(rt reflect.Type) uintptr {
  135. return uintptr(((*unsafeIntf)(unsafe.Pointer(&rt))).word)
  136. }
  137. func i2rtid(i interface{}) uintptr {
  138. return uintptr(((*unsafeIntf)(unsafe.Pointer(&i))).typ)
  139. }
  140. // --------------------------
  141. func isEmptyValue(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool {
  142. urv := (*unsafeReflectValue)(unsafe.Pointer(&v))
  143. if urv.flag == 0 {
  144. return true
  145. }
  146. switch v.Kind() {
  147. case reflect.Invalid:
  148. return true
  149. case reflect.String:
  150. return (*unsafeString)(urv.ptr).Len == 0
  151. case reflect.Slice:
  152. return (*unsafeSlice)(urv.ptr).Len == 0
  153. case reflect.Bool:
  154. return !*(*bool)(urv.ptr)
  155. case reflect.Int:
  156. return *(*int)(urv.ptr) == 0
  157. case reflect.Int8:
  158. return *(*int8)(urv.ptr) == 0
  159. case reflect.Int16:
  160. return *(*int16)(urv.ptr) == 0
  161. case reflect.Int32:
  162. return *(*int32)(urv.ptr) == 0
  163. case reflect.Int64:
  164. return *(*int64)(urv.ptr) == 0
  165. case reflect.Uint:
  166. return *(*uint)(urv.ptr) == 0
  167. case reflect.Uint8:
  168. return *(*uint8)(urv.ptr) == 0
  169. case reflect.Uint16:
  170. return *(*uint16)(urv.ptr) == 0
  171. case reflect.Uint32:
  172. return *(*uint32)(urv.ptr) == 0
  173. case reflect.Uint64:
  174. return *(*uint64)(urv.ptr) == 0
  175. case reflect.Uintptr:
  176. return *(*uintptr)(urv.ptr) == 0
  177. case reflect.Float32:
  178. return *(*float32)(urv.ptr) == 0
  179. case reflect.Float64:
  180. return *(*float64)(urv.ptr) == 0
  181. case reflect.Interface:
  182. isnil := urv.ptr == nil || *(*unsafe.Pointer)(urv.ptr) == nil
  183. if deref {
  184. if isnil {
  185. return true
  186. }
  187. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  188. }
  189. return isnil
  190. case reflect.Ptr:
  191. // isnil := urv.ptr == nil // (not sufficient, as a pointer value encodes the type)
  192. isnil := urv.ptr == nil || *(*unsafe.Pointer)(urv.ptr) == nil
  193. if deref {
  194. if isnil {
  195. return true
  196. }
  197. return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
  198. }
  199. return isnil
  200. case reflect.Struct:
  201. return isEmptyStruct(v, tinfos, deref, checkStruct)
  202. case reflect.Map, reflect.Array, reflect.Chan:
  203. return v.Len() == 0
  204. }
  205. return false
  206. }
  207. // --------------------------
  208. // atomicXXX is expected to be 2 words (for symmetry with atomic.Value)
  209. //
  210. // Note that we do not atomically load/store length and data pointer separately,
  211. // as this could lead to some races. Instead, we atomically load/store cappedSlice.
  212. //
  213. // Note: with atomic.(Load|Store)Pointer, we MUST work with an unsafe.Pointer directly.
  214. // ----------------------
  215. type atomicTypeInfoSlice struct {
  216. v unsafe.Pointer // *[]rtid2ti
  217. _ uint64 // padding (atomicXXX expected to be 2 words)
  218. }
  219. func (x *atomicTypeInfoSlice) load() (s []rtid2ti) {
  220. x2 := atomic.LoadPointer(&x.v)
  221. if x2 != nil {
  222. s = *(*[]rtid2ti)(x2)
  223. }
  224. return
  225. }
  226. func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
  227. atomic.StorePointer(&x.v, unsafe.Pointer(&p))
  228. }
  229. // --------------------------
  230. type atomicRtidFnSlice struct {
  231. v unsafe.Pointer // *[]codecRtidFn
  232. _ uint64 // padding (atomicXXX expected to be 2 words) (make 1 word so JsonHandle fits)
  233. }
  234. func (x *atomicRtidFnSlice) load() (s []codecRtidFn) {
  235. x2 := atomic.LoadPointer(&x.v)
  236. if x2 != nil {
  237. s = *(*[]codecRtidFn)(x2)
  238. }
  239. return
  240. }
  241. func (x *atomicRtidFnSlice) store(p []codecRtidFn) {
  242. atomic.StorePointer(&x.v, unsafe.Pointer(&p))
  243. }
  244. // --------------------------
  245. type atomicClsErr struct {
  246. v unsafe.Pointer // *clsErr
  247. _ uint64 // padding (atomicXXX expected to be 2 words)
  248. }
  249. func (x *atomicClsErr) load() (e clsErr) {
  250. x2 := (*clsErr)(atomic.LoadPointer(&x.v))
  251. if x2 != nil {
  252. e = *x2
  253. }
  254. return
  255. }
  256. func (x *atomicClsErr) store(p clsErr) {
  257. atomic.StorePointer(&x.v, unsafe.Pointer(&p))
  258. }
  259. // --------------------------
  260. // to create a reflect.Value for each member field of fauxUnion,
  261. // we first create a global fauxUnion, and create reflect.Value
  262. // for them all.
  263. // This way, we have the flags and type in the reflect.Value.
  264. // Then, when a reflect.Value is called, we just copy it,
  265. // update the ptr to the fauxUnion's, and return it.
  266. type unsafeDecNakedWrapper struct {
  267. fauxUnion
  268. ru, ri, rf, rl, rs, rb, rt reflect.Value // mapping to the primitives above
  269. }
  270. func (n *unsafeDecNakedWrapper) init() {
  271. n.ru = rv4i(&n.u).Elem()
  272. n.ri = rv4i(&n.i).Elem()
  273. n.rf = rv4i(&n.f).Elem()
  274. n.rl = rv4i(&n.l).Elem()
  275. n.rs = rv4i(&n.s).Elem()
  276. n.rt = rv4i(&n.t).Elem()
  277. n.rb = rv4i(&n.b).Elem()
  278. // n.rr[] = rv4i(&n.)
  279. }
  280. var defUnsafeDecNakedWrapper unsafeDecNakedWrapper
  281. func init() {
  282. defUnsafeDecNakedWrapper.init()
  283. }
  284. func (n *fauxUnion) ru() (v reflect.Value) {
  285. v = defUnsafeDecNakedWrapper.ru
  286. ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.u)
  287. return
  288. }
  289. func (n *fauxUnion) ri() (v reflect.Value) {
  290. v = defUnsafeDecNakedWrapper.ri
  291. ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.i)
  292. return
  293. }
  294. func (n *fauxUnion) rf() (v reflect.Value) {
  295. v = defUnsafeDecNakedWrapper.rf
  296. ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.f)
  297. return
  298. }
  299. func (n *fauxUnion) rl() (v reflect.Value) {
  300. v = defUnsafeDecNakedWrapper.rl
  301. ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.l)
  302. return
  303. }
  304. func (n *fauxUnion) rs() (v reflect.Value) {
  305. v = defUnsafeDecNakedWrapper.rs
  306. ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.s)
  307. return
  308. }
  309. func (n *fauxUnion) rt() (v reflect.Value) {
  310. v = defUnsafeDecNakedWrapper.rt
  311. ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.t)
  312. return
  313. }
  314. func (n *fauxUnion) rb() (v reflect.Value) {
  315. v = defUnsafeDecNakedWrapper.rb
  316. ((*unsafeReflectValue)(unsafe.Pointer(&v))).ptr = unsafe.Pointer(&n.b)
  317. return
  318. }
  319. // --------------------------
  320. func rvSetBytes(rv reflect.Value, v []byte) {
  321. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  322. *(*[]byte)(urv.ptr) = v
  323. }
  324. func rvSetString(rv reflect.Value, v string) {
  325. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  326. *(*string)(urv.ptr) = v
  327. }
  328. func rvSetBool(rv reflect.Value, v bool) {
  329. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  330. *(*bool)(urv.ptr) = v
  331. }
  332. func rvSetTime(rv reflect.Value, v time.Time) {
  333. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  334. *(*time.Time)(urv.ptr) = v
  335. }
  336. func rvSetFloat32(rv reflect.Value, v float32) {
  337. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  338. *(*float32)(urv.ptr) = v
  339. }
  340. func rvSetFloat64(rv reflect.Value, v float64) {
  341. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  342. *(*float64)(urv.ptr) = v
  343. }
  344. func rvSetInt(rv reflect.Value, v int) {
  345. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  346. *(*int)(urv.ptr) = v
  347. }
  348. func rvSetInt8(rv reflect.Value, v int8) {
  349. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  350. *(*int8)(urv.ptr) = v
  351. }
  352. func rvSetInt16(rv reflect.Value, v int16) {
  353. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  354. *(*int16)(urv.ptr) = v
  355. }
  356. func rvSetInt32(rv reflect.Value, v int32) {
  357. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  358. *(*int32)(urv.ptr) = v
  359. }
  360. func rvSetInt64(rv reflect.Value, v int64) {
  361. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  362. *(*int64)(urv.ptr) = v
  363. }
  364. func rvSetUint(rv reflect.Value, v uint) {
  365. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  366. *(*uint)(urv.ptr) = v
  367. }
  368. func rvSetUintptr(rv reflect.Value, v uintptr) {
  369. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  370. *(*uintptr)(urv.ptr) = v
  371. }
  372. func rvSetUint8(rv reflect.Value, v uint8) {
  373. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  374. *(*uint8)(urv.ptr) = v
  375. }
  376. func rvSetUint16(rv reflect.Value, v uint16) {
  377. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  378. *(*uint16)(urv.ptr) = v
  379. }
  380. func rvSetUint32(rv reflect.Value, v uint32) {
  381. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  382. *(*uint32)(urv.ptr) = v
  383. }
  384. func rvSetUint64(rv reflect.Value, v uint64) {
  385. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  386. *(*uint64)(urv.ptr) = v
  387. }
  388. // ----------------
  389. // rvSetDirect is rv.Set for all kinds except reflect.Interface
  390. func rvSetDirect(rv reflect.Value, v reflect.Value) {
  391. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  392. uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
  393. if uv.flag&unsafeFlagIndir == 0 {
  394. *(*unsafe.Pointer)(urv.ptr) = uv.ptr
  395. } else {
  396. typedmemmove(urv.typ, urv.ptr, uv.ptr)
  397. }
  398. }
  399. // rvSlice returns a slice of the slice of lenth
  400. func rvSlice(rv reflect.Value, length int) (v reflect.Value) {
  401. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  402. uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
  403. *uv = *urv
  404. var x []unsafe.Pointer
  405. uv.ptr = unsafe.Pointer(&x)
  406. *(*unsafeSlice)(uv.ptr) = *(*unsafeSlice)(urv.ptr)
  407. (*unsafeSlice)(uv.ptr).Len = length
  408. return
  409. }
  410. // ------------
  411. func rvSliceIndex(rv reflect.Value, i int, ti *typeInfo) (v reflect.Value) {
  412. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  413. uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
  414. uv.ptr = unsafe.Pointer(uintptr(((*unsafeSlice)(urv.ptr)).Data) + (ti.elemsize * uintptr(i)))
  415. uv.typ = ((*unsafeIntf)(unsafe.Pointer(&ti.elem))).word
  416. uv.flag = uintptr(ti.elemkind) | unsafeFlagIndir | unsafeFlagAddr
  417. return
  418. }
  419. func rvGetSliceLen(rv reflect.Value) int {
  420. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  421. return (*unsafeSlice)(urv.ptr).Len
  422. }
  423. func rvGetSliceCap(rv reflect.Value) int {
  424. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  425. return (*unsafeSlice)(urv.ptr).Cap
  426. }
  427. func rvGetArrayBytesRO(rv reflect.Value, scratch []byte) (bs []byte) {
  428. l := rv.Len()
  429. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  430. bx := (*unsafeSlice)(unsafe.Pointer(&bs))
  431. bx.Data = urv.ptr
  432. bx.Len, bx.Cap = l, l
  433. return
  434. }
  435. func rvGetArray4Slice(rv reflect.Value) (v reflect.Value) {
  436. // It is possible that this slice is based off an array with a larger
  437. // len that we want (where array len == slice cap).
  438. // However, it is ok to create an array type that is a subset of the full
  439. // e.g. full slice is based off a *[16]byte, but we can create a *[4]byte
  440. // off of it. That is ok.
  441. //
  442. // Consequently, we use rvGetSliceLen, not rvGetSliceCap.
  443. t := reflectArrayOf(rvGetSliceLen(rv), rv.Type().Elem())
  444. // v = rvZeroAddrK(t, reflect.Array)
  445. uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
  446. uv.flag = uintptr(reflect.Array) | unsafeFlagIndir | unsafeFlagAddr
  447. uv.typ = ((*unsafeIntf)(unsafe.Pointer(&t))).word
  448. urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  449. uv.ptr = *(*unsafe.Pointer)(urv.ptr) // slice rv has a ptr to the slice.
  450. return
  451. }
  452. func rvGetSlice4Array(rv reflect.Value, tslice reflect.Type) (v reflect.Value) {
  453. uv := (*unsafeReflectValue)(unsafe.Pointer(&v))
  454. var x []unsafe.Pointer
  455. uv.ptr = unsafe.Pointer(&x)
  456. uv.typ = ((*unsafeIntf)(unsafe.Pointer(&tslice))).word
  457. uv.flag = unsafeFlagIndir | uintptr(reflect.Slice)
  458. s := (*unsafeSlice)(uv.ptr)
  459. s.Data = ((*unsafeReflectValue)(unsafe.Pointer(&rv))).ptr
  460. s.Len = rv.Len()
  461. s.Cap = s.Len
  462. return
  463. }
  464. func rvCopySlice(dest, src reflect.Value) {
  465. t := dest.Type().Elem()
  466. urv := (*unsafeReflectValue)(unsafe.Pointer(&dest))
  467. destPtr := urv.ptr
  468. urv = (*unsafeReflectValue)(unsafe.Pointer(&src))
  469. typedslicecopy((*unsafeIntf)(unsafe.Pointer(&t)).word,
  470. *(*unsafeSlice)(destPtr), *(*unsafeSlice)(urv.ptr))
  471. }
  472. // ------------
  473. func rvGetBool(rv reflect.Value) bool {
  474. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  475. return *(*bool)(v.ptr)
  476. }
  477. func rvGetBytes(rv reflect.Value) []byte {
  478. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  479. return *(*[]byte)(v.ptr)
  480. }
  481. func rvGetTime(rv reflect.Value) time.Time {
  482. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  483. return *(*time.Time)(v.ptr)
  484. }
  485. func rvGetString(rv reflect.Value) string {
  486. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  487. return *(*string)(v.ptr)
  488. }
  489. func rvGetFloat64(rv reflect.Value) float64 {
  490. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  491. return *(*float64)(v.ptr)
  492. }
  493. func rvGetFloat32(rv reflect.Value) float32 {
  494. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  495. return *(*float32)(v.ptr)
  496. }
  497. func rvGetInt(rv reflect.Value) int {
  498. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  499. return *(*int)(v.ptr)
  500. }
  501. func rvGetInt8(rv reflect.Value) int8 {
  502. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  503. return *(*int8)(v.ptr)
  504. }
  505. func rvGetInt16(rv reflect.Value) int16 {
  506. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  507. return *(*int16)(v.ptr)
  508. }
  509. func rvGetInt32(rv reflect.Value) int32 {
  510. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  511. return *(*int32)(v.ptr)
  512. }
  513. func rvGetInt64(rv reflect.Value) int64 {
  514. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  515. return *(*int64)(v.ptr)
  516. }
  517. func rvGetUint(rv reflect.Value) uint {
  518. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  519. return *(*uint)(v.ptr)
  520. }
  521. func rvGetUint8(rv reflect.Value) uint8 {
  522. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  523. return *(*uint8)(v.ptr)
  524. }
  525. func rvGetUint16(rv reflect.Value) uint16 {
  526. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  527. return *(*uint16)(v.ptr)
  528. }
  529. func rvGetUint32(rv reflect.Value) uint32 {
  530. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  531. return *(*uint32)(v.ptr)
  532. }
  533. func rvGetUint64(rv reflect.Value) uint64 {
  534. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  535. return *(*uint64)(v.ptr)
  536. }
  537. func rvGetUintptr(rv reflect.Value) uintptr {
  538. v := (*unsafeReflectValue)(unsafe.Pointer(&rv))
  539. return *(*uintptr)(v.ptr)
  540. }
  541. // ------------ map range and map indexing ----------
  542. // regular calls to map via reflection: MapKeys, MapIndex, MapRange/MapIter etc
  543. // will always allocate for each map key or value.
  544. //
  545. // It is more performant to provide a value that the map entry is set into,
  546. // and that elides the allocation.
  547. // unsafeMapHashIter
  548. //
  549. // go 1.4+ has runtime/hashmap.go or runtime/map.go which has a
  550. // hIter struct with the first 2 values being key and value
  551. // of the current iteration.
  552. //
  553. // This *hIter is passed to mapiterinit, mapiternext, mapiterkey, mapiterelem.
  554. // We bypass the reflect wrapper functions and just use the *hIter directly.
  555. //
  556. // Though *hIter has many fields, we only care about the first 2.
  557. type unsafeMapHashIter struct {
  558. key, value unsafe.Pointer
  559. // other fields are ignored
  560. }
  561. type mapIter struct {
  562. unsafeMapIter
  563. }
  564. type unsafeMapIter struct {
  565. it *unsafeMapHashIter
  566. // k, v reflect.Value
  567. mtyp, ktyp, vtyp unsafe.Pointer
  568. mptr, kptr, vptr unsafe.Pointer
  569. kisref, visref bool
  570. mapvalues bool
  571. done bool
  572. started bool
  573. // _ [2]uint64 // padding (cache-aligned)
  574. }
  575. func (t *unsafeMapIter) ValidKV() (r bool) {
  576. return false
  577. }
  578. func (t *unsafeMapIter) Next() (r bool) {
  579. if t == nil || t.done {
  580. return
  581. }
  582. if t.started {
  583. mapiternext((unsafe.Pointer)(t.it))
  584. } else {
  585. t.started = true
  586. }
  587. t.done = t.it.key == nil
  588. if t.done {
  589. return
  590. }
  591. unsafeMapSet(t.kptr, t.ktyp, t.it.key, t.kisref)
  592. if t.mapvalues {
  593. unsafeMapSet(t.vptr, t.vtyp, t.it.value, t.visref)
  594. }
  595. return true
  596. }
  597. func (t *unsafeMapIter) Key() (r reflect.Value) {
  598. return
  599. }
  600. func (t *unsafeMapIter) Value() (r reflect.Value) {
  601. return
  602. }
  603. func (t *unsafeMapIter) Done() {
  604. }
  605. func unsafeMapSet(p, ptyp, p2 unsafe.Pointer, isref bool) {
  606. if isref {
  607. *(*unsafe.Pointer)(p) = *(*unsafe.Pointer)(p2) // p2
  608. } else {
  609. typedmemmove(ptyp, p, p2) // *(*unsafe.Pointer)(p2)) // p2)
  610. }
  611. }
  612. func unsafeMapKVPtr(urv *unsafeReflectValue) unsafe.Pointer {
  613. if urv.flag&unsafeFlagIndir == 0 {
  614. return unsafe.Pointer(&urv.ptr)
  615. }
  616. return urv.ptr
  617. }
  618. func mapRange(t *mapIter, m, k, v reflect.Value, mapvalues bool) {
  619. if rvIsNil(m) {
  620. t.done = true
  621. return
  622. }
  623. t.done = false
  624. t.started = false
  625. t.mapvalues = mapvalues
  626. var urv *unsafeReflectValue
  627. urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
  628. t.mtyp = urv.typ
  629. t.mptr = rv2ptr(urv)
  630. t.it = (*unsafeMapHashIter)(mapiterinit(t.mtyp, t.mptr))
  631. urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
  632. t.ktyp = urv.typ
  633. t.kptr = urv.ptr
  634. t.kisref = refBitset.isset(byte(k.Kind()))
  635. if mapvalues {
  636. urv = (*unsafeReflectValue)(unsafe.Pointer(&v))
  637. t.vtyp = urv.typ
  638. t.vptr = urv.ptr
  639. t.visref = refBitset.isset(byte(v.Kind()))
  640. } else {
  641. t.vtyp = nil
  642. t.vptr = nil
  643. }
  644. }
  645. func mapGet(m, k, v reflect.Value) (vv reflect.Value) {
  646. var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
  647. var kptr = unsafeMapKVPtr(urv)
  648. urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
  649. vvptr := mapaccess(urv.typ, rv2ptr(urv), kptr)
  650. if vvptr == nil {
  651. return
  652. }
  653. // vvptr = *(*unsafe.Pointer)(vvptr)
  654. urv = (*unsafeReflectValue)(unsafe.Pointer(&v))
  655. unsafeMapSet(urv.ptr, urv.typ, vvptr, refBitset.isset(byte(v.Kind())))
  656. return v
  657. }
  658. func mapSet(m, k, v reflect.Value) {
  659. var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
  660. var kptr = unsafeMapKVPtr(urv)
  661. urv = (*unsafeReflectValue)(unsafe.Pointer(&v))
  662. var vptr = unsafeMapKVPtr(urv)
  663. urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
  664. mapassign(urv.typ, rv2ptr(urv), kptr, vptr)
  665. }
  666. // func mapDelete(m, k reflect.Value) {
  667. // var urv = (*unsafeReflectValue)(unsafe.Pointer(&k))
  668. // var kptr = unsafeMapKVPtr(urv)
  669. // urv = (*unsafeReflectValue)(unsafe.Pointer(&m))
  670. // mapdelete(urv.typ, rv2ptr(urv), kptr)
  671. // }
  672. // return an addressable reflect value that can be used in mapRange and mapGet operations.
  673. //
  674. // all calls to mapGet or mapRange will call here to get an addressable reflect.Value.
  675. func mapAddressableRV(t reflect.Type, k reflect.Kind) (r reflect.Value) {
  676. // return reflect.New(t).Elem()
  677. return rvZeroAddrK(t, k)
  678. }
  679. //go:linkname mapiterinit reflect.mapiterinit
  680. //go:noescape
  681. func mapiterinit(typ unsafe.Pointer, it unsafe.Pointer) (key unsafe.Pointer)
  682. //go:linkname mapiternext reflect.mapiternext
  683. //go:noescape
  684. func mapiternext(it unsafe.Pointer) (key unsafe.Pointer)
  685. //go:linkname mapaccess reflect.mapaccess
  686. //go:noescape
  687. func mapaccess(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer) (val unsafe.Pointer)
  688. //go:linkname mapassign reflect.mapassign
  689. //go:noescape
  690. func mapassign(typ unsafe.Pointer, m unsafe.Pointer, key, val unsafe.Pointer)
  691. //go:linkname mapdelete reflect.mapdelete
  692. //go:noescape
  693. func mapdelete(typ unsafe.Pointer, m unsafe.Pointer, key unsafe.Pointer)
  694. //go:linkname typedmemmove reflect.typedmemmove
  695. //go:noescape
  696. func typedmemmove(typ unsafe.Pointer, dst, src unsafe.Pointer)
  697. //go:linkname unsafe_New reflect.unsafe_New
  698. //go:noescape
  699. func unsafe_New(typ unsafe.Pointer) unsafe.Pointer
  700. //go:linkname typedslicecopy reflect.typedslicecopy
  701. //go:noescape
  702. func typedslicecopy(elemType unsafe.Pointer, dst, src unsafeSlice) int
  703. // ---------- ENCODER optimized ---------------
  704. func (e *Encoder) jsondriver() *jsonEncDriver {
  705. return (*jsonEncDriver)((*unsafeIntf)(unsafe.Pointer(&e.e)).word)
  706. }
  707. // ---------- DECODER optimized ---------------
  708. func (d *Decoder) checkBreak() bool {
  709. // jsonDecDriver.CheckBreak() CANNOT be inlined.
  710. // Consequently, there's no benefit in incurring the cost of this
  711. // wrapping function checkBreak.
  712. //
  713. // It is faster to just call the interface method directly.
  714. // if d.js {
  715. // return d.jsondriver().CheckBreak()
  716. // }
  717. // if d.cbor {
  718. // return d.cbordriver().CheckBreak()
  719. // }
  720. return d.d.CheckBreak()
  721. }
  722. func (d *Decoder) jsondriver() *jsonDecDriver {
  723. return (*jsonDecDriver)((*unsafeIntf)(unsafe.Pointer(&d.d)).word)
  724. }
  725. // func (d *Decoder) cbordriver() *cborDecDriver {
  726. // return (*cborDecDriver)((*unsafeIntf)(unsafe.Pointer(&d.d)).word)
  727. // }