gen-fast-path.go 9.6 KB

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