fast-path.go.tmpl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // //+build ignore
  2. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  4. // ************************************************************
  5. // DO NOT EDIT.
  6. // THIS FILE IS AUTO-GENERATED from fast-path.go.tmpl
  7. // ************************************************************
  8. package codec
  9. // Fast path functions try to create a fast path encode or decode implementation
  10. // for common maps and slices.
  11. //
  12. // We define the functions and register then in this single file
  13. // so as not to pollute the encode.go and decode.go, and create a dependency in there.
  14. // This file can be omitted without causing a build failure.
  15. //
  16. // The advantage of fast paths is:
  17. // - Many calls bypass reflection altogether
  18. //
  19. // Currently support
  20. // - slice of all builtin types,
  21. // - map of all builtin types to string or interface value
  22. // - symetrical maps of all builtin types (e.g. str-str, uint8-uint8)
  23. // This should provide adequate "typical" implementations.
  24. //
  25. // Note that fast track decode functions must handle values for which an address cannot be obtained.
  26. // For example:
  27. // m2 := map[string]int{}
  28. // p2 := []interface{}{m2}
  29. // // decoding into p2 will bomb if fast track functions do not treat like unaddressable.
  30. //
  31. import (
  32. "reflect"
  33. "sort"
  34. )
  35. const fastpathCheckNilFalse = false // for reflect
  36. const fastpathCheckNilTrue = true // for type switch
  37. type fastpathT struct {}
  38. var fastpathTV fastpathT
  39. type fastpathE struct {
  40. rtid uintptr
  41. rt reflect.Type
  42. encfn func(encFnInfo, reflect.Value)
  43. decfn func(decFnInfo, reflect.Value)
  44. }
  45. type fastpathA [{{ .FastpathLen }}]fastpathE
  46. func (x *fastpathA) index(rtid uintptr) int {
  47. // use binary search to grab the index (adapted from sort/search.go)
  48. h, i, j := 0, 0, {{ .FastpathLen }} // len(x)
  49. for i < j {
  50. h = i + (j-i)/2
  51. if x[h].rtid < rtid {
  52. i = h + 1
  53. } else {
  54. j = h
  55. }
  56. }
  57. if i < {{ .FastpathLen }} && x[i].rtid == rtid {
  58. return i
  59. }
  60. return -1
  61. }
  62. type fastpathAslice []fastpathE
  63. func (x fastpathAslice) Len() int { return len(x) }
  64. func (x fastpathAslice) Less(i, j int) bool { return x[i].rtid < x[j].rtid }
  65. func (x fastpathAslice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  66. var fastpathAV fastpathA
  67. // due to possible initialization loop error, make fastpath in an init()
  68. func init() {
  69. if !fastpathEnabled {
  70. return
  71. }
  72. i := 0
  73. fn := func(v interface{}, fe func(encFnInfo, reflect.Value), fd func(decFnInfo, reflect.Value)) (f fastpathE) {
  74. xrt := reflect.TypeOf(v)
  75. xptr := reflect.ValueOf(xrt).Pointer()
  76. fastpathAV[i] = fastpathE{xptr, xrt, fe, fd}
  77. i++
  78. return
  79. }
  80. {{range .Values}}{{if not .Primitive}}{{if .Slice }}
  81. fn([]{{ .Elem }}(nil), (encFnInfo).{{ .MethodNamePfx "fastpathEnc" false }}R, (decFnInfo).{{ .MethodNamePfx "fastpathDec" false }}R){{end}}{{end}}{{end}}
  82. {{range .Values}}{{if not .Primitive}}{{if not .Slice }}
  83. fn(map[{{ .MapKey }}]{{ .Elem }}(nil), (encFnInfo).{{ .MethodNamePfx "fastpathEnc" false }}R, (decFnInfo).{{ .MethodNamePfx "fastpathDec" false }}R){{end}}{{end}}{{end}}
  84. sort.Sort(fastpathAslice(fastpathAV[:]))
  85. }
  86. // -- encode
  87. // -- -- fast path type switch
  88. func fastpathEncodeTypeSwitch(iv interface{}, e *Encoder) bool {
  89. switch v := iv.(type) {
  90. {{range .Values}}{{if not .Primitive}}{{if .Slice }}
  91. case []{{ .Elem }}:{{else}}
  92. case map[{{ .MapKey }}]{{ .Elem }}:{{end}}
  93. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, fastpathCheckNilTrue, e){{if .Slice }}
  94. case *[]{{ .Elem }}:{{else}}
  95. case *map[{{ .MapKey }}]{{ .Elem }}:{{end}}
  96. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, fastpathCheckNilTrue, e)
  97. {{end}}{{end}}
  98. default:
  99. return false
  100. }
  101. return true
  102. }
  103. // -- -- fast path functions
  104. {{range .Values}}{{if not .Primitive}}{{if .Slice }}
  105. func (f encFnInfo) {{ .MethodNamePfx "fastpathEnc" false }}R(rv reflect.Value) {
  106. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(rv.Interface().([]{{ .Elem }}), fastpathCheckNilFalse, f.e)
  107. }
  108. func (_ fastpathT) {{ .MethodNamePfx "Enc" false }}V(v []{{ .Elem }}, checkNil bool, e *Encoder) {
  109. ee := e.e
  110. if checkNil && v == nil {
  111. ee.EncodeNil()
  112. return
  113. }
  114. ee.EncodeArrayStart(len(v))
  115. if e.be {
  116. for _, v2 := range v {
  117. {{ encmd .Elem "v2"}}
  118. }
  119. } else {
  120. for j, v2 := range v {
  121. if j > 0 {
  122. ee.EncodeArrayEntrySeparator()
  123. }
  124. {{ encmd .Elem "v2"}}
  125. }
  126. ee.EncodeArrayEnd()
  127. }
  128. }
  129. {{end}}{{end}}{{end}}
  130. {{range .Values}}{{if not .Primitive}}{{if not .Slice }}
  131. func (f encFnInfo) {{ .MethodNamePfx "fastpathEnc" false }}R(rv reflect.Value) {
  132. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(rv.Interface().(map[{{ .MapKey }}]{{ .Elem }}), fastpathCheckNilFalse, f.e)
  133. }
  134. func (_ fastpathT) {{ .MethodNamePfx "Enc" false }}V(v map[{{ .MapKey }}]{{ .Elem }}, checkNil bool, e *Encoder) {
  135. ee := e.e
  136. if checkNil && v == nil {
  137. ee.EncodeNil()
  138. return
  139. }
  140. ee.EncodeMapStart(len(v))
  141. {{if eq .MapKey "string"}}asSymbols := e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0{{end}}
  142. if e.be {
  143. for k2, v2 := range v {
  144. {{if eq .MapKey "string"}}if asSymbols {
  145. ee.EncodeSymbol(k2)
  146. } else {
  147. ee.EncodeString(c_UTF8, k2)
  148. }{{else}}{{ encmd .MapKey "k2"}}{{end}}
  149. {{ encmd .Elem "v2"}}
  150. }
  151. } else {
  152. j := 0
  153. for k2, v2 := range v {
  154. if j > 0 {
  155. ee.EncodeMapEntrySeparator()
  156. }
  157. {{if eq .MapKey "string"}}if asSymbols {
  158. ee.EncodeSymbol(k2)
  159. } else {
  160. ee.EncodeString(c_UTF8, k2)
  161. }{{else}}{{ encmd .MapKey "k2"}}{{end}}
  162. ee.EncodeMapKVSeparator()
  163. {{ encmd .Elem "v2"}}
  164. j++
  165. }
  166. ee.EncodeMapEnd()
  167. }
  168. }
  169. {{end}}{{end}}{{end}}
  170. // -- decode
  171. // -- -- fast path type switch
  172. func fastpathDecodeTypeSwitch(iv interface{}, d *Decoder) bool {
  173. switch v := iv.(type) {
  174. {{range .Values}}{{if not .Primitive}}{{if .Slice }}
  175. case []{{ .Elem }}:{{else}}
  176. case map[{{ .MapKey }}]{{ .Elem }}:{{end}}
  177. fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, fastpathCheckNilFalse, false, d){{if .Slice }}
  178. case *[]{{ .Elem }}:{{else}}
  179. case *map[{{ .MapKey }}]{{ .Elem }}:{{end}}
  180. v2, changed2 := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*v, fastpathCheckNilFalse, true, d)
  181. if changed2 {
  182. *v = v2
  183. }
  184. {{end}}{{end}}
  185. default:
  186. return false
  187. }
  188. return true
  189. }
  190. // -- -- fast path functions
  191. {{range .Values}}{{if not .Primitive}}{{if .Slice }}
  192. {{/*
  193. Slices can change if they
  194. - did not come from an array
  195. - are addressable (from a ptr)
  196. - are settable (e.g. contained in an interface{})
  197. */}}
  198. func (f decFnInfo) {{ .MethodNamePfx "fastpathDec" false }}R(rv reflect.Value) {
  199. array := f.seq == seqTypeArray
  200. if !array && rv.CanAddr() { // CanSet => CanAddr + Exported
  201. vp := rv.Addr().Interface().(*[]{{ .Elem }})
  202. v, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*vp, fastpathCheckNilFalse, !array, f.d)
  203. if changed {
  204. *vp = v
  205. }
  206. } else {
  207. v := rv.Interface().([]{{ .Elem }})
  208. fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, fastpathCheckNilFalse, false, f.d)
  209. }
  210. }
  211. func (f fastpathT) {{ .MethodNamePfx "Dec" false }}X(vp *[]{{ .Elem }}, checkNil bool, d *Decoder) {
  212. v, changed := f.{{ .MethodNamePfx "Dec" false }}V(*vp, checkNil, true, d)
  213. if changed {
  214. *vp = v
  215. }
  216. }
  217. func (_ fastpathT) {{ .MethodNamePfx "Dec" false }}V(v []{{ .Elem }}, checkNil bool, canChange bool,
  218. d *Decoder) (_ []{{ .Elem }}, changed bool) {
  219. dd := d.d
  220. // if dd.isContainerType(valueTypeNil) { dd.TryDecodeAsNil()
  221. if checkNil && dd.TryDecodeAsNil() {
  222. if v != nil {
  223. changed = true
  224. }
  225. return nil, changed
  226. }
  227. slh, containerLenS := d.decSliceHelperStart()
  228. if canChange && v == nil {
  229. if containerLenS <= 0 {
  230. v = []{{ .Elem }}{}
  231. } else {
  232. v = make([]{{ .Elem }}, containerLenS, containerLenS)
  233. }
  234. changed = true
  235. }
  236. if containerLenS == 0 {
  237. if canChange && len(v) != 0 {
  238. v = v[:0]
  239. changed = true
  240. }{{/*
  241. // slh.End() // dd.ReadArrayEnd()
  242. */}}
  243. return v, changed
  244. }
  245. // for j := 0; j < containerLenS; j++ {
  246. if containerLenS > 0 {
  247. decLen := containerLenS
  248. if containerLenS > cap(v) {
  249. if canChange {
  250. s := make([]{{ .Elem }}, containerLenS, containerLenS)
  251. // copy(s, v[:cap(v)])
  252. v = s
  253. changed = true
  254. } else {
  255. d.arrayCannotExpand(len(v), containerLenS)
  256. decLen = len(v)
  257. }
  258. } else if containerLenS != len(v) {
  259. v = v[:containerLenS]
  260. changed = true
  261. }
  262. // all checks done. cannot go past len.
  263. j := 0
  264. for ; j < decLen; j++ {
  265. {{ if eq .Elem "interface{}" }}d.decode(&v[j]){{ else }}v[j] = {{ decmd .Elem }}{{ end }}
  266. }
  267. if !canChange {
  268. for ; j < containerLenS; j++ {
  269. d.swallow()
  270. }
  271. }
  272. } else {
  273. j := 0
  274. for ; !dd.CheckBreak(); j++ {
  275. if j >= len(v) {
  276. if canChange {
  277. v = append(v, {{ zerocmd .Elem }})
  278. changed = true
  279. } else {
  280. d.arrayCannotExpand(len(v), j+1)
  281. }
  282. }
  283. if j > 0 {
  284. slh.Sep(j)
  285. }
  286. if j < len(v) { // all checks done. cannot go past len.
  287. {{ if eq .Elem "interface{}" }}d.decode(&v[j])
  288. {{ else }}v[j] = {{ decmd .Elem }}{{ end }}
  289. } else {
  290. d.swallow()
  291. }
  292. }
  293. slh.End()
  294. }
  295. return v, changed
  296. }
  297. {{end}}{{end}}{{end}}
  298. {{range .Values}}{{if not .Primitive}}{{if not .Slice }}
  299. {{/*
  300. Maps can change if they are
  301. - addressable (from a ptr)
  302. - settable (e.g. contained in an interface{})
  303. */}}
  304. func (f decFnInfo) {{ .MethodNamePfx "fastpathDec" false }}R(rv reflect.Value) {
  305. if rv.CanAddr() {
  306. vp := rv.Addr().Interface().(*map[{{ .MapKey }}]{{ .Elem }})
  307. v, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*vp, fastpathCheckNilFalse, true, f.d)
  308. if changed {
  309. *vp = v
  310. }
  311. } else {
  312. v := rv.Interface().(map[{{ .MapKey }}]{{ .Elem }})
  313. fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, fastpathCheckNilFalse, false, f.d)
  314. }
  315. }
  316. func (f fastpathT) {{ .MethodNamePfx "Dec" false }}X(vp *map[{{ .MapKey }}]{{ .Elem }}, checkNil bool, d *Decoder) {
  317. v, changed := f.{{ .MethodNamePfx "Dec" false }}V(*vp, checkNil, true, d)
  318. if changed {
  319. *vp = v
  320. }
  321. }
  322. func (_ fastpathT) {{ .MethodNamePfx "Dec" false }}V(v map[{{ .MapKey }}]{{ .Elem }}, checkNil bool, canChange bool,
  323. d *Decoder) (_ map[{{ .MapKey }}]{{ .Elem }}, changed bool) {
  324. dd := d.d
  325. // if dd.isContainerType(valueTypeNil) {dd.TryDecodeAsNil()
  326. if checkNil && dd.TryDecodeAsNil() {
  327. if v != nil {
  328. changed = true
  329. }
  330. return nil, changed
  331. }
  332. containerLen := dd.ReadMapStart()
  333. if canChange && v == nil {
  334. if containerLen > 0 {
  335. v = make(map[{{ .MapKey }}]{{ .Elem }}, containerLen)
  336. } else {
  337. v = make(map[{{ .MapKey }}]{{ .Elem }}) // supports indefinite-length, etc
  338. }
  339. changed = true
  340. }
  341. if containerLen > 0 {
  342. for j := 0; j < containerLen; j++ {
  343. {{ if eq .MapKey "interface{}" }}var mk interface{}
  344. d.decode(&mk)
  345. if bv, bok := mk.([]byte); bok {
  346. mk = string(bv) // maps cannot have []byte as key. switch to string.
  347. }{{ else }}mk := {{ decmd .MapKey }}{{ end }}
  348. mv := v[mk]
  349. {{ if eq .Elem "interface{}" }}d.decode(&mv)
  350. {{ else }}mv = {{ decmd .Elem }}{{ end }}
  351. if v != nil {
  352. v[mk] = mv
  353. }
  354. }
  355. } else if containerLen < 0 {
  356. for j := 0; !dd.CheckBreak(); j++ {
  357. if j > 0 {
  358. dd.ReadMapEntrySeparator()
  359. }
  360. {{ if eq .MapKey "interface{}" }}var mk interface{}
  361. d.decode(&mk)
  362. if bv, bok := mk.([]byte); bok {
  363. mk = string(bv) // maps cannot have []byte as key. switch to string.
  364. }{{ else }}mk := {{ decmd .MapKey }}{{ end }}
  365. dd.ReadMapKVSeparator()
  366. mv := v[mk]
  367. {{ if eq .Elem "interface{}" }}d.decode(&mv)
  368. {{ else }}mv = {{ decmd .Elem }}{{ end }}
  369. if v != nil {
  370. v[mk] = mv
  371. }
  372. }
  373. dd.ReadMapEnd()
  374. }
  375. return v, changed
  376. }
  377. {{end}}{{end}}{{end}}