helper_unsafe.go 21 KB

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