fast-path.go.tmpl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // +build !notfastpath
  2. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. // Code generated from fast-path.go.tmpl - DO NOT EDIT.
  5. package codec
  6. // Fast path functions try to create a fast path encode or decode implementation
  7. // for common maps and slices.
  8. //
  9. // We define the functions and register then in this single file
  10. // so as not to pollute the encode.go and decode.go, and create a dependency in there.
  11. // This file can be omitted without causing a build failure.
  12. //
  13. // The advantage of fast paths is:
  14. // - Many calls bypass reflection altogether
  15. //
  16. // Currently support
  17. // - slice of all builtin types,
  18. // - map of all builtin types to string or interface value
  19. // - symmetrical maps of all builtin types (e.g. str-str, uint8-uint8)
  20. // This should provide adequate "typical" implementations.
  21. //
  22. // Note that fast track decode functions must handle values for which an address cannot be obtained.
  23. // For example:
  24. // m2 := map[string]int{}
  25. // p2 := []interface{}{m2}
  26. // // decoding into p2 will bomb if fast track functions do not treat like unaddressable.
  27. //
  28. import (
  29. "reflect"
  30. "sort"
  31. )
  32. const fastpathEnabled = true
  33. type fastpathT struct {}
  34. var fastpathTV fastpathT
  35. type fastpathE struct {
  36. rtid uintptr
  37. rt reflect.Type
  38. encfn func(*Encoder, *codecFnInfo, reflect.Value)
  39. decfn func(*Decoder, *codecFnInfo, reflect.Value)
  40. }
  41. type fastpathA [{{ .FastpathLen }}]fastpathE
  42. func (x *fastpathA) index(rtid uintptr) int {
  43. // use binary search to grab the index (adapted from sort/search.go)
  44. h, i, j := 0, 0, {{ .FastpathLen }} // len(x)
  45. for i < j {
  46. h = i + (j-i)/2
  47. if x[h].rtid < rtid {
  48. i = h + 1
  49. } else {
  50. j = h
  51. }
  52. }
  53. if i < {{ .FastpathLen }} && x[i].rtid == rtid {
  54. return i
  55. }
  56. return -1
  57. }
  58. type fastpathAslice []fastpathE
  59. func (x fastpathAslice) Len() int { return len(x) }
  60. func (x fastpathAslice) Less(i, j int) bool { return x[i].rtid < x[j].rtid }
  61. func (x fastpathAslice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  62. var fastpathAV fastpathA
  63. // due to possible initialization loop error, make fastpath in an init()
  64. func init() {
  65. i := 0
  66. fn := func(v interface{},
  67. fe func(*Encoder, *codecFnInfo, reflect.Value),
  68. fd func(*Decoder, *codecFnInfo, reflect.Value)) (f fastpathE) {
  69. xrt := reflect.TypeOf(v)
  70. xptr := rt2id(xrt)
  71. fastpathAV[i] = fastpathE{xptr, xrt, fe, fd}
  72. i++
  73. return
  74. }
  75. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  76. fn([]{{ .Elem }}(nil), (*Encoder).{{ .MethodNamePfx "fastpathEnc" false }}R, (*Decoder).{{ .MethodNamePfx "fastpathDec" false }}R){{end}}{{end}}{{end}}
  77. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  78. fn(map[{{ .MapKey }}]{{ .Elem }}(nil), (*Encoder).{{ .MethodNamePfx "fastpathEnc" false }}R, (*Decoder).{{ .MethodNamePfx "fastpathDec" false }}R){{end}}{{end}}{{end}}
  79. sort.Sort(fastpathAslice(fastpathAV[:]))
  80. }
  81. // -- encode
  82. // -- -- fast path type switch
  83. func fastpathEncodeTypeSwitch(iv interface{}, e *Encoder) bool {
  84. switch v := iv.(type) {
  85. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  86. case []{{ .Elem }}:{{else}}
  87. case map[{{ .MapKey }}]{{ .Elem }}:{{end}}
  88. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e){{if not .MapKey }}
  89. case *[]{{ .Elem }}:{{else}}
  90. case *map[{{ .MapKey }}]{{ .Elem }}:{{end}}
  91. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e)
  92. {{end}}{{end}}
  93. default:
  94. _ = v // TODO: workaround https://github.com/golang/go/issues/12927 (remove after go 1.6 release)
  95. return false
  96. }
  97. return true
  98. }
  99. {{/* **** removing this block, as they are never called directly ****
  100. func fastpathEncodeTypeSwitchSlice(iv interface{}, e *Encoder) bool {
  101. switch v := iv.(type) {
  102. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  103. case []{{ .Elem }}:
  104. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
  105. case *[]{{ .Elem }}:
  106. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e)
  107. {{end}}{{end}}{{end}}
  108. default:
  109. _ = v // TODO: workaround https://github.com/golang/go/issues/12927 (remove after go 1.6 release)
  110. return false
  111. }
  112. return true
  113. }
  114. func fastpathEncodeTypeSwitchMap(iv interface{}, e *Encoder) bool {
  115. switch v := iv.(type) {
  116. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  117. case map[{{ .MapKey }}]{{ .Elem }}:
  118. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
  119. case *map[{{ .MapKey }}]{{ .Elem }}:
  120. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e)
  121. {{end}}{{end}}{{end}}
  122. default:
  123. _ = v // TODO: workaround https://github.com/golang/go/issues/12927 (remove after go 1.6 release)
  124. return false
  125. }
  126. return true
  127. }
  128. */}}
  129. // -- -- fast path functions
  130. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  131. func (e *Encoder) {{ .MethodNamePfx "fastpathEnc" false }}R(f *codecFnInfo, rv reflect.Value) {
  132. if f.ti.mbs {
  133. fastpathTV.{{ .MethodNamePfx "EncAsMap" false }}V(rv2i(rv).([]{{ .Elem }}), e)
  134. } else {
  135. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(rv2i(rv).([]{{ .Elem }}), e)
  136. }
  137. }
  138. func (_ fastpathT) {{ .MethodNamePfx "Enc" false }}V(v []{{ .Elem }}, e *Encoder) {
  139. if v == nil { e.e.EncodeNil(); return }
  140. ee, esep := e.e, e.hh.hasElemSeparators()
  141. ee.WriteArrayStart(len(v))
  142. for _, v2 := range v {
  143. if esep { ee.WriteArrayElem() }
  144. {{ encmd .Elem "v2"}}
  145. }
  146. ee.WriteArrayEnd()
  147. }
  148. func (_ fastpathT) {{ .MethodNamePfx "EncAsMap" false }}V(v []{{ .Elem }}, e *Encoder) {
  149. ee, esep := e.e, e.hh.hasElemSeparators()
  150. if len(v)%2 == 1 {
  151. e.errorf("mapBySlice requires even slice length, but got %v", len(v))
  152. return
  153. }
  154. ee.WriteMapStart(len(v) / 2)
  155. for j, v2 := range v {
  156. if esep {
  157. if j%2 == 0 {
  158. ee.WriteMapElemKey()
  159. } else {
  160. ee.WriteMapElemValue()
  161. }
  162. }
  163. {{ encmd .Elem "v2"}}
  164. }
  165. ee.WriteMapEnd()
  166. }
  167. {{end}}{{end}}{{end}}
  168. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  169. func (e *Encoder) {{ .MethodNamePfx "fastpathEnc" false }}R(f *codecFnInfo, rv reflect.Value) {
  170. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(rv2i(rv).(map[{{ .MapKey }}]{{ .Elem }}), e)
  171. }
  172. func (_ fastpathT) {{ .MethodNamePfx "Enc" false }}V(v map[{{ .MapKey }}]{{ .Elem }}, e *Encoder) {
  173. if v == nil { e.e.EncodeNil(); return }
  174. ee, esep := e.e, e.hh.hasElemSeparators()
  175. ee.WriteMapStart(len(v))
  176. {{if eq .MapKey "string"}}asSymbols := e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0
  177. {{end}}if e.h.Canonical {
  178. {{if eq .MapKey "interface{}"}}{{/* out of band
  179. */}}var mksv []byte = make([]byte, 0, len(v)*16) // temporary byte slice for the encoding
  180. e2 := NewEncoderBytes(&mksv, e.hh)
  181. v2 := make([]bytesI, len(v))
  182. var i, l int
  183. var vp *bytesI {{/* put loop variables outside. seems currently needed for better perf */}}
  184. for k2, _ := range v {
  185. l = len(mksv)
  186. e2.MustEncode(k2)
  187. vp = &v2[i]
  188. vp.v = mksv[l:]
  189. vp.i = k2
  190. i++
  191. }
  192. sort.Sort(bytesISlice(v2))
  193. for j := range v2 {
  194. if esep { ee.WriteMapElemKey() }
  195. e.asis(v2[j].v)
  196. if esep { ee.WriteMapElemValue() }
  197. e.encode(v[v2[j].i])
  198. } {{else}}{{ $x := sorttype .MapKey true}}v2 := make([]{{ $x }}, len(v))
  199. var i int
  200. for k, _ := range v {
  201. v2[i] = {{ $x }}(k)
  202. i++
  203. }
  204. sort.Sort({{ sorttype .MapKey false}}(v2))
  205. for _, k2 := range v2 {
  206. if esep { ee.WriteMapElemKey() }
  207. {{if eq .MapKey "string"}}if asSymbols {
  208. ee.EncodeSymbol(k2)
  209. } else {
  210. ee.EncodeString(c_UTF8, k2)
  211. }{{else}}{{ $y := printf "%s(k2)" .MapKey }}{{ encmd .MapKey $y }}{{end}}
  212. if esep { ee.WriteMapElemValue() }
  213. {{ $y := printf "v[%s(k2)]" .MapKey }}{{ encmd .Elem $y }}
  214. } {{end}}
  215. } else {
  216. for k2, v2 := range v {
  217. if esep { ee.WriteMapElemKey() }
  218. {{if eq .MapKey "string"}}if asSymbols {
  219. ee.EncodeSymbol(k2)
  220. } else {
  221. ee.EncodeString(c_UTF8, k2)
  222. }{{else}}{{ encmd .MapKey "k2"}}{{end}}
  223. if esep { ee.WriteMapElemValue() }
  224. {{ encmd .Elem "v2"}}
  225. }
  226. }
  227. ee.WriteMapEnd()
  228. }
  229. {{end}}{{end}}{{end}}
  230. // -- decode
  231. // -- -- fast path type switch
  232. func fastpathDecodeTypeSwitch(iv interface{}, d *Decoder) bool {
  233. switch v := iv.(type) {
  234. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  235. case []{{ .Elem }}:{{else}}
  236. case map[{{ .MapKey }}]{{ .Elem }}:{{end}}
  237. fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, false, d){{if not .MapKey }}
  238. case *[]{{ .Elem }}: {{else}}
  239. case *map[{{ .MapKey }}]{{ .Elem }}: {{end}}
  240. if v2, changed2 := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*v, true, d); changed2 {
  241. *v = v2
  242. }
  243. {{end}}{{end}}
  244. default:
  245. _ = v // TODO: workaround https://github.com/golang/go/issues/12927 (remove after go 1.6 release)
  246. return false
  247. }
  248. return true
  249. }
  250. func fastpathDecodeSetZeroTypeSwitch(iv interface{}) bool {
  251. switch v := iv.(type) {
  252. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  253. case *[]{{ .Elem }}: {{else}}
  254. case *map[{{ .MapKey }}]{{ .Elem }}: {{end}}
  255. *v = nil
  256. {{end}}{{end}}
  257. default:
  258. _ = v // TODO: workaround https://github.com/golang/go/issues/12927 (remove after go 1.6 release)
  259. return false
  260. }
  261. return true
  262. }
  263. // -- -- fast path functions
  264. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  265. {{/*
  266. Slices can change if they
  267. - did not come from an array
  268. - are addressable (from a ptr)
  269. - are settable (e.g. contained in an interface{})
  270. */}}
  271. func (d *Decoder) {{ .MethodNamePfx "fastpathDec" false }}R(f *codecFnInfo, rv reflect.Value) {
  272. if array := f.seq == seqTypeArray; !array && rv.Kind() == reflect.Ptr {
  273. var vp = rv2i(rv).(*[]{{ .Elem }})
  274. if v, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*vp, !array, d); changed {
  275. *vp = v
  276. }
  277. } else {
  278. fastpathTV.{{ .MethodNamePfx "Dec" false }}V(rv2i(rv).([]{{ .Elem }}), !array, d)
  279. }
  280. }
  281. func (f fastpathT) {{ .MethodNamePfx "Dec" false }}X(vp *[]{{ .Elem }}, d *Decoder) {
  282. if v, changed := f.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d); changed {
  283. *vp = v
  284. }
  285. }
  286. func (_ fastpathT) {{ .MethodNamePfx "Dec" false }}V(v []{{ .Elem }}, canChange bool, d *Decoder) (_ []{{ .Elem }}, changed bool) {
  287. dd := d.d
  288. {{/* // if dd.isContainerType(valueTypeNil) { dd.TryDecodeAsNil() */}}
  289. slh, containerLenS := d.decSliceHelperStart()
  290. if containerLenS == 0 {
  291. if canChange {
  292. if v == nil {
  293. v = []{{ .Elem }}{}
  294. } else if len(v) != 0 {
  295. v = v[:0]
  296. }
  297. changed = true
  298. }
  299. slh.End()
  300. return v, changed
  301. }
  302. hasLen := containerLenS > 0
  303. var xlen int
  304. if hasLen && canChange {
  305. if containerLenS > cap(v) {
  306. xlen = decInferLen(containerLenS, d.h.MaxInitLen, {{ .Size }})
  307. if xlen <= cap(v) {
  308. v = v[:xlen]
  309. } else {
  310. v = make([]{{ .Elem }}, xlen)
  311. }
  312. changed = true
  313. } else if containerLenS != len(v) {
  314. v = v[:containerLenS]
  315. changed = true
  316. }
  317. }
  318. j := 0
  319. for ; (hasLen && j < containerLenS) || !(hasLen || dd.CheckBreak()); j++ {
  320. if j == 0 && len(v) == 0 {
  321. if hasLen {
  322. xlen = decInferLen(containerLenS, d.h.MaxInitLen, {{ .Size }})
  323. } else {
  324. xlen = 8
  325. }
  326. v = make([]{{ .Elem }}, xlen)
  327. changed = true
  328. }
  329. // if indefinite, etc, then expand the slice if necessary
  330. var decodeIntoBlank bool
  331. if j >= len(v) {
  332. if canChange {
  333. v = append(v, {{ zerocmd .Elem }})
  334. changed = true
  335. } else {
  336. d.arrayCannotExpand(len(v), j+1)
  337. decodeIntoBlank = true
  338. }
  339. }
  340. slh.ElemContainerState(j)
  341. if decodeIntoBlank {
  342. d.swallow()
  343. } else if dd.TryDecodeAsNil() {
  344. v[j] = {{ zerocmd .Elem }}
  345. } else {
  346. {{ if eq .Elem "interface{}" }}d.decode(&v[j]){{ else }}v[j] = {{ decmd .Elem }}{{ end }}
  347. }
  348. }
  349. if canChange {
  350. if j < len(v) {
  351. v = v[:j]
  352. changed = true
  353. } else if j == 0 && v == nil {
  354. v = make([]{{ .Elem }}, 0)
  355. changed = true
  356. }
  357. }
  358. slh.End()
  359. return v, changed
  360. }
  361. {{end}}{{end}}{{end}}
  362. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  363. {{/*
  364. Maps can change if they are
  365. - addressable (from a ptr)
  366. - settable (e.g. contained in an interface{})
  367. */}}
  368. func (d *Decoder) {{ .MethodNamePfx "fastpathDec" false }}R(f *codecFnInfo, rv reflect.Value) {
  369. if rv.Kind() == reflect.Ptr {
  370. vp := rv2i(rv).(*map[{{ .MapKey }}]{{ .Elem }})
  371. if v, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d); changed {
  372. *vp = v
  373. }
  374. return
  375. }
  376. fastpathTV.{{ .MethodNamePfx "Dec" false }}V(rv2i(rv).(map[{{ .MapKey }}]{{ .Elem }}), false, d)
  377. }
  378. func (f fastpathT) {{ .MethodNamePfx "Dec" false }}X(vp *map[{{ .MapKey }}]{{ .Elem }}, d *Decoder) {
  379. if v, changed := f.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d); changed {
  380. *vp = v
  381. }
  382. }
  383. func (_ fastpathT) {{ .MethodNamePfx "Dec" false }}V(v map[{{ .MapKey }}]{{ .Elem }}, canChange bool,
  384. d *Decoder) (_ map[{{ .MapKey }}]{{ .Elem }}, changed bool) {
  385. dd, esep := d.d, d.hh.hasElemSeparators()
  386. {{/* // if dd.isContainerType(valueTypeNil) {dd.TryDecodeAsNil() */}}
  387. containerLen := dd.ReadMapStart()
  388. if canChange && v == nil {
  389. xlen := decInferLen(containerLen, d.h.MaxInitLen, {{ .Size }})
  390. v = make(map[{{ .MapKey }}]{{ .Elem }}, xlen)
  391. changed = true
  392. }
  393. if containerLen == 0 {
  394. dd.ReadMapEnd()
  395. return v, changed
  396. }
  397. {{ if eq .Elem "interface{}" }}mapGet := !d.h.MapValueReset && !d.h.InterfaceReset{{end}}
  398. var mk {{ .MapKey }}
  399. var mv {{ .Elem }}
  400. hasLen := containerLen > 0
  401. for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ {
  402. if esep { dd.ReadMapElemKey() }
  403. {{ if eq .MapKey "interface{}" }}mk = nil
  404. d.decode(&mk)
  405. if bv, bok := mk.([]byte); bok {
  406. mk = d.string(bv) {{/* // maps cannot have []byte as key. switch to string. */}}
  407. }{{ else }}mk = {{ decmd .MapKey }}{{ end }}
  408. if esep { dd.ReadMapElemValue() }
  409. if dd.TryDecodeAsNil() {
  410. if d.h.DeleteOnNilMapValue { delete(v, mk) } else { v[mk] = {{ zerocmd .Elem }} }
  411. continue
  412. }
  413. {{ if eq .Elem "interface{}" }}if mapGet { mv = v[mk] } else { mv = nil }
  414. d.decode(&mv){{ else }}mv = {{ decmd .Elem }}{{ end }}
  415. if v != nil {
  416. v[mk] = mv
  417. }
  418. }
  419. dd.ReadMapEnd()
  420. return v, changed
  421. }
  422. {{end}}{{end}}{{end}}