fast-path.go.tmpl 15 KB

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