fast-path.go.tmpl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // +build !notfastpath
  2. // Copyright (c) 2012-2018 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. const fastpathMapBySliceErrMsg = "mapBySlice requires even slice length, but got %v"
  34. type fastpathT struct {}
  35. var fastpathTV fastpathT
  36. type fastpathE struct {
  37. rtid uintptr
  38. rt reflect.Type
  39. encfn func(*Encoder, *codecFnInfo, reflect.Value)
  40. decfn func(*Decoder, *codecFnInfo, reflect.Value)
  41. }
  42. type fastpathA [{{ .FastpathLen }}]fastpathE
  43. func (x *fastpathA) index(rtid uintptr) int {
  44. // use binary search to grab the index (adapted from sort/search.go)
  45. // Note: we use goto (instead of for loop) so this can be inlined.
  46. // h, i, j := 0, 0, len(x)
  47. var h, i uint
  48. var j = uint(len(x))
  49. LOOP:
  50. if i < j {
  51. h = i + (j-i)/2
  52. if x[h].rtid < rtid {
  53. i = h + 1
  54. } else {
  55. j = h
  56. }
  57. goto LOOP
  58. }
  59. if i < uint(len(x)) && x[i].rtid == rtid {
  60. return int(i)
  61. }
  62. return -1
  63. }
  64. type fastpathAslice []fastpathE
  65. func (x fastpathAslice) Len() int { return len(x) }
  66. func (x fastpathAslice) Less(i, j int) bool { return x[uint(i)].rtid < x[uint(j)].rtid }
  67. func (x fastpathAslice) Swap(i, j int) { x[uint(i)], x[uint(j)] = x[uint(j)], x[uint(i)] }
  68. var fastpathAV fastpathA
  69. // due to possible initialization loop error, make fastpath in an init()
  70. func init() {
  71. var i uint = 0
  72. fn := func(v interface{},
  73. fe func(*Encoder, *codecFnInfo, reflect.Value),
  74. fd func(*Decoder, *codecFnInfo, reflect.Value)) {
  75. xrt := reflect.TypeOf(v)
  76. xptr := rt2id(xrt)
  77. fastpathAV[i] = fastpathE{xptr, xrt, fe, fd}
  78. i++
  79. }
  80. {{/* do not register []uint8 in fast-path */}}
  81. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}{{if ne .Elem "uint8"}}
  82. fn([]{{ .Elem }}(nil), (*Encoder).{{ .MethodNamePfx "fastpathEnc" false }}R, (*Decoder).{{ .MethodNamePfx "fastpathDec" false }}R){{end}}{{end}}{{end}}{{end}}
  83. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  84. fn(map[{{ .MapKey }}]{{ .Elem }}(nil), (*Encoder).{{ .MethodNamePfx "fastpathEnc" false }}R, (*Decoder).{{ .MethodNamePfx "fastpathDec" false }}R){{end}}{{end}}{{end}}
  85. sort.Sort(fastpathAslice(fastpathAV[:]))
  86. }
  87. // -- encode
  88. // -- -- fast path type switch
  89. func fastpathEncodeTypeSwitch(iv interface{}, e *Encoder) bool {
  90. switch v := iv.(type) {
  91. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}{{if ne .Elem "uint8"}}
  92. case []{{ .Elem }}:
  93. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
  94. case *[]{{ .Elem }}:
  95. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e){{/*
  96. */}}{{end}}{{end}}{{end}}{{end}}
  97. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  98. case map[{{ .MapKey }}]{{ .Elem }}:
  99. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
  100. case *map[{{ .MapKey }}]{{ .Elem }}:
  101. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e){{/*
  102. */}}{{end}}{{end}}{{end}}
  103. default:
  104. _ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
  105. return false
  106. }
  107. return true
  108. }
  109. {{/*
  110. **** removing this block, as they are never called directly ****
  111. **** removing this block, as they are never called directly ****
  112. func fastpathEncodeTypeSwitchSlice(iv interface{}, e *Encoder) bool {
  113. switch v := iv.(type) {
  114. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  115. case []{{ .Elem }}:
  116. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
  117. case *[]{{ .Elem }}:
  118. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e)
  119. {{end}}{{end}}{{end}}
  120. default:
  121. _ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
  122. return false
  123. }
  124. return true
  125. }
  126. func fastpathEncodeTypeSwitchMap(iv interface{}, e *Encoder) bool {
  127. switch v := iv.(type) {
  128. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  129. case map[{{ .MapKey }}]{{ .Elem }}:
  130. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(v, e)
  131. case *map[{{ .MapKey }}]{{ .Elem }}:
  132. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(*v, e)
  133. {{end}}{{end}}{{end}}
  134. default:
  135. _ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
  136. return false
  137. }
  138. return true
  139. }
  140. **** removing this block, as they are never called directly ****
  141. **** removing this block, as they are never called directly ****
  142. */}}
  143. // -- -- fast path functions
  144. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  145. func (e *Encoder) {{ .MethodNamePfx "fastpathEnc" false }}R(f *codecFnInfo, rv reflect.Value) {
  146. if f.ti.mbs {
  147. fastpathTV.{{ .MethodNamePfx "EncAsMap" false }}V(rv2i(rv).([]{{ .Elem }}), e)
  148. } else {
  149. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(rv2i(rv).([]{{ .Elem }}), e)
  150. }
  151. }
  152. func (_ fastpathT) {{ .MethodNamePfx "Enc" false }}V(v []{{ .Elem }}, e *Encoder) {
  153. if v == nil { e.e.EncodeNil(); return }
  154. ee, esep := e.e, e.hh.hasElemSeparators()
  155. ee.WriteArrayStart(len(v))
  156. for _, v2 := range v {
  157. if esep { ee.WriteArrayElem() }
  158. {{ encmd .Elem "v2"}}
  159. }
  160. ee.WriteArrayEnd()
  161. }
  162. func (_ fastpathT) {{ .MethodNamePfx "EncAsMap" false }}V(v []{{ .Elem }}, e *Encoder) {
  163. ee, esep := e.e, e.hh.hasElemSeparators()
  164. if len(v)%2 == 1 {
  165. e.errorf(fastpathMapBySliceErrMsg, len(v))
  166. return
  167. }
  168. ee.WriteMapStart(len(v) / 2)
  169. for j, v2 := range v {
  170. if esep {
  171. if j%2 == 0 {
  172. ee.WriteMapElemKey()
  173. } else {
  174. ee.WriteMapElemValue()
  175. }
  176. }
  177. {{ encmd .Elem "v2"}}
  178. }
  179. ee.WriteMapEnd()
  180. }
  181. {{end}}{{end}}{{end}}
  182. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  183. func (e *Encoder) {{ .MethodNamePfx "fastpathEnc" false }}R(f *codecFnInfo, rv reflect.Value) {
  184. fastpathTV.{{ .MethodNamePfx "Enc" false }}V(rv2i(rv).(map[{{ .MapKey }}]{{ .Elem }}), e)
  185. }
  186. func (_ fastpathT) {{ .MethodNamePfx "Enc" false }}V(v map[{{ .MapKey }}]{{ .Elem }}, e *Encoder) {
  187. if v == nil { e.e.EncodeNil(); return }
  188. ee, esep := e.e, e.hh.hasElemSeparators()
  189. ee.WriteMapStart(len(v))
  190. if e.h.Canonical {
  191. {{if eq .MapKey "interface{}"}}{{/* out of band
  192. */}}var mksv []byte = make([]byte, 0, len(v)*16) // temporary byte slice for the encoding
  193. e2 := NewEncoderBytes(&mksv, e.hh)
  194. v2 := make([]bytesI, len(v))
  195. var i, l uint
  196. var vp *bytesI {{/* put loop variables outside. seems currently needed for better perf */}}
  197. for k2 := range v {
  198. l = uint(len(mksv))
  199. e2.MustEncode(k2)
  200. vp = &v2[i]
  201. vp.v = mksv[l:]
  202. vp.i = k2
  203. i++
  204. }
  205. sort.Sort(bytesISlice(v2))
  206. for j := range v2 {
  207. if esep { ee.WriteMapElemKey() }
  208. e.asis(v2[j].v)
  209. if esep { ee.WriteMapElemValue() }
  210. e.encode(v[v2[j].i])
  211. } {{else}}{{ $x := sorttype .MapKey true}}v2 := make([]{{ $x }}, len(v))
  212. var i uint
  213. for k := range v {
  214. v2[i] = {{ $x }}(k)
  215. i++
  216. }
  217. sort.Sort({{ sorttype .MapKey false}}(v2))
  218. for _, k2 := range v2 {
  219. if esep { ee.WriteMapElemKey() }
  220. {{if eq .MapKey "string"}} if e.h.StringToRaw {ee.EncodeStringBytesRaw(bytesView(k2))} else {ee.EncodeStringEnc(cUTF8, k2)} {{else}}{{ $y := printf "%s(k2)" .MapKey }}{{ encmd .MapKey $y }}{{end}}
  221. if esep { ee.WriteMapElemValue() }
  222. {{ $y := printf "v[%s(k2)]" .MapKey }}{{ encmd .Elem $y }}
  223. } {{end}}
  224. } else {
  225. for k2, v2 := range v {
  226. if esep { ee.WriteMapElemKey() }
  227. {{if eq .MapKey "string"}} if e.h.StringToRaw {ee.EncodeStringBytesRaw(bytesView(k2))} else {ee.EncodeStringEnc(cUTF8, k2)} {{else}}{{ encmd .MapKey "k2"}}{{end}}
  228. if esep { ee.WriteMapElemValue() }
  229. {{ encmd .Elem "v2"}}
  230. }
  231. }
  232. ee.WriteMapEnd()
  233. }
  234. {{end}}{{end}}{{end}}
  235. // -- decode
  236. // -- -- fast path type switch
  237. func fastpathDecodeTypeSwitch(iv interface{}, d *Decoder) bool {
  238. var changed bool
  239. switch v := iv.(type) {
  240. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}{{if ne .Elem "uint8"}}
  241. case []{{ .Elem }}:
  242. var v2 []{{ .Elem }}
  243. v2, changed = fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, false, d)
  244. if changed && len(v) > 0 && len(v2) > 0 && !(len(v2) == len(v) && &v2[0] == &v[0]) {
  245. copy(v, v2)
  246. }
  247. case *[]{{ .Elem }}:
  248. var v2 []{{ .Elem }}
  249. v2, changed = fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*v, true, d)
  250. if changed {
  251. *v = v2
  252. }{{/*
  253. */}}{{end}}{{end}}{{end}}{{end}}
  254. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}{{/*
  255. // maps only change if nil, and in that case, there's no point copying
  256. */}}
  257. case map[{{ .MapKey }}]{{ .Elem }}:
  258. fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, false, d)
  259. case *map[{{ .MapKey }}]{{ .Elem }}:
  260. var v2 map[{{ .MapKey }}]{{ .Elem }}
  261. v2, changed = fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*v, true, d)
  262. if changed {
  263. *v = v2
  264. }{{/*
  265. */}}{{end}}{{end}}{{end}}
  266. default:
  267. _ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
  268. return false
  269. }
  270. return true
  271. }
  272. func fastpathDecodeSetZeroTypeSwitch(iv interface{}) bool {
  273. switch v := iv.(type) {
  274. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  275. case *[]{{ .Elem }}:
  276. *v = nil {{/*
  277. */}}{{end}}{{end}}{{end}}
  278. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  279. case *map[{{ .MapKey }}]{{ .Elem }}:
  280. *v = nil {{/*
  281. */}}{{end}}{{end}}{{end}}
  282. default:
  283. _ = v // workaround https://github.com/golang/go/issues/12927 seen in go1.4
  284. return false
  285. }
  286. return true
  287. }
  288. // -- -- fast path functions
  289. {{range .Values}}{{if not .Primitive}}{{if not .MapKey }}
  290. {{/*
  291. Slices can change if they
  292. - did not come from an array
  293. - are addressable (from a ptr)
  294. - are settable (e.g. contained in an interface{})
  295. */}}
  296. func (d *Decoder) {{ .MethodNamePfx "fastpathDec" false }}R(f *codecFnInfo, rv reflect.Value) {
  297. if array := f.seq == seqTypeArray; !array && rv.Kind() == reflect.Ptr {
  298. vp := rv2i(rv).(*[]{{ .Elem }})
  299. v, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*vp, !array, d)
  300. if changed { *vp = v }
  301. } else {
  302. v := rv2i(rv).([]{{ .Elem }})
  303. v2, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(v, !array, d)
  304. if changed && len(v) > 0 && len(v2) > 0 && !(len(v2) == len(v) && &v2[0] == &v[0]) {
  305. copy(v, v2)
  306. }
  307. }
  308. }
  309. func (f fastpathT) {{ .MethodNamePfx "Dec" false }}X(vp *[]{{ .Elem }}, d *Decoder) {
  310. v, changed := f.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d)
  311. if changed { *vp = v }
  312. }
  313. func (_ fastpathT) {{ .MethodNamePfx "Dec" false }}V(v []{{ .Elem }}, canChange bool, d *Decoder) (_ []{{ .Elem }}, changed bool) {
  314. dd := d.d{{/*
  315. // if dd.isContainerType(valueTypeNil) { dd.TryDecodeAsNil()
  316. */}}
  317. slh, containerLenS := d.decSliceHelperStart()
  318. if containerLenS == 0 {
  319. if canChange {
  320. if v == nil { v = []{{ .Elem }}{} } else if len(v) != 0 { v = v[:0] }
  321. changed = true
  322. }
  323. slh.End()
  324. return v, changed
  325. }
  326. d.depthIncr()
  327. hasLen := containerLenS > 0
  328. var xlen int
  329. if hasLen && canChange {
  330. if containerLenS > cap(v) {
  331. xlen = decInferLen(containerLenS, d.h.MaxInitLen, {{ .Size }})
  332. if xlen <= cap(v) {
  333. v = v[:uint(xlen)]
  334. } else {
  335. v = make([]{{ .Elem }}, uint(xlen))
  336. }
  337. changed = true
  338. } else if containerLenS != len(v) {
  339. v = v[:containerLenS]
  340. changed = true
  341. }
  342. }
  343. var j int
  344. for j = 0; (hasLen && j < containerLenS) || !(hasLen || dd.CheckBreak()); j++ {
  345. if j == 0 && len(v) == 0 && canChange {
  346. if hasLen {
  347. xlen = decInferLen(containerLenS, d.h.MaxInitLen, {{ .Size }})
  348. } else {
  349. xlen = 8
  350. }
  351. v = make([]{{ .Elem }}, uint(xlen))
  352. changed = true
  353. }
  354. // if indefinite, etc, then expand the slice if necessary
  355. var decodeIntoBlank bool
  356. if j >= len(v) {
  357. if canChange {
  358. v = append(v, {{ zerocmd .Elem }})
  359. changed = true
  360. } else {
  361. d.arrayCannotExpand(len(v), j+1)
  362. decodeIntoBlank = true
  363. }
  364. }
  365. slh.ElemContainerState(j)
  366. if decodeIntoBlank {
  367. d.swallow()
  368. } else if dd.TryDecodeAsNil() {
  369. v[uint(j)] = {{ zerocmd .Elem }}
  370. } else {
  371. {{ if eq .Elem "interface{}" }}d.decode(&v[uint(j)]){{ else }}v[uint(j)] = {{ decmd .Elem }}{{ end }}
  372. }
  373. }
  374. if canChange {
  375. if j < len(v) {
  376. v = v[:uint(j)]
  377. changed = true
  378. } else if j == 0 && v == nil {
  379. v = make([]{{ .Elem }}, 0)
  380. changed = true
  381. }
  382. }
  383. slh.End()
  384. d.depthDecr()
  385. return v, changed
  386. }
  387. {{end}}{{end}}{{end}}
  388. {{range .Values}}{{if not .Primitive}}{{if .MapKey }}
  389. {{/*
  390. Maps can change if they are
  391. - addressable (from a ptr)
  392. - settable (e.g. contained in an interface{})
  393. */}}
  394. func (d *Decoder) {{ .MethodNamePfx "fastpathDec" false }}R(f *codecFnInfo, rv reflect.Value) {
  395. if rv.Kind() == reflect.Ptr {
  396. vp := rv2i(rv).(*map[{{ .MapKey }}]{{ .Elem }})
  397. v, changed := fastpathTV.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d);
  398. if changed { *vp = v }
  399. } else {
  400. fastpathTV.{{ .MethodNamePfx "Dec" false }}V(rv2i(rv).(map[{{ .MapKey }}]{{ .Elem }}), false, d)
  401. }
  402. }
  403. func (f fastpathT) {{ .MethodNamePfx "Dec" false }}X(vp *map[{{ .MapKey }}]{{ .Elem }}, d *Decoder) {
  404. v, changed := f.{{ .MethodNamePfx "Dec" false }}V(*vp, true, d)
  405. if changed { *vp = v }
  406. }
  407. func (_ fastpathT) {{ .MethodNamePfx "Dec" false }}V(v map[{{ .MapKey }}]{{ .Elem }}, canChange bool,
  408. d *Decoder) (_ map[{{ .MapKey }}]{{ .Elem }}, changed bool) {
  409. dd, esep := d.d, d.hh.hasElemSeparators(){{/*
  410. // if dd.isContainerType(valueTypeNil) {dd.TryDecodeAsNil()
  411. */}}
  412. containerLen := dd.ReadMapStart()
  413. if canChange && v == nil {
  414. xlen := decInferLen(containerLen, d.h.MaxInitLen, {{ .Size }})
  415. v = make(map[{{ .MapKey }}]{{ .Elem }}, xlen)
  416. changed = true
  417. }
  418. if containerLen == 0 {
  419. dd.ReadMapEnd()
  420. return v, changed
  421. }
  422. d.depthIncr()
  423. {{ if eq .Elem "interface{}" }}mapGet := v != nil && !d.h.MapValueReset && !d.h.InterfaceReset
  424. {{end}}var mk {{ .MapKey }}
  425. var mv {{ .Elem }}
  426. hasLen := containerLen > 0
  427. for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ {
  428. if esep { dd.ReadMapElemKey() }
  429. {{ if eq .MapKey "interface{}" }}mk = nil
  430. d.decode(&mk)
  431. if bv, bok := mk.([]byte); bok {
  432. mk = d.string(bv) {{/* // maps cannot have []byte as key. switch to string. */}}
  433. }{{ else }}mk = {{ decmd .MapKey }}{{ end }}
  434. if esep { dd.ReadMapElemValue() }
  435. if dd.TryDecodeAsNil() {
  436. if v == nil {} else if d.h.DeleteOnNilMapValue { delete(v, mk) } else { v[mk] = {{ zerocmd .Elem }} }
  437. continue
  438. }
  439. {{ if eq .Elem "interface{}" }}if mapGet { mv = v[mk] } else { mv = nil }
  440. d.decode(&mv){{ else }}mv = {{ decmd .Elem }}{{ end }}
  441. if v != nil { v[mk] = mv }
  442. }
  443. dd.ReadMapEnd()
  444. d.depthDecr()
  445. return v, changed
  446. }
  447. {{end}}{{end}}{{end}}