pool.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package common
  2. import (
  3. "bytes"
  4. "sync"
  5. )
  6. const PoolSize = 64 * 1024
  7. const PoolSizeSmall = 100
  8. const PoolSizeUdp = 1472 + 200
  9. const PoolSizeCopy = 32 << 10
  10. const PoolSizeBuffer = 4096
  11. const PoolSizeWindow = PoolSizeBuffer - 2 - 4 - 4 - 1
  12. var BufPool = sync.Pool{
  13. New: func() interface{} {
  14. return make([]byte, PoolSize)
  15. },
  16. }
  17. var BufPoolUdp = sync.Pool{
  18. New: func() interface{} {
  19. return make([]byte, PoolSizeUdp)
  20. },
  21. }
  22. var BufPoolMax = sync.Pool{
  23. New: func() interface{} {
  24. return make([]byte, PoolSize)
  25. },
  26. }
  27. var BufPoolSmall = sync.Pool{
  28. New: func() interface{} {
  29. return make([]byte, PoolSizeSmall)
  30. },
  31. }
  32. var BufPoolCopy = sync.Pool{
  33. New: func() interface{} {
  34. return make([]byte, PoolSizeCopy)
  35. },
  36. }
  37. func PutBufPoolUdp(buf []byte) {
  38. if cap(buf) == PoolSizeUdp {
  39. BufPoolUdp.Put(buf[:PoolSizeUdp])
  40. }
  41. }
  42. func PutBufPoolCopy(buf []byte) {
  43. if cap(buf) == PoolSizeCopy {
  44. BufPoolCopy.Put(buf[:PoolSizeCopy])
  45. }
  46. }
  47. func GetBufPoolCopy() []byte {
  48. return (BufPoolCopy.Get().([]byte))[:PoolSizeCopy]
  49. }
  50. func PutBufPoolMax(buf []byte) {
  51. if cap(buf) == PoolSize {
  52. BufPoolMax.Put(buf[:PoolSize])
  53. }
  54. }
  55. type copyBufferPool struct {
  56. pool sync.Pool
  57. }
  58. func (Self *copyBufferPool) New() {
  59. Self.pool = sync.Pool{
  60. New: func() interface{} {
  61. return make([]byte, PoolSizeCopy, PoolSizeCopy)
  62. },
  63. }
  64. }
  65. func (Self *copyBufferPool) Get() []byte {
  66. buf := Self.pool.Get().([]byte)
  67. return buf[:PoolSizeCopy] // just like make a new slice, but data may not be 0
  68. }
  69. func (Self *copyBufferPool) Put(x []byte) {
  70. if len(x) == PoolSizeCopy {
  71. Self.pool.Put(x)
  72. } else {
  73. x = nil // buf is not full, not allowed, New method returns a full buf
  74. }
  75. }
  76. type windowBufferPool struct {
  77. pool sync.Pool
  78. }
  79. func (Self *windowBufferPool) New() {
  80. Self.pool = sync.Pool{
  81. New: func() interface{} {
  82. return make([]byte, PoolSizeWindow)
  83. },
  84. }
  85. }
  86. func (Self *windowBufferPool) Get() (buf []byte) {
  87. buf = Self.pool.Get().([]byte)
  88. buf = buf[:PoolSizeWindow]
  89. return buf
  90. }
  91. func (Self *windowBufferPool) Put(x []byte) {
  92. x = x[:0] // clean buf
  93. Self.pool.Put(x)
  94. }
  95. type bufferPool struct {
  96. pool sync.Pool
  97. }
  98. func (Self *bufferPool) New() {
  99. Self.pool = sync.Pool{
  100. New: func() interface{} {
  101. return bytes.NewBuffer(make([]byte, 0, PoolSizeBuffer))
  102. },
  103. }
  104. }
  105. func (Self *bufferPool) Get() *bytes.Buffer {
  106. return Self.pool.Get().(*bytes.Buffer)
  107. }
  108. func (Self *bufferPool) Put(x *bytes.Buffer) {
  109. x.Reset()
  110. Self.pool.Put(x)
  111. }
  112. type muxPackagerPool struct {
  113. pool sync.Pool
  114. }
  115. func (Self *muxPackagerPool) New() {
  116. Self.pool = sync.Pool{
  117. New: func() interface{} {
  118. pack := MuxPackager{}
  119. return &pack
  120. },
  121. }
  122. }
  123. func (Self *muxPackagerPool) Get() *MuxPackager {
  124. return Self.pool.Get().(*MuxPackager)
  125. }
  126. func (Self *muxPackagerPool) Put(pack *MuxPackager) {
  127. pack.reset()
  128. Self.pool.Put(pack)
  129. }
  130. type ListElement struct {
  131. Buf []byte
  132. L uint16
  133. Part bool
  134. }
  135. type listElementPool struct {
  136. pool sync.Pool
  137. }
  138. func (Self *listElementPool) New() {
  139. Self.pool = sync.Pool{
  140. New: func() interface{} {
  141. element := ListElement{}
  142. return &element
  143. },
  144. }
  145. }
  146. func (Self *listElementPool) Get() *ListElement {
  147. return Self.pool.Get().(*ListElement)
  148. }
  149. func (Self *listElementPool) Put(element *ListElement) {
  150. element.L = 0
  151. element.Buf = nil
  152. element.Part = false
  153. Self.pool.Put(element)
  154. }
  155. var once = sync.Once{}
  156. var BuffPool = bufferPool{}
  157. var CopyBuff = copyBufferPool{}
  158. var MuxPack = muxPackagerPool{}
  159. var WindowBuff = windowBufferPool{}
  160. var ListElementPool = listElementPool{}
  161. func newPool() {
  162. BuffPool.New()
  163. CopyBuff.New()
  164. MuxPack.New()
  165. WindowBuff.New()
  166. ListElementPool.New()
  167. }
  168. func init() {
  169. once.Do(newPool)
  170. }