pools.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2020 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package jsontext
  5. import (
  6. "bytes"
  7. "io"
  8. "math/bits"
  9. "sync"
  10. )
  11. // TODO(https://go.dev/issue/47657): Use sync.PoolOf.
  12. var (
  13. // This owns the internal buffer since there is no io.Writer to output to.
  14. // Since the buffer can get arbitrarily large in normal usage,
  15. // there is statistical tracking logic to determine whether to recycle
  16. // the internal buffer or not based on a history of utilization.
  17. bufferedEncoderPool = &sync.Pool{New: func() any { return new(Encoder) }}
  18. // This owns the internal buffer, but it is only used to temporarily store
  19. // buffered JSON before flushing it to the underlying io.Writer.
  20. // In a sufficiently efficient streaming mode, we do not expect the buffer
  21. // to grow arbitrarily large. Thus, we avoid recycling large buffers.
  22. streamingEncoderPool = &sync.Pool{New: func() any { return new(Encoder) }}
  23. // This does not own the internal buffer since
  24. // it is taken directly from the provided bytes.Buffer.
  25. bytesBufferEncoderPool = &sync.Pool{New: func() any { return new(Encoder) }}
  26. )
  27. // bufferStatistics is statistics to track buffer utilization.
  28. // It is used to determine whether to recycle a buffer or not
  29. // to avoid https://go.dev/issue/23199.
  30. type bufferStatistics struct {
  31. strikes int // number of times the buffer was under-utilized
  32. prevLen int // length of previous buffer
  33. }
  34. func getBufferedEncoder(opts ...Options) *Encoder {
  35. e := bufferedEncoderPool.Get().(*Encoder)
  36. if e.s.Buf == nil {
  37. // Round up to nearest 2ⁿ to make best use of malloc size classes.
  38. // See runtime/sizeclasses.go on Go1.15.
  39. // Logical OR with 63 to ensure 64 as the minimum buffer size.
  40. n := 1 << bits.Len(uint(e.s.bufStats.prevLen|63))
  41. e.s.Buf = make([]byte, 0, n)
  42. }
  43. e.s.reset(e.s.Buf[:0], nil, opts...)
  44. return e
  45. }
  46. func putBufferedEncoder(e *Encoder) {
  47. // Recycle large buffers only if sufficiently utilized.
  48. // If a buffer is under-utilized enough times sequentially,
  49. // then it is discarded, ensuring that a single large buffer
  50. // won't be kept alive by a continuous stream of small usages.
  51. //
  52. // The worst case utilization is computed as:
  53. // MIN_UTILIZATION_THRESHOLD / (1 + MAX_NUM_STRIKES)
  54. //
  55. // For the constants chosen below, this is (25%)/(1+4) ⇒ 5%.
  56. // This may seem low, but it ensures a lower bound on
  57. // the absolute worst-case utilization. Without this check,
  58. // this would be theoretically 0%, which is infinitely worse.
  59. //
  60. // See https://go.dev/issue/27735.
  61. switch {
  62. case cap(e.s.Buf) <= 4<<10: // always recycle buffers smaller than 4KiB
  63. e.s.bufStats.strikes = 0
  64. case cap(e.s.Buf)/4 <= len(e.s.Buf): // at least 25% utilization
  65. e.s.bufStats.strikes = 0
  66. case e.s.bufStats.strikes < 4: // at most 4 strikes
  67. e.s.bufStats.strikes++
  68. default: // discard the buffer; too large and too often under-utilized
  69. e.s.bufStats.strikes = 0
  70. e.s.bufStats.prevLen = len(e.s.Buf) // heuristic for size to allocate next time
  71. e.s.Buf = nil
  72. }
  73. bufferedEncoderPool.Put(e)
  74. }
  75. func getStreamingEncoder(w io.Writer, opts ...Options) *Encoder {
  76. if _, ok := w.(*bytes.Buffer); ok {
  77. e := bytesBufferEncoderPool.Get().(*Encoder)
  78. e.s.reset(nil, w, opts...) // buffer taken from bytes.Buffer
  79. return e
  80. } else {
  81. e := streamingEncoderPool.Get().(*Encoder)
  82. e.s.reset(e.s.Buf[:0], w, opts...) // preserve existing buffer
  83. return e
  84. }
  85. }
  86. func putStreamingEncoder(e *Encoder) {
  87. if _, ok := e.s.wr.(*bytes.Buffer); ok {
  88. bytesBufferEncoderPool.Put(e)
  89. } else {
  90. if cap(e.s.Buf) > 64<<10 {
  91. e.s.Buf = nil // avoid pinning arbitrarily large amounts of memory
  92. }
  93. streamingEncoderPool.Put(e)
  94. }
  95. }
  96. var (
  97. // This does not own the internal buffer since it is externally provided.
  98. bufferedDecoderPool = &sync.Pool{New: func() any { return new(Decoder) }}
  99. // This owns the internal buffer, but it is only used to temporarily store
  100. // buffered JSON fetched from the underlying io.Reader.
  101. // In a sufficiently efficient streaming mode, we do not expect the buffer
  102. // to grow arbitrarily large. Thus, we avoid recycling large buffers.
  103. streamingDecoderPool = &sync.Pool{New: func() any { return new(Decoder) }}
  104. // This does not own the internal buffer since
  105. // it is taken directly from the provided bytes.Buffer.
  106. bytesBufferDecoderPool = bufferedDecoderPool
  107. )
  108. func getBufferedDecoder(b []byte, opts ...Options) *Decoder {
  109. d := bufferedDecoderPool.Get().(*Decoder)
  110. d.s.reset(b, nil, opts...)
  111. return d
  112. }
  113. func putBufferedDecoder(d *Decoder) {
  114. bufferedDecoderPool.Put(d)
  115. }
  116. func getStreamingDecoder(r io.Reader, opts ...Options) *Decoder {
  117. if _, ok := r.(*bytes.Buffer); ok {
  118. d := bytesBufferDecoderPool.Get().(*Decoder)
  119. d.s.reset(nil, r, opts...) // buffer taken from bytes.Buffer
  120. return d
  121. } else {
  122. d := streamingDecoderPool.Get().(*Decoder)
  123. d.s.reset(d.s.buf[:0], r, opts...) // preserve existing buffer
  124. return d
  125. }
  126. }
  127. func putStreamingDecoder(d *Decoder) {
  128. if _, ok := d.s.rd.(*bytes.Buffer); ok {
  129. bytesBufferDecoderPool.Put(d)
  130. } else {
  131. if cap(d.s.buf) > 64<<10 {
  132. d.s.buf = nil // avoid pinning arbitrarily large amounts of memory
  133. }
  134. streamingDecoderPool.Put(d)
  135. }
  136. }