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