pool: use binary search to find proper index

This commit is contained in:
nadoo 2020-08-07 12:07:30 +08:00
parent 382f9cc519
commit 2fee24995a
3 changed files with 18 additions and 18 deletions

View File

@ -1,14 +1,17 @@
package pool
import (
"sort"
"sync"
)
const num = 17 // number of pools, pool size range: 1<<0 ~ 1<<16 bytes. (1Byte~64KBytes)
// number of pools.
// pool sizes: 1<<0 ~ 1<<16 bytes, (1Byte~64KBytes).
const num = 17
var (
pools [num]sync.Pool
sizes [num]int
pools [num]sync.Pool
)
func init() {
@ -23,21 +26,18 @@ func init() {
// GetBuffer gets a buffer from pool.
func GetBuffer(size int) []byte {
for i := 0; i < num; i++ {
if size <= sizes[i] {
return pools[i].Get().([]byte)[:size]
}
i := sort.Search(num, func(i int) bool { return sizes[i] >= size })
if i < num {
return pools[i].Get().([]byte)[:size]
}
return make([]byte, size)
}
// PutBuffer puts a buffer into pool.
func PutBuffer(buf []byte) {
c := cap(buf)
for i := 0; i < num; i++ {
if c == sizes[i] {
pools[i].Put(buf)
return
}
size := cap(buf)
i := sort.Search(num, func(i int) bool { return sizes[i] >= size })
if i < num && sizes[i] == size {
pools[i].Put(buf)
}
}

4
go.mod
View File

@ -15,8 +15,8 @@ require (
github.com/xtaci/kcp-go/v5 v5.5.14
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
golang.org/x/sys v0.0.0-20200806060901-a37d78b92225 // indirect
golang.org/x/tools v0.0.0-20200806022845-90696ccdc692 // indirect
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82 // indirect
golang.org/x/tools v0.0.0-20200806234136-990129eca547 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)

8
go.sum
View File

@ -111,14 +111,14 @@ golang.org/x/sys v0.0.0-20191020212454-3e7259c5e7c2/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200806060901-a37d78b92225 h1:a5kp7Ohh+lqGCGHUBQdPwGHTJXKNhVVWp34F+ncDC9M=
golang.org/x/sys v0.0.0-20200806060901-a37d78b92225/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82 h1:6cBnXxYO+CiRVrChvCosSv7magqTPbyAgz1M8iOv5wM=
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200425043458-8463f397d07c h1:iHhCR0b26amDCiiO+kBguKZom9aMF+NrFxh9zeKR/XU=
golang.org/x/tools v0.0.0-20200425043458-8463f397d07c/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200806022845-90696ccdc692 h1:fsn47thVa7Ar/TMyXYlZgOoT7M4+kRpb+KpSAqRQx1w=
golang.org/x/tools v0.0.0-20200806022845-90696ccdc692/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200806234136-990129eca547 h1:HjPd370wmFEFsBBdDEikoje9tDY3OHE7UB2erLJBmss=
golang.org/x/tools v0.0.0-20200806234136-990129eca547/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=