chunkreader.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package pgproto3
  2. import (
  3. "io"
  4. "github.com/jackc/pgx/v5/internal/iobufpool"
  5. )
  6. // chunkReader is a io.Reader wrapper that minimizes IO reads and memory allocations. It allocates memory in chunks and
  7. // will read as much as will fit in the current buffer in a single call regardless of how large a read is actually
  8. // requested. The memory returned via Next is only valid until the next call to Next.
  9. //
  10. // This is roughly equivalent to a bufio.Reader that only uses Peek and Discard to never copy bytes.
  11. type chunkReader struct {
  12. r io.Reader
  13. buf *[]byte
  14. rp, wp int // buf read position and write position
  15. minBufSize int
  16. }
  17. // newChunkReader creates and returns a new chunkReader for r with default configuration. If minBufSize is <= 0 it uses
  18. // a default value.
  19. func newChunkReader(r io.Reader, minBufSize int) *chunkReader {
  20. if minBufSize <= 0 {
  21. // By historical reasons Postgres currently has 8KB send buffer inside,
  22. // so here we want to have at least the same size buffer.
  23. // @see https://github.com/postgres/postgres/blob/249d64999615802752940e017ee5166e726bc7cd/src/backend/libpq/pqcomm.c#L134
  24. // @see https://www.postgresql.org/message-id/0cdc5485-cb3c-5e16-4a46-e3b2f7a41322%40ya.ru
  25. //
  26. // In addition, testing has found no benefit of any larger buffer.
  27. minBufSize = 8192
  28. }
  29. return &chunkReader{
  30. r: r,
  31. minBufSize: minBufSize,
  32. buf: iobufpool.Get(minBufSize),
  33. }
  34. }
  35. // Next returns buf filled with the next n bytes. buf is only valid until next call of Next. If an error occurs, buf
  36. // will be nil.
  37. func (r *chunkReader) Next(n int) (buf []byte, err error) {
  38. // Reset the buffer if it is empty
  39. if r.rp == r.wp {
  40. if len(*r.buf) != r.minBufSize {
  41. iobufpool.Put(r.buf)
  42. r.buf = iobufpool.Get(r.minBufSize)
  43. }
  44. r.rp = 0
  45. r.wp = 0
  46. }
  47. // n bytes already in buf
  48. if (r.wp - r.rp) >= n {
  49. buf = (*r.buf)[r.rp : r.rp+n : r.rp+n]
  50. r.rp += n
  51. return buf, err
  52. }
  53. // buf is smaller than requested number of bytes
  54. if len(*r.buf) < n {
  55. bigBuf := iobufpool.Get(n)
  56. r.wp = copy((*bigBuf), (*r.buf)[r.rp:r.wp])
  57. r.rp = 0
  58. iobufpool.Put(r.buf)
  59. r.buf = bigBuf
  60. }
  61. // buf is large enough, but need to shift filled area to start to make enough contiguous space
  62. minReadCount := n - (r.wp - r.rp)
  63. if (len(*r.buf) - r.wp) < minReadCount {
  64. r.wp = copy((*r.buf), (*r.buf)[r.rp:r.wp])
  65. r.rp = 0
  66. }
  67. // Read at least the required number of bytes from the underlying io.Reader
  68. readBytesCount, err := io.ReadAtLeast(r.r, (*r.buf)[r.wp:], minReadCount)
  69. r.wp += readBytesCount
  70. // fmt.Println("read", n)
  71. if err != nil {
  72. return nil, err
  73. }
  74. buf = (*r.buf)[r.rp : r.rp+n : r.rp+n]
  75. r.rp += n
  76. return buf, nil
  77. }