buffer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import "io"
  10. const defaultBufSize = 4096
  11. // A buffer which is used for both reading and writing.
  12. // This is possible since communication on each connection is synchronous.
  13. // In other words, we can't write and read simultaneously on the same connection.
  14. // The buffer is similar to bufio.Reader / Writer but zero-copy-ish
  15. // Also highly optimized for this particular use case.
  16. type buffer struct {
  17. buf []byte
  18. rd io.Reader
  19. idx int
  20. length int
  21. }
  22. func newBuffer(rd io.Reader) *buffer {
  23. var b [defaultBufSize]byte
  24. return &buffer{
  25. buf: b[:],
  26. rd: rd,
  27. }
  28. }
  29. // fill reads into the buffer until at least _need_ bytes are in it
  30. func (b *buffer) fill(need int) (err error) {
  31. // move existing data to the beginning
  32. if b.length > 0 && b.idx > 0 {
  33. copy(b.buf[0:b.length], b.buf[b.idx:])
  34. }
  35. // grow buffer if necessary
  36. // TODO: let the buffer shrink again at some point
  37. // Maybe keep the org buf slice and swap back?
  38. if need > len(b.buf) {
  39. // Round up to the next multiple of the default size
  40. newBuf := make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
  41. copy(newBuf, b.buf)
  42. b.buf = newBuf
  43. }
  44. b.idx = 0
  45. var n int
  46. for {
  47. n, err = b.rd.Read(b.buf[b.length:])
  48. b.length += n
  49. if b.length < need && err == nil {
  50. continue
  51. }
  52. return // err
  53. }
  54. return
  55. }
  56. // returns next N bytes from buffer.
  57. // The returned slice is only guaranteed to be valid until the next read
  58. func (b *buffer) readNext(need int) (p []byte, err error) {
  59. if b.length < need {
  60. // refill
  61. err = b.fill(need) // err deferred
  62. if err == io.EOF && b.length >= need {
  63. err = nil
  64. }
  65. }
  66. p = b.buf[b.idx : b.idx+need]
  67. b.idx += need
  68. b.length -= need
  69. return
  70. }
  71. // returns a buffer with the requested size.
  72. // If possible, a slice from the existing buffer is returned.
  73. // Otherwise a bigger buffer is made.
  74. // Only one buffer (total) can be used at a time.
  75. func (b *buffer) takeBuffer(length int) []byte {
  76. if b.length > 0 {
  77. return nil
  78. }
  79. // test (cheap) general case first
  80. if length <= defaultBufSize || length <= cap(b.buf) {
  81. return b.buf[:length]
  82. }
  83. if length < maxPacketSize {
  84. b.buf = make([]byte, length)
  85. return b.buf
  86. }
  87. return make([]byte, length)
  88. }
  89. // shortcut which can be used if the requested buffer is guaranteed to be
  90. // smaller than defaultBufSize
  91. // Only one buffer (total) can be used at a time.
  92. func (b *buffer) takeSmallBuffer(length int) []byte {
  93. if b.length == 0 {
  94. return b.buf[:length]
  95. }
  96. return nil
  97. }
  98. // takeCompleteBuffer returns the complete existing buffer.
  99. // This can be used if the necessary buffer size is unknown.
  100. // Only one buffer (total) can be used at a time.
  101. func (b *buffer) takeCompleteBuffer() []byte {
  102. if b.length == 0 {
  103. return b.buf
  104. }
  105. return nil
  106. }
  107. var fieldCache = make(chan []mysqlField, 16)
  108. func makeFields(n int) []mysqlField {
  109. select {
  110. case f := <-fieldCache:
  111. if cap(f) >= n {
  112. return f[:n]
  113. }
  114. default:
  115. }
  116. return make([]mysqlField, n)
  117. }
  118. func putFields(f []mysqlField) {
  119. select {
  120. case fieldCache <- f:
  121. default:
  122. }
  123. }
  124. var rowsCache = make(chan *mysqlRows, 16)
  125. func newMysqlRows() *mysqlRows {
  126. select {
  127. case r := <-rowsCache:
  128. return r
  129. default:
  130. return new(mysqlRows)
  131. }
  132. }
  133. func putMysqlRows(r *mysqlRows) {
  134. *r = mysqlRows{} // zero it
  135. select {
  136. case rowsCache <- r:
  137. default:
  138. }
  139. }