gen-fast-path.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. return
  117. }
  118. if v == nil {
  119. if containerLenS > 0 {
  120. v = make([]{{ .Elem }}, containerLenS, containerLenS)
  121. } else {
  122. v = make([]{{ .Elem }}, 0, 4)
  123. }
  124. } else if containerLenS > cap(v) {
  125. if f.array {
  126. decErr(msgDecCannotExpandArr, cap(v), containerLenS)
  127. }
  128. s := make([]{{ .Elem }}, containerLenS, containerLenS)
  129. copy(s, v)
  130. v = s
  131. } else if containerLenS > len(v) {
  132. v = v[:containerLenS]
  133. }
  134. // for j := 0; j < containerLenS; j++ {
  135. for j := 0; ; j++ {
  136. if containerLenS >= 0 {
  137. if j >= containerLenS {
  138. break
  139. }
  140. } else if f.dd.checkBreak() {
  141. break
  142. }
  143. if j >= len(v) {
  144. v = append(v, {{ zerocmd .Elem }})
  145. }
  146. {{ if eq .Elem "interface{}" }}f.d.decode(&v[j])
  147. {{ else }}f.dd.initReadNext()
  148. v[j] = {{ decmd .Elem }}
  149. {{ end }}
  150. }
  151. if xaddr {
  152. *vp = v
  153. }
  154. }
  155. {{end}}{{end}}
  156. {{range .Values}}{{if not .Slice }}
  157. func (f *decFnInfo) {{ .MethodName false }}(rv reflect.Value) {
  158. xaddr := rv.CanAddr()
  159. var vp (*map[{{ .MapKey }}]{{ .Elem }})
  160. var v map[{{ .MapKey }}]{{ .Elem }}
  161. if xaddr {
  162. vp = rv.Addr().Interface().(*map[{{ .MapKey }}]{{ .Elem }})
  163. v = *vp
  164. } else {
  165. v = rv.Interface().(map[{{ .MapKey }}]{{ .Elem }})
  166. }
  167. vtype := f.dd.currentEncodedType()
  168. if vtype == valueTypeNil {
  169. if xaddr {
  170. v = nil
  171. *vp = v
  172. } // else do nothing. We never remove from a map.
  173. return
  174. }
  175. containerLen := f.dd.readMapLen()
  176. if containerLen == 0 {
  177. return
  178. }
  179. if xaddr && v == nil {
  180. if containerLen > 0 {
  181. v = make(map[{{ .MapKey }}]{{ .Elem }}, containerLen)
  182. } else {
  183. v = make(map[{{ .MapKey }}]{{ .Elem }}) // supports indefinite-length, etc
  184. }
  185. *vp = v
  186. }
  187. // for j := 0; j < containerLen; j++ {
  188. for j := 0; ; j++ {
  189. if containerLen >= 0 {
  190. if j >= containerLen {
  191. break
  192. }
  193. } else if f.dd.checkBreak() {
  194. break
  195. }
  196. {{ if eq .MapKey "interface{}" }}var mk interface{}
  197. f.d.decode(&mk)
  198. // special case if a byte array.
  199. if bv, bok := mk.([]byte); bok {
  200. mk = string(bv)
  201. }
  202. {{ else }}f.dd.initReadNext()
  203. mk := {{ decmd .MapKey }}
  204. {{ end }}
  205. mv := v[mk]
  206. {{ if eq .Elem "interface{}" }}f.d.decode(&mv)
  207. {{ else }}f.dd.initReadNext()
  208. mv = {{ decmd .Elem }}
  209. {{ end }}
  210. if v != nil {
  211. v[mk] = mv
  212. }
  213. }
  214. }
  215. {{end}}{{end}}
  216. `
  217. type genInfo struct {
  218. Slice bool
  219. MapKey string
  220. Elem string
  221. }
  222. func EncCommandAsString(s string, vname string) string {
  223. switch s {
  224. case "uint", "uint8", "uint16", "uint32", "uint64":
  225. return "f.ee.encodeUint(uint64(" + vname + "))"
  226. case "int", "int8", "int16", "int32", "int64":
  227. return "f.ee.encodeInt(int64(" + vname + "))"
  228. case "string":
  229. return "f.ee.encodeString(c_UTF8, " + vname + ")"
  230. case "float32":
  231. return "f.ee.encodeFloat32(" + vname + ")"
  232. case "float64":
  233. return "f.ee.encodeFloat64(" + vname + ")"
  234. case "bool":
  235. return "f.ee.encodeBool(" + vname + ")"
  236. case "symbol":
  237. return "f.ee.encodeSymbol(" + vname + ")"
  238. default:
  239. return "f.e.encode(" + vname + ")"
  240. }
  241. }
  242. func DecCommandAsString(s string) string {
  243. switch s {
  244. case "uint":
  245. return "uint(f.dd.decodeUint(uintBitsize))"
  246. case "uint8":
  247. return "uint8(f.dd.decodeUint(8))"
  248. case "uint16":
  249. return "uint16(f.dd.decodeUint(16))"
  250. case "uint32":
  251. return "uint32(f.dd.decodeUint(32))"
  252. case "uint64":
  253. return "f.dd.decodeUint(64)"
  254. case "int":
  255. return "int(f.dd.decodeInt(intBitsize))"
  256. case "int8":
  257. return "int8(f.dd.decodeInt(8))"
  258. case "int16":
  259. return "int16(f.dd.decodeInt(16))"
  260. case "int32":
  261. return "int32(f.dd.decodeInt(32))"
  262. case "int64":
  263. return "f.dd.decodeInt(64)"
  264. case "string":
  265. return "f.dd.decodeString()"
  266. case "float32":
  267. return "float32(f.dd.decodeFloat(true))"
  268. case "float64":
  269. return "f.dd.decodeFloat(false)"
  270. case "bool":
  271. return "f.dd.decodeBool()"
  272. default:
  273. panic("unknown type for decode: " + s)
  274. }
  275. }
  276. func (x *genInfo) MethodName(encode bool) string {
  277. var name []byte
  278. name = append(name, "fast"...)
  279. if encode {
  280. name = append(name, "Enc"...)
  281. } else {
  282. name = append(name, "Dec"...)
  283. }
  284. if x.Slice {
  285. name = append(name, "Slice"...)
  286. } else {
  287. name = append(name, "Map"...)
  288. name = append(name, titleCaseName(x.MapKey)...)
  289. }
  290. name = append(name, titleCaseName(x.Elem)...)
  291. return string(name)
  292. }
  293. func titleCaseName(s string) string {
  294. switch s {
  295. case "interface{}":
  296. return "Intf"
  297. default:
  298. return strings.ToUpper(s[0:1]) + s[1:]
  299. }
  300. }
  301. func ZeroValue(s string) string {
  302. switch s {
  303. case "interface{}":
  304. return "nil"
  305. case "bool":
  306. return "false"
  307. case "string":
  308. return `""`
  309. default:
  310. return "0"
  311. }
  312. }
  313. type genTmpl struct {
  314. Values []genInfo
  315. }
  316. func main() {
  317. types := []string{
  318. "interface{}",
  319. "string",
  320. "float32",
  321. "float64",
  322. "uint",
  323. "uint8",
  324. "uint16",
  325. "uint32",
  326. "uint64",
  327. "int",
  328. "int8",
  329. "int16",
  330. "int32",
  331. "int64",
  332. "bool",
  333. }
  334. // keep as slice, so it is in specific iteration order.
  335. // Initial order was uint64, string, interface{}, int, int64
  336. mapvaltypes := []string{
  337. "interface{}",
  338. "string",
  339. "uint",
  340. "uint32",
  341. "uint64",
  342. "int",
  343. "int32",
  344. "int64",
  345. }
  346. mapvaltypes2 := make(map[string]bool)
  347. for _, s := range mapvaltypes {
  348. mapvaltypes2[s] = true
  349. }
  350. var gt genTmpl
  351. // For each slice or map type, there must be a (symetrical) Encode and Decode fast-path function
  352. for _, s := range types {
  353. if s != "uint8" { // do not generate fast path for slice of bytes. Treat specially already.
  354. gt.Values = append(gt.Values, genInfo{true, "", s})
  355. }
  356. if !mapvaltypes2[s] {
  357. gt.Values = append(gt.Values, genInfo{false, s, s})
  358. }
  359. for _, ms := range mapvaltypes {
  360. gt.Values = append(gt.Values, genInfo{false, s, ms})
  361. }
  362. }
  363. funcs := make(template.FuncMap)
  364. // funcs["haspfx"] = strings.HasPrefix
  365. funcs["encmd"] = EncCommandAsString
  366. funcs["decmd"] = DecCommandAsString
  367. funcs["zerocmd"] = ZeroValue
  368. t := template.New("")
  369. t = t.Funcs(funcs)
  370. t, err := t.Parse(tmplstr)
  371. if err != nil {
  372. panic(err)
  373. }
  374. var out bytes.Buffer
  375. err = t.Execute(&out, &gt)
  376. if err != nil {
  377. panic(err)
  378. }
  379. // os.Stdout.Write(out.Bytes())
  380. bout, err := format.Source(out.Bytes())
  381. if err != nil {
  382. panic(err)
  383. }
  384. os.Stdout.Write(bout)
  385. }