gen-fast-path.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. import (
  36. "reflect"
  37. )
  38. func init() {
  39. if !fastpathEnabled {
  40. return // basically disable the fast path checks (since accessing empty map is basically free)
  41. }
  42. fdx := func(i interface{}, fd func(*decFnInfo, reflect.Value)) {
  43. fastpathsDec[reflect.ValueOf(reflect.TypeOf(i)).Pointer()] = fd
  44. }
  45. fex := func(i interface{}, fe func(*encFnInfo, reflect.Value)) {
  46. fastpathsEnc[reflect.ValueOf(reflect.TypeOf(i)).Pointer()] = fe
  47. }
  48. {{range .Values}}{{if .Encode}}{{if .Slice }}
  49. fex([]{{ .Elem }}(nil), (*encFnInfo).{{ .MethodName }}){{end}}{{end}}{{end}}
  50. {{range .Values}}{{if .Encode}}{{if not .Slice }}
  51. fex(map[{{ .MapKey }}]{{ .Elem }}(nil), (*encFnInfo).{{ .MethodName }}){{end}}{{end}}{{end}}
  52. {{range .Values}}{{if not .Encode}}{{if .Slice }}
  53. fdx([]{{ .Elem }}(nil), (*decFnInfo).{{ .MethodName }}){{end}}{{end}}{{end}}
  54. {{range .Values}}{{if not .Encode}}{{if not .Slice }}
  55. fdx(map[{{ .MapKey }}]{{ .Elem }}(nil), (*decFnInfo).{{ .MethodName }}){{end}}{{end}}{{end}}
  56. }
  57. // -- encode
  58. {{range .Values}}{{if .Encode}}{{if .Slice }}
  59. func (f *encFnInfo) {{ .MethodName }}(rv reflect.Value) {
  60. v := rv.Interface().([]{{ .Elem }})
  61. f.ee.encodeArrayPreamble(len(v))
  62. for _, v2 := range v {
  63. {{ encmd .Elem "v2"}}
  64. }
  65. }
  66. {{end}}{{end}}{{end}}
  67. {{range .Values}}{{if .Encode}}{{if not .Slice }}
  68. func (f *encFnInfo) {{ .MethodName }}(rv reflect.Value) {
  69. v := rv.Interface().(map[{{ .MapKey }}]{{ .Elem }})
  70. f.ee.encodeMapPreamble(len(v))
  71. {{if eq .MapKey "string"}}asSymbols := f.e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0{{end}}
  72. for k2, v2 := range v {
  73. {{if eq .MapKey "string"}}if asSymbols {
  74. f.ee.encodeSymbol(k2)
  75. } else {
  76. f.ee.encodeString(c_UTF8, k2)
  77. }{{else}}{{ encmd .MapKey "k2"}}{{end}}
  78. {{ encmd .Elem "v2"}}
  79. }
  80. }
  81. {{end}}{{end}}{{end}}
  82. // -- decode
  83. {{range .Values}}{{if not .Encode}}{{if .Slice }}
  84. func (f *decFnInfo) {{ .MethodName }}(rv reflect.Value) {
  85. v := rv.Addr().Interface().(*[]{{ .Elem }})
  86. var s []{{ .Elem }}
  87. vtype := f.dd.currentEncodedType()
  88. if vtype == valueTypeNil {
  89. *v = s
  90. return
  91. }
  92. _, containerLenS := decContLens(f.dd, vtype)
  93. s = *v
  94. if s == nil {
  95. s = make([]{{ .Elem }}, containerLenS, containerLenS)
  96. } else if containerLenS > cap(s) {
  97. if f.array {
  98. decErr(msgDecCannotExpandArr, cap(s), containerLenS)
  99. }
  100. s = make([]{{ .Elem }}, containerLenS, containerLenS)
  101. copy(s, *v)
  102. } else if containerLenS > len(s) {
  103. s = s[:containerLenS]
  104. }
  105. for j := 0; j < containerLenS; j++ {
  106. {{ if eq .Elem "interface{}" }}f.d.decode(&s[j])
  107. {{ else }}f.dd.initReadNext()
  108. s[j] = {{ decmd .Elem }}
  109. {{ end }}
  110. }
  111. *v = s
  112. }
  113. {{end}}{{end}}{{end}}
  114. {{range .Values}}{{if not .Encode}}{{if not .Slice }}
  115. func (f *decFnInfo) {{ .MethodName }}(rv reflect.Value) {
  116. v := rv.Addr().Interface().(*map[{{ .MapKey }}]{{ .Elem }})
  117. var m map[{{ .MapKey }}]{{ .Elem }}
  118. vtype := f.dd.currentEncodedType()
  119. if vtype == valueTypeNil {
  120. *v = m
  121. return
  122. }
  123. containerLen := f.dd.readMapLen()
  124. m = *v
  125. if m == nil {
  126. m = make(map[{{ .MapKey }}]{{ .Elem }}, containerLen)
  127. *v = m
  128. }
  129. for j := 0; j < containerLen; j++ {
  130. {{ if eq .MapKey "interface{}" }}var mk interface{}
  131. f.d.decode(&mk)
  132. // special case if a byte array.
  133. if bv, bok := mk.([]byte); bok {
  134. mk = string(bv)
  135. }
  136. {{ else }}f.dd.initReadNext()
  137. mk := {{ decmd .MapKey }}
  138. {{ end }}
  139. mv := m[mk]
  140. {{ if eq .Elem "interface{}" }}f.d.decode(&mv)
  141. {{ else }}f.dd.initReadNext()
  142. mv = {{ decmd .Elem }}
  143. {{ end }}
  144. m[mk] = mv
  145. }
  146. }
  147. {{end}}{{end}}{{end}}
  148. `
  149. type genInfo struct {
  150. Slice bool
  151. Encode bool
  152. MapKey string
  153. Elem string
  154. }
  155. func EncCommandAsString(s string, vname string) string {
  156. switch s {
  157. case "uint", "uint8", "uint16", "uint31", "uint64":
  158. return "f.ee.encodeUint(uint64(" + vname + "))"
  159. case "int", "int8", "int16", "int31", "int64":
  160. return "f.ee.encodeInt(int64(" + vname + "))"
  161. case "string":
  162. return "f.ee.encodeString(c_UTF8, " + vname + ")"
  163. case "float32":
  164. return "f.ee.encodeFloat32(" + vname + ")"
  165. case "float64":
  166. return "f.ee.encodeFloat64(" + vname + ")"
  167. case "bool":
  168. return "f.ee.encodeBool(" + vname + ")"
  169. case "symbol":
  170. return "f.ee.encodeSymbol(" + vname + ")"
  171. default:
  172. return "f.e.encode(" + vname + ")"
  173. }
  174. }
  175. func DecCommandAsString(s string) string {
  176. switch s {
  177. case "uint":
  178. return "uint(f.dd.decodeUint(uintBitsize))"
  179. case "uint8":
  180. return "uint8(f.dd.decodeUint(8))"
  181. case "uint16":
  182. return "uint16(f.dd.decodeUint(16))"
  183. case "uint32":
  184. return "uint32(f.dd.decodeUint(32))"
  185. case "uint64":
  186. return "f.dd.decodeUint(64)"
  187. case "int":
  188. return "int(f.dd.decodeInt(intBitsize))"
  189. case "int8":
  190. return "int8(f.dd.decodeInt(8))"
  191. case "int16":
  192. return "int16(f.dd.decodeInt(16))"
  193. case "int32":
  194. return "int32(f.dd.decodeInt(32))"
  195. case "int64":
  196. return "f.dd.decodeInt(64)"
  197. case "string":
  198. return "f.dd.decodeString()"
  199. case "float32":
  200. return "float32(f.dd.decodeFloat(true))"
  201. case "float64":
  202. return "f.dd.decodeFloat(false)"
  203. case "bool":
  204. return "f.dd.decodeBool()"
  205. default:
  206. panic("unknown type for decode: " + s)
  207. }
  208. }
  209. func (x *genInfo) MethodName() string {
  210. var name []byte
  211. name = append(name, "fast"...)
  212. if x.Encode {
  213. name = append(name, "Enc"...)
  214. } else {
  215. name = append(name, "Dec"...)
  216. }
  217. if x.Slice {
  218. name = append(name, "Slice"...)
  219. } else {
  220. name = append(name, "Map"...)
  221. name = append(name, titleCaseName(x.MapKey)...)
  222. }
  223. name = append(name, titleCaseName(x.Elem)...)
  224. return string(name)
  225. }
  226. func titleCaseName(s string) string {
  227. switch s {
  228. case "interface{}":
  229. return "Intf"
  230. default:
  231. return strings.ToUpper(s[0:1]) + s[1:]
  232. }
  233. }
  234. type genTmpl struct {
  235. Values []genInfo
  236. }
  237. func main() {
  238. types := []string{
  239. "interface{}",
  240. "string",
  241. "float32",
  242. "float64",
  243. "uint",
  244. "uint8",
  245. "uint16",
  246. "uint32",
  247. "uint64",
  248. "int",
  249. "int8",
  250. "int16",
  251. "int32",
  252. "int64",
  253. "bool",
  254. }
  255. var gt genTmpl
  256. for _, s := range types {
  257. if s != "uint8" { // do not generate fast path for slice of bytes. Treat specially already.
  258. gt.Values = append(gt.Values, genInfo{true, true, "", s})
  259. gt.Values = append(gt.Values, genInfo{true, false, "", s})
  260. }
  261. gt.Values = append(gt.Values, genInfo{false, true, s, "interface{}"})
  262. gt.Values = append(gt.Values, genInfo{false, false, s, "interface{}"})
  263. gt.Values = append(gt.Values, genInfo{false, true, s, "string"})
  264. gt.Values = append(gt.Values, genInfo{false, false, s, "string"})
  265. if s != "string" && s != "interface{}" {
  266. gt.Values = append(gt.Values, genInfo{false, true, s, s})
  267. gt.Values = append(gt.Values, genInfo{false, false, s, s})
  268. }
  269. }
  270. funcs := make(template.FuncMap)
  271. // funcs["haspfx"] = strings.HasPrefix
  272. funcs["encmd"] = EncCommandAsString
  273. funcs["decmd"] = DecCommandAsString
  274. t := template.New("")
  275. t = t.Funcs(funcs)
  276. t, err := t.Parse(tmplstr)
  277. if err != nil {
  278. panic(err)
  279. }
  280. var out bytes.Buffer
  281. err = t.Execute(&out, &gt)
  282. if err != nil {
  283. panic(err)
  284. }
  285. // os.Stdout.Write(out.Bytes())
  286. bout, err := format.Source(out.Bytes())
  287. if err != nil {
  288. panic(err)
  289. }
  290. os.Stdout.Write(bout)
  291. }