writer.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import "io"
  5. // encWriter abstracts writing to a byte array or to an io.Writer.
  6. type encWriter interface {
  7. writeb([]byte)
  8. writestr(string)
  9. writeqstr(string) // write string wrapped in quotes ie "..."
  10. writen1(byte)
  11. // add convenience functions for writing 2,4
  12. writen2(byte, byte)
  13. writen4([4]byte)
  14. writen8([8]byte)
  15. end()
  16. }
  17. // ---------------------------------------------
  18. type bufioEncWriter struct {
  19. w io.Writer
  20. buf []byte
  21. n int
  22. b [16]byte // scratch buffer and padding (cache-aligned)
  23. }
  24. func (z *bufioEncWriter) reset(w io.Writer, bufsize int, blist *bytesFreelist) {
  25. z.w = w
  26. z.n = 0
  27. if bufsize <= 0 {
  28. bufsize = defEncByteBufSize
  29. }
  30. // bufsize must be >= 8, to accomodate writen methods (where n <= 8)
  31. if bufsize <= 8 {
  32. bufsize = 8
  33. }
  34. if cap(z.buf) < bufsize {
  35. if len(z.buf) > 0 && &z.buf[0] != &z.b[0] {
  36. blist.put(z.buf)
  37. }
  38. if len(z.b) > bufsize {
  39. z.buf = z.b[:]
  40. } else {
  41. z.buf = blist.get(bufsize)
  42. }
  43. }
  44. z.buf = z.buf[:cap(z.buf)]
  45. }
  46. func (z *bufioEncWriter) flushErr() (err error) {
  47. n, err := z.w.Write(z.buf[:z.n])
  48. z.n -= n
  49. if z.n > 0 {
  50. if err == nil {
  51. err = io.ErrShortWrite
  52. }
  53. if n > 0 {
  54. copy(z.buf, z.buf[n:z.n+n])
  55. }
  56. }
  57. return err
  58. }
  59. func (z *bufioEncWriter) flush() {
  60. halt.onerror(z.flushErr())
  61. }
  62. func (z *bufioEncWriter) writeb(s []byte) {
  63. LOOP:
  64. a := len(z.buf) - z.n
  65. if len(s) > a {
  66. z.n += copy(z.buf[z.n:], s[:a])
  67. s = s[a:]
  68. z.flush()
  69. goto LOOP
  70. }
  71. z.n += copy(z.buf[z.n:], s)
  72. }
  73. func (z *bufioEncWriter) writestr(s string) {
  74. // z.writeb(bytesView(s)) // inlined below
  75. LOOP:
  76. a := len(z.buf) - z.n
  77. if len(s) > a {
  78. z.n += copy(z.buf[z.n:], s[:a])
  79. s = s[a:]
  80. z.flush()
  81. goto LOOP
  82. }
  83. z.n += copy(z.buf[z.n:], s)
  84. }
  85. func (z *bufioEncWriter) writeqstr(s string) {
  86. // z.writen1('"')
  87. // z.writestr(s)
  88. // z.writen1('"')
  89. if z.n+len(s)+2 > len(z.buf) {
  90. z.flush()
  91. }
  92. setByteAt(z.buf, uint(z.n), '"')
  93. // z.buf[z.n] = '"'
  94. z.n++
  95. LOOP:
  96. a := len(z.buf) - z.n
  97. if len(s)+1 > a {
  98. z.n += copy(z.buf[z.n:], s[:a])
  99. s = s[a:]
  100. z.flush()
  101. goto LOOP
  102. }
  103. z.n += copy(z.buf[z.n:], s)
  104. setByteAt(z.buf, uint(z.n), '"')
  105. // z.buf[z.n] = '"'
  106. z.n++
  107. }
  108. func (z *bufioEncWriter) writen1(b1 byte) {
  109. if 1 > len(z.buf)-z.n {
  110. z.flush()
  111. }
  112. setByteAt(z.buf, uint(z.n), b1)
  113. // z.buf[z.n] = b1
  114. z.n++
  115. }
  116. func (z *bufioEncWriter) writen2(b1, b2 byte) {
  117. if 2 > len(z.buf)-z.n {
  118. z.flush()
  119. }
  120. setByteAt(z.buf, uint(z.n+1), b2)
  121. setByteAt(z.buf, uint(z.n), b1)
  122. // z.buf[z.n+1] = b2
  123. // z.buf[z.n] = b1
  124. z.n += 2
  125. }
  126. func (z *bufioEncWriter) writen4(b [4]byte) {
  127. if 4 > len(z.buf)-z.n {
  128. z.flush()
  129. }
  130. // setByteAt(z.buf, uint(z.n+3), b4)
  131. // setByteAt(z.buf, uint(z.n+2), b3)
  132. // setByteAt(z.buf, uint(z.n+1), b2)
  133. // setByteAt(z.buf, uint(z.n), b1)
  134. copy(z.buf[z.n:], b[:])
  135. z.n += 4
  136. }
  137. func (z *bufioEncWriter) writen8(b [8]byte) {
  138. if 8 > len(z.buf)-z.n {
  139. z.flush()
  140. }
  141. copy(z.buf[z.n:], b[:])
  142. z.n += 8
  143. }
  144. func (z *bufioEncWriter) endErr() (err error) {
  145. if z.n > 0 {
  146. err = z.flushErr()
  147. }
  148. return
  149. }
  150. // ---------------------------------------------
  151. // bytesEncAppender implements encWriter and can write to an byte slice.
  152. type bytesEncAppender struct {
  153. b []byte
  154. out *[]byte
  155. }
  156. func (z *bytesEncAppender) writeb(s []byte) {
  157. z.b = append(z.b, s...)
  158. }
  159. func (z *bytesEncAppender) writestr(s string) {
  160. z.b = append(z.b, s...)
  161. }
  162. func (z *bytesEncAppender) writeqstr(s string) {
  163. z.b = append(append(append(z.b, '"'), s...), '"')
  164. // z.b = append(z.b, '"')
  165. // z.b = append(z.b, s...)
  166. // z.b = append(z.b, '"')
  167. }
  168. func (z *bytesEncAppender) writen1(b1 byte) {
  169. z.b = append(z.b, b1)
  170. }
  171. func (z *bytesEncAppender) writen2(b1, b2 byte) {
  172. z.b = append(z.b, b1, b2)
  173. }
  174. func (z *bytesEncAppender) writen4(b [4]byte) {
  175. z.b = append(z.b, b[:]...)
  176. // z.b = append(z.b, b1, b2, b3, b4) // prevents inlining encWr.writen4
  177. }
  178. func (z *bytesEncAppender) writen8(b [8]byte) {
  179. z.b = append(z.b, b[:]...)
  180. // z.b = append(z.b, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]) // prevents inlining encWr.writen4
  181. }
  182. func (z *bytesEncAppender) endErr() error {
  183. *(z.out) = z.b
  184. return nil
  185. }
  186. func (z *bytesEncAppender) reset(in []byte, out *[]byte) {
  187. z.b = in[:0]
  188. z.out = out
  189. }
  190. // --------------------------------------------------
  191. type encWr struct {
  192. wb bytesEncAppender
  193. wf *bufioEncWriter
  194. bytes bool // encoding to []byte
  195. // MARKER: these fields below should belong directly in Encoder.
  196. // we pack them here for space efficiency and cache-line optimization.
  197. js bool // is json encoder?
  198. be bool // is binary encoder?
  199. c containerState
  200. calls uint16
  201. seq uint16 // sequencer (e.g. used by binc for symbols, etc)
  202. }
  203. // MARKER: manually inline bytesEncAppender.writenx/writeqstr methods,
  204. // as calling them causes encWr.writenx/writeqstr methods to not be inlined (cost > 80).
  205. //
  206. // i.e. e.g. instead of writing z.wb.writen2(b1, b2), use z.wb.b = append(z.wb.b, b1, b2)
  207. func (z *encWr) writeb(s []byte) {
  208. if z.bytes {
  209. z.wb.writeb(s)
  210. } else {
  211. z.wf.writeb(s)
  212. }
  213. }
  214. func (z *encWr) writestr(s string) {
  215. if z.bytes {
  216. z.wb.writestr(s)
  217. } else {
  218. z.wf.writestr(s)
  219. }
  220. }
  221. // MARKER: Add WriteStr to be called directly by generated code without a genHelper forwarding function.
  222. // Go's inlining model adds cost for forwarding functions, preventing inlining (cost goes above 80 budget).
  223. func (z *encWr) WriteStr(s string) {
  224. if z.bytes {
  225. z.wb.writestr(s)
  226. } else {
  227. z.wf.writestr(s)
  228. }
  229. }
  230. func (z *encWr) writen1(b1 byte) {
  231. if z.bytes {
  232. z.wb.writen1(b1)
  233. } else {
  234. z.wf.writen1(b1)
  235. }
  236. }
  237. func (z *encWr) writen2(b1, b2 byte) {
  238. if z.bytes {
  239. // MARKER: z.wb.writen2(b1, b2)
  240. z.wb.b = append(z.wb.b, b1, b2)
  241. } else {
  242. z.wf.writen2(b1, b2)
  243. }
  244. }
  245. func (z *encWr) writen4(b [4]byte) {
  246. if z.bytes {
  247. // MARKER: z.wb.writen4(b1, b2, b3, b4)
  248. z.wb.b = append(z.wb.b, b[:]...)
  249. // z.wb.writen4(b)
  250. } else {
  251. z.wf.writen4(b)
  252. }
  253. }
  254. func (z *encWr) writen8(b [8]byte) {
  255. if z.bytes {
  256. // z.wb.b = append(z.wb.b, b[:]...)
  257. z.wb.writen8(b)
  258. } else {
  259. z.wf.writen8(b)
  260. }
  261. }
  262. func (z *encWr) writeqstr(s string) {
  263. if z.bytes {
  264. // MARKER: z.wb.writeqstr(s)
  265. z.wb.b = append(append(append(z.wb.b, '"'), s...), '"')
  266. } else {
  267. z.wf.writeqstr(s)
  268. }
  269. }
  270. func (z *encWr) endErr() error {
  271. if z.bytes {
  272. return z.wb.endErr()
  273. }
  274. return z.wf.endErr()
  275. }
  276. func (z *encWr) end() {
  277. halt.onerror(z.endErr())
  278. }
  279. var _ encWriter = (*encWr)(nil)