try use pool with channel

This commit is contained in:
gobwas 2016-02-24 19:38:05 +03:00
parent 72f082815c
commit 6dc0cef691
1 changed files with 72 additions and 5 deletions

View File

@ -4,7 +4,14 @@ import (
"sync" "sync"
) )
var segmentsPools [1024]*sync.Pool type SomePool interface {
Get() []int
Put([]int)
}
var segmentsPools [1024]SomePool
//var segmentsPools [1024]*sync.Pool
func toPowerOfTwo(v int) int { func toPowerOfTwo(v int) int {
v-- v--
@ -49,9 +56,12 @@ const (
func init() { func init() {
for i := cacheToAndHigher; i >= cacheFrom; i >>= 1 { for i := cacheToAndHigher; i >= cacheFrom; i >>= 1 {
func(i int) { func(i int) {
segmentsPools[i-1] = &sync.Pool{New: func() interface{} { // segmentsPools[i-1] = &sync.Pool{New: func() interface{} {
// return make([]int, 0, i)
// }}
segmentsPools[i-1] = newChanPool(func() []int {
return make([]int, 0, i) return make([]int, 0, i)
}} })
}(i) }(i)
} }
} }
@ -75,7 +85,8 @@ func acquireSegments(c int) []int {
return make([]int, 0, c) return make([]int, 0, c)
} }
return segmentsPools[getTableIndex(c)].Get().([]int)[:0] // return segmentsPools[getTableIndex(c)].Get().([]int)[:0]
return segmentsPools[getTableIndex(c)].Get()
} }
func releaseSegments(s []int) { func releaseSegments(s []int) {
@ -87,5 +98,61 @@ func releaseSegments(s []int) {
return return
} }
segmentsPools[getTableIndex(cap(s))].Put(s) segmentsPools[getTableIndex(c)].Put(s)
}
type maker func() []int
type syncPool struct {
new maker
pool sync.Pool
}
func newSyncPool(m maker) *syncPool {
return &syncPool{
new: m,
pool: sync.Pool{New: func() interface{} {
return m()
}},
}
}
func (s *syncPool) Get() []int {
return s.pool.Get().([]int)[:0]
}
func (s *syncPool) Put(x []int) {
s.pool.Put(x)
}
type chanPool struct {
pool chan []int
new maker
index int
}
func newChanPool(m maker) *chanPool {
return &chanPool{
pool: make(chan []int, 32),
new: m,
}
}
func (c *chanPool) Get() []int {
select {
case s := <-c.pool:
return s[:0]
default:
// pool is empty
return c.new()
}
}
func (c *chanPool) Put(s []int) {
select {
case c.pool <- s:
// ok
default:
// pool is full
}
} }