alloc.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package pool
  2. // Inspired by https://github.com/xtaci/smux/blob/master/alloc.go
  3. import (
  4. "errors"
  5. "math/bits"
  6. "sync"
  7. )
  8. var defaultAllocator = NewAllocator()
  9. // Allocator for incoming frames, optimized to prevent overwriting after zeroing
  10. type Allocator struct {
  11. buffers [11]sync.Pool
  12. }
  13. // NewAllocator initiates a []byte allocator for frames less than 65536 bytes,
  14. // the waste(memory fragmentation) of space allocation is guaranteed to be
  15. // no more than 50%.
  16. func NewAllocator() *Allocator {
  17. return &Allocator{
  18. buffers: [...]sync.Pool{ // 64B -> 64K
  19. {New: func() any { return new([1 << 6]byte) }},
  20. {New: func() any { return new([1 << 7]byte) }},
  21. {New: func() any { return new([1 << 8]byte) }},
  22. {New: func() any { return new([1 << 9]byte) }},
  23. {New: func() any { return new([1 << 10]byte) }},
  24. {New: func() any { return new([1 << 11]byte) }},
  25. {New: func() any { return new([1 << 12]byte) }},
  26. {New: func() any { return new([1 << 13]byte) }},
  27. {New: func() any { return new([1 << 14]byte) }},
  28. {New: func() any { return new([1 << 15]byte) }},
  29. {New: func() any { return new([1 << 16]byte) }},
  30. },
  31. }
  32. }
  33. // Get a []byte from pool with most appropriate cap
  34. func (alloc *Allocator) Get(size int) []byte {
  35. switch {
  36. case size < 0:
  37. panic("alloc.Get: len out of range")
  38. case size == 0:
  39. return nil
  40. case size > 65536:
  41. return make([]byte, size)
  42. default:
  43. var index uint16
  44. if size > 64 {
  45. index = msb(size)
  46. if size != 1<<index {
  47. index += 1
  48. }
  49. index -= 6
  50. }
  51. buffer := alloc.buffers[index].Get()
  52. switch index {
  53. case 0:
  54. return buffer.(*[1 << 6]byte)[:size]
  55. case 1:
  56. return buffer.(*[1 << 7]byte)[:size]
  57. case 2:
  58. return buffer.(*[1 << 8]byte)[:size]
  59. case 3:
  60. return buffer.(*[1 << 9]byte)[:size]
  61. case 4:
  62. return buffer.(*[1 << 10]byte)[:size]
  63. case 5:
  64. return buffer.(*[1 << 11]byte)[:size]
  65. case 6:
  66. return buffer.(*[1 << 12]byte)[:size]
  67. case 7:
  68. return buffer.(*[1 << 13]byte)[:size]
  69. case 8:
  70. return buffer.(*[1 << 14]byte)[:size]
  71. case 9:
  72. return buffer.(*[1 << 15]byte)[:size]
  73. case 10:
  74. return buffer.(*[1 << 16]byte)[:size]
  75. default:
  76. panic("invalid pool index")
  77. }
  78. }
  79. }
  80. // Put returns a []byte to pool for future use,
  81. // which the cap must be exactly 2^n
  82. func (alloc *Allocator) Put(buf []byte) error {
  83. if cap(buf) == 0 || cap(buf) > 65536 {
  84. return nil
  85. }
  86. bits := msb(cap(buf))
  87. if cap(buf) != 1<<bits {
  88. return errors.New("allocator Put() incorrect buffer size")
  89. }
  90. if cap(buf) < 1<<6 {
  91. return nil
  92. }
  93. bits -= 6
  94. buf = buf[:cap(buf)]
  95. //nolint
  96. //lint:ignore SA6002 ignore temporarily
  97. switch bits {
  98. case 0:
  99. alloc.buffers[bits].Put((*[1 << 6]byte)(buf))
  100. case 1:
  101. alloc.buffers[bits].Put((*[1 << 7]byte)(buf))
  102. case 2:
  103. alloc.buffers[bits].Put((*[1 << 8]byte)(buf))
  104. case 3:
  105. alloc.buffers[bits].Put((*[1 << 9]byte)(buf))
  106. case 4:
  107. alloc.buffers[bits].Put((*[1 << 10]byte)(buf))
  108. case 5:
  109. alloc.buffers[bits].Put((*[1 << 11]byte)(buf))
  110. case 6:
  111. alloc.buffers[bits].Put((*[1 << 12]byte)(buf))
  112. case 7:
  113. alloc.buffers[bits].Put((*[1 << 13]byte)(buf))
  114. case 8:
  115. alloc.buffers[bits].Put((*[1 << 14]byte)(buf))
  116. case 9:
  117. alloc.buffers[bits].Put((*[1 << 15]byte)(buf))
  118. case 10:
  119. alloc.buffers[bits].Put((*[1 << 16]byte)(buf))
  120. default:
  121. panic("invalid pool index")
  122. }
  123. return nil
  124. }
  125. // msb return the pos of most significant bit
  126. func msb(size int) uint16 {
  127. return uint16(bits.Len32(uint32(size)) - 1)
  128. }