pool: use bitwise operate instead of binary search

This commit is contained in:
nadoo 2020-08-07 22:41:28 +08:00
parent 2fee24995a
commit 3c509f8b7a

View File

@ -1,13 +1,15 @@
package pool package pool
import ( import (
"sort" "math/bits"
"sync" "sync"
) )
// number of pools. const (
// pool sizes: 1<<0 ~ 1<<16 bytes, (1Byte~64KBytes). // number of pools.
const num = 17 num = 17 // pool sizes: 1<<0 ~ 1<<16 bytes, (1Byte~64KBytes).
maxsize = 1 << (num - 1)
)
var ( var (
sizes [num]int sizes [num]int
@ -24,10 +26,13 @@ func init() {
} }
} }
// GetBuffer gets a buffer from pool. // GetBuffer gets a buffer from pool, size should in range: [1, 65536], otherwise, this function will call make([]byte, size) directly.
func GetBuffer(size int) []byte { func GetBuffer(size int) []byte {
i := sort.Search(num, func(i int) bool { return sizes[i] >= size }) if size >= 1 && size < maxsize {
if i < num { i := bits.Len32(uint32(size)) - 1
if sizes[i] < size {
i += 1
}
return pools[i].Get().([]byte)[:size] return pools[i].Get().([]byte)[:size]
} }
return make([]byte, size) return make([]byte, size)
@ -36,7 +41,7 @@ func GetBuffer(size int) []byte {
// PutBuffer puts a buffer into pool. // PutBuffer puts a buffer into pool.
func PutBuffer(buf []byte) { func PutBuffer(buf []byte) {
size := cap(buf) size := cap(buf)
i := sort.Search(num, func(i int) bool { return sizes[i] >= size }) i := bits.Len32(uint32(size)) - 1
if i < num && sizes[i] == size { if i < num && sizes[i] == size {
pools[i].Put(buf) pools[i].Put(buf)
} }