1
0

pool.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, PoolSizeWindow)
  83. },
  84. }
  85. }
  86. func (Self *windowBufferPool) Get() (buf []byte) {
  87. buf = Self.pool.Get().([]byte)
  88. return buf[:PoolSizeWindow]
  89. }
  90. func (Self *windowBufferPool) Put(x []byte) {
  91. Self.pool.Put(x[:PoolSizeWindow]) // make buf to full
  92. }
  93. type bufferPool struct {
  94. pool sync.Pool
  95. }
  96. func (Self *bufferPool) New() {
  97. Self.pool = sync.Pool{
  98. New: func() interface{} {
  99. return bytes.NewBuffer(make([]byte, 0, PoolSizeBuffer))
  100. },
  101. }
  102. }
  103. func (Self *bufferPool) Get() *bytes.Buffer {
  104. return Self.pool.Get().(*bytes.Buffer)
  105. }
  106. func (Self *bufferPool) Put(x *bytes.Buffer) {
  107. x.Reset()
  108. Self.pool.Put(x)
  109. }
  110. type muxPackagerPool struct {
  111. pool sync.Pool
  112. }
  113. func (Self *muxPackagerPool) New() {
  114. Self.pool = sync.Pool{
  115. New: func() interface{} {
  116. pack := MuxPackager{}
  117. return &pack
  118. },
  119. }
  120. }
  121. func (Self *muxPackagerPool) Get() *MuxPackager {
  122. return Self.pool.Get().(*MuxPackager)
  123. }
  124. func (Self *muxPackagerPool) Put(pack *MuxPackager) {
  125. Self.pool.Put(pack)
  126. }
  127. type ListElement struct {
  128. Buf []byte
  129. L uint16
  130. Part bool
  131. }
  132. type listElementPool struct {
  133. pool sync.Pool
  134. }
  135. func (Self *listElementPool) New() {
  136. Self.pool = sync.Pool{
  137. New: func() interface{} {
  138. element := ListElement{}
  139. return &element
  140. },
  141. }
  142. }
  143. func (Self *listElementPool) Get() *ListElement {
  144. return Self.pool.Get().(*ListElement)
  145. }
  146. func (Self *listElementPool) Put(element *ListElement) {
  147. element.L = 0
  148. element.Buf = nil
  149. element.Part = false
  150. Self.pool.Put(element)
  151. }
  152. var once = sync.Once{}
  153. var BuffPool = bufferPool{}
  154. var CopyBuff = copyBufferPool{}
  155. var MuxPack = muxPackagerPool{}
  156. var WindowBuff = windowBufferPool{}
  157. var ListElementPool = listElementPool{}
  158. func newPool() {
  159. BuffPool.New()
  160. CopyBuff.New()
  161. MuxPack.New()
  162. WindowBuff.New()
  163. ListElementPool.New()
  164. }
  165. func init() {
  166. once.Do(newPool)
  167. }