gen-fast-path.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. package main
  5. import (
  6. "bytes"
  7. "go/format"
  8. "os"
  9. "strings"
  10. "text/template"
  11. )
  12. // fastpathenabled uses maps to track the uintptr to the function.
  13. // It is shown by experiments that maps scale better than linear search
  14. // when there are more than 32 entries.
  15. const tmplstr = `
  16. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  17. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  18. // ************************************************************
  19. // DO NOT EDIT.
  20. // THIS FILE IS GENERATED BY RUNNING: go run gen-fast-path.go
  21. // ************************************************************
  22. package codec
  23. // Fast path functions try to create a fast path encode or decode implementation
  24. // for common maps and slices.
  25. //
  26. // We define the functions and register then in this single file
  27. // so as not to pollute the encode.go and decode.go, and create a dependency in there.
  28. // This file can be omitted without causing a build failure.
  29. //
  30. // The advantage of fast paths is:
  31. // - Many calls bypass reflection altogether
  32. //
  33. // Currently support
  34. // - slice of all builtin types,
  35. // - map of all builtin types to string or interface value
  36. // - symetrical maps of all builtin types (e.g. str-str, uint8-uint8)
  37. // This should provide adequate "typical" implementations.
  38. //
  39. // Note that fast track decode functions must handle values for which an address cannot be obtained.
  40. // For example:
  41. // m2 := map[string]int{}
  42. // p2 := []interface{}{m2}
  43. // // decoding into p2 will bomb if fast track functions do not treat like unaddressable.
  44. //
  45. import (
  46. "reflect"
  47. )
  48. func init() {
  49. if !fastpathEnabled {
  50. return // basically disable the fast path checks (since accessing empty map is basically free)
  51. }
  52. fx := func(i interface{}, fe func(*encFnInfo, reflect.Value), fd func(*decFnInfo, reflect.Value)) {
  53. xrt := reflect.TypeOf(i)
  54. xptr := reflect.ValueOf(xrt).Pointer()
  55. fastpathsTyp[xptr] = xrt
  56. fastpathsEnc[xptr] = fe
  57. fastpathsDec[xptr] = fd
  58. }
  59. {{range .Values}}{{if .Slice }}
  60. fx([]{{ .Elem }}(nil), (*encFnInfo).{{ .MethodName true }}, (*decFnInfo).{{ .MethodName false}}){{end}}{{end}}
  61. {{range .Values}}{{if not .Slice }}
  62. fx(map[{{ .MapKey }}]{{ .Elem }}(nil), (*encFnInfo).{{ .MethodName true }}, (*decFnInfo).{{ .MethodName false}}){{end}}{{end}}
  63. }
  64. // -- encode
  65. {{range .Values}}{{if .Slice }}
  66. func (f *encFnInfo) {{ .MethodName true }}(rv reflect.Value) {
  67. v := rv.Interface().([]{{ .Elem }})
  68. if v == nil {
  69. f.ee.encodeNil()
  70. return
  71. }
  72. f.ee.encodeArrayStart(len(v))
  73. for j, v2 := range v {
  74. if j > 0 {
  75. f.ee.encodeArrayEntrySeparator()
  76. }
  77. {{ encmd .Elem "v2"}}
  78. }
  79. f.ee.encodeArrayEnd()
  80. }
  81. {{end}}{{end}}
  82. {{range .Values}}{{if not .Slice }}
  83. func (f *encFnInfo) {{ .MethodName true }}(rv reflect.Value) {
  84. v := rv.Interface().(map[{{ .MapKey }}]{{ .Elem }})
  85. if v == nil {
  86. f.ee.encodeNil()
  87. return
  88. }
  89. f.ee.encodeMapStart(len(v))
  90. {{if eq .MapKey "string"}}asSymbols := f.e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0{{end}}
  91. j := 0
  92. for k2, v2 := range v {
  93. if j > 0 {
  94. f.ee.encodeMapEntrySeparator()
  95. }
  96. {{if eq .MapKey "string"}}if asSymbols {
  97. f.ee.encodeSymbol(k2)
  98. } else {
  99. f.ee.encodeString(c_UTF8, k2)
  100. }{{else}}{{ encmd .MapKey "k2"}}{{end}}
  101. f.ee.encodeMapKVSeparator()
  102. {{ encmd .Elem "v2"}}
  103. j++
  104. }
  105. f.ee.encodeMapEnd()
  106. }
  107. {{end}}{{end}}
  108. // -- decode
  109. {{range .Values}}{{if .Slice }}
  110. func (f *decFnInfo) {{ .MethodName false }}(rv reflect.Value) {
  111. xaddr := rv.CanAddr()
  112. var vp *[]{{ .Elem }}
  113. var v []{{ .Elem }}
  114. if xaddr {
  115. vp = rv.Addr().Interface().(*[]{{ .Elem }})
  116. v = *vp
  117. } else {
  118. v = rv.Interface().([]{{ .Elem }})
  119. }
  120. if f.dd.isContainerType(valueTypeNil) {
  121. if xaddr {
  122. v = nil
  123. *vp = v
  124. } // else do nothing.
  125. return
  126. }
  127. slh := decSliceHelper{dd: f.dd}
  128. containerLenS := slh.start()
  129. if containerLenS == 0 {
  130. if v == nil {
  131. v = []{{ .Elem }}{}
  132. } else {
  133. v = v[:0]
  134. }
  135. *vp = v
  136. f.dd.readArrayEnd()
  137. return
  138. }
  139. if v == nil {
  140. if containerLenS > 0 {
  141. v = make([]{{ .Elem }}, containerLenS, containerLenS)
  142. } else {
  143. v = make([]{{ .Elem }}, 0, 4)
  144. }
  145. } else if containerLenS > cap(v) {
  146. if f.array {
  147. decErr(msgDecCannotExpandArr, cap(v), containerLenS)
  148. }
  149. s := make([]{{ .Elem }}, containerLenS, containerLenS)
  150. copy(s, v)
  151. v = s
  152. } else if containerLenS > len(v) {
  153. v = v[:containerLenS]
  154. }
  155. // for j := 0; j < containerLenS; j++ {
  156. for j := 0; ; j++ {
  157. if containerLenS >= 0 {
  158. if j >= containerLenS {
  159. break
  160. }
  161. } else if f.dd.checkBreak() {
  162. break
  163. }
  164. if j >= len(v) {
  165. v = append(v, {{ zerocmd .Elem }})
  166. }
  167. if j > 0 {
  168. slh.sep(j)
  169. }
  170. {{ if eq .Elem "interface{}" }}f.d.decode(&v[j])
  171. {{ else }}f.dd.initReadNext()
  172. v[j] = {{ decmd .Elem }}
  173. {{ end }}
  174. }
  175. slh.end()
  176. if xaddr {
  177. *vp = v
  178. }
  179. }
  180. {{end}}{{end}}
  181. {{range .Values}}{{if not .Slice }}
  182. func (f *decFnInfo) {{ .MethodName false }}(rv reflect.Value) {
  183. xaddr := rv.CanAddr()
  184. var vp (*map[{{ .MapKey }}]{{ .Elem }})
  185. var v map[{{ .MapKey }}]{{ .Elem }}
  186. if xaddr {
  187. vp = rv.Addr().Interface().(*map[{{ .MapKey }}]{{ .Elem }})
  188. v = *vp
  189. } else {
  190. v = rv.Interface().(map[{{ .MapKey }}]{{ .Elem }})
  191. }
  192. if f.dd.isContainerType(valueTypeNil) {
  193. if xaddr {
  194. v = nil
  195. *vp = v
  196. } // else do nothing. We never remove from a map.
  197. return
  198. }
  199. containerLen := f.dd.readMapStart()
  200. if containerLen == 0 {
  201. if v == nil {
  202. v = map[{{ .MapKey }}]{{ .Elem }}{}
  203. *vp = v
  204. }
  205. f.dd.readMapEnd()
  206. return
  207. }
  208. if xaddr && v == nil {
  209. if containerLen > 0 {
  210. v = make(map[{{ .MapKey }}]{{ .Elem }}, containerLen)
  211. } else {
  212. v = make(map[{{ .MapKey }}]{{ .Elem }}) // supports indefinite-length, etc
  213. }
  214. *vp = v
  215. }
  216. // for j := 0; j < containerLen; j++ {
  217. for j := 0; ; j++ {
  218. if containerLen >= 0 {
  219. if j >= containerLen {
  220. break
  221. }
  222. } else if f.dd.checkBreak() {
  223. break
  224. }
  225. if j > 0 {
  226. f.dd.readMapEntrySeparator()
  227. }
  228. {{ if eq .MapKey "interface{}" }}var mk interface{}
  229. f.d.decode(&mk)
  230. // special case if a byte array.
  231. if bv, bok := mk.([]byte); bok {
  232. mk = string(bv)
  233. }
  234. {{ else }}f.dd.initReadNext()
  235. mk := {{ decmd .MapKey }}
  236. {{ end }}
  237. f.dd.readMapKVSeparator()
  238. mv := v[mk]
  239. {{ if eq .Elem "interface{}" }}f.d.decode(&mv)
  240. {{ else }}f.dd.initReadNext()
  241. mv = {{ decmd .Elem }}
  242. {{ end }}
  243. if v != nil {
  244. v[mk] = mv
  245. }
  246. }
  247. f.dd.readMapEnd()
  248. }
  249. {{end}}{{end}}
  250. `
  251. type genInfo struct {
  252. Slice bool
  253. MapKey string
  254. Elem string
  255. }
  256. func EncCommandAsString(s string, vname string) string {
  257. switch s {
  258. case "uint", "uint8", "uint16", "uint32", "uint64":
  259. return "f.ee.encodeUint(uint64(" + vname + "))"
  260. case "int", "int8", "int16", "int32", "int64":
  261. return "f.ee.encodeInt(int64(" + vname + "))"
  262. case "string":
  263. return "f.ee.encodeString(c_UTF8, " + vname + ")"
  264. case "float32":
  265. return "f.ee.encodeFloat32(" + vname + ")"
  266. case "float64":
  267. return "f.ee.encodeFloat64(" + vname + ")"
  268. case "bool":
  269. return "f.ee.encodeBool(" + vname + ")"
  270. case "symbol":
  271. return "f.ee.encodeSymbol(" + vname + ")"
  272. default:
  273. return "f.e.encode(" + vname + ")"
  274. }
  275. }
  276. func DecCommandAsString(s string) string {
  277. switch s {
  278. case "uint":
  279. return "uint(f.dd.decodeUint(uintBitsize))"
  280. case "uint8":
  281. return "uint8(f.dd.decodeUint(8))"
  282. case "uint16":
  283. return "uint16(f.dd.decodeUint(16))"
  284. case "uint32":
  285. return "uint32(f.dd.decodeUint(32))"
  286. case "uint64":
  287. return "f.dd.decodeUint(64)"
  288. case "int":
  289. return "int(f.dd.decodeInt(intBitsize))"
  290. case "int8":
  291. return "int8(f.dd.decodeInt(8))"
  292. case "int16":
  293. return "int16(f.dd.decodeInt(16))"
  294. case "int32":
  295. return "int32(f.dd.decodeInt(32))"
  296. case "int64":
  297. return "f.dd.decodeInt(64)"
  298. case "string":
  299. return "f.dd.decodeString()"
  300. case "float32":
  301. return "float32(f.dd.decodeFloat(true))"
  302. case "float64":
  303. return "f.dd.decodeFloat(false)"
  304. case "bool":
  305. return "f.dd.decodeBool()"
  306. default:
  307. panic("unknown type for decode: " + s)
  308. }
  309. }
  310. func (x *genInfo) MethodName(encode bool) string {
  311. var name []byte
  312. name = append(name, "fast"...)
  313. if encode {
  314. name = append(name, "Enc"...)
  315. } else {
  316. name = append(name, "Dec"...)
  317. }
  318. if x.Slice {
  319. name = append(name, "Slice"...)
  320. } else {
  321. name = append(name, "Map"...)
  322. name = append(name, titleCaseName(x.MapKey)...)
  323. }
  324. name = append(name, titleCaseName(x.Elem)...)
  325. return string(name)
  326. }
  327. func titleCaseName(s string) string {
  328. switch s {
  329. case "interface{}":
  330. return "Intf"
  331. default:
  332. return strings.ToUpper(s[0:1]) + s[1:]
  333. }
  334. }
  335. func ZeroValue(s string) string {
  336. switch s {
  337. case "interface{}":
  338. return "nil"
  339. case "bool":
  340. return "false"
  341. case "string":
  342. return `""`
  343. default:
  344. return "0"
  345. }
  346. }
  347. type genTmpl struct {
  348. Values []genInfo
  349. }
  350. func main() {
  351. types := []string{
  352. "interface{}",
  353. "string",
  354. "float32",
  355. "float64",
  356. "uint",
  357. "uint8",
  358. "uint16",
  359. "uint32",
  360. "uint64",
  361. "int",
  362. "int8",
  363. "int16",
  364. "int32",
  365. "int64",
  366. "bool",
  367. }
  368. // keep as slice, so it is in specific iteration order.
  369. // Initial order was uint64, string, interface{}, int, int64
  370. mapvaltypes := []string{
  371. "interface{}",
  372. "string",
  373. "uint",
  374. "uint32",
  375. "uint64",
  376. "int",
  377. "int32",
  378. "int64",
  379. }
  380. mapvaltypes2 := make(map[string]bool)
  381. for _, s := range mapvaltypes {
  382. mapvaltypes2[s] = true
  383. }
  384. var gt genTmpl
  385. // For each slice or map type, there must be a (symetrical) Encode and Decode fast-path function
  386. for _, s := range types {
  387. if s != "uint8" { // do not generate fast path for slice of bytes. Treat specially already.
  388. gt.Values = append(gt.Values, genInfo{true, "", s})
  389. }
  390. if !mapvaltypes2[s] {
  391. gt.Values = append(gt.Values, genInfo{false, s, s})
  392. }
  393. for _, ms := range mapvaltypes {
  394. gt.Values = append(gt.Values, genInfo{false, s, ms})
  395. }
  396. }
  397. funcs := make(template.FuncMap)
  398. // funcs["haspfx"] = strings.HasPrefix
  399. funcs["encmd"] = EncCommandAsString
  400. funcs["decmd"] = DecCommandAsString
  401. funcs["zerocmd"] = ZeroValue
  402. t := template.New("")
  403. t = t.Funcs(funcs)
  404. t, err := t.Parse(tmplstr)
  405. if err != nil {
  406. panic(err)
  407. }
  408. var out bytes.Buffer
  409. err = t.Execute(&out, &gt)
  410. if err != nil {
  411. panic(err)
  412. }
  413. // os.Stdout.Write(out.Bytes())
  414. bout, err := format.Source(out.Bytes())
  415. if err != nil {
  416. panic(err)
  417. }
  418. os.Stdout.Write(bout)
  419. }