glob/match/segments.go

162 lines
2.5 KiB
Go
Raw Normal View History

2016-02-22 23:47:31 +03:00
package match
import (
"sync"
)
2016-02-24 19:38:05 +03:00
type SomePool interface {
Get() []int
Put([]int)
}
var segmentsPools [1024]SomePool
//var segmentsPools [1024]*sync.Pool
2016-02-22 23:47:31 +03:00
func toPowerOfTwo(v int) int {
v--
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v++
return v
}
const (
cacheFrom = 16
cacheToAndHigher = 1024
cacheFromIndex = 15
cacheToAndHigherIndex = 1023
2016-02-22 23:47:31 +03:00
)
2016-02-23 14:46:20 +03:00
var (
segments0 = []int{0}
segments1 = []int{1}
segments2 = []int{2}
segments3 = []int{3}
segments4 = []int{4}
)
2016-02-24 11:19:54 +03:00
var segmentsByRuneLength [5][]int = [5][]int{
0: segments0,
1: segments1,
2: segments2,
3: segments3,
4: segments4,
}
2016-02-23 14:46:20 +03:00
const (
asciiLo = 0
asciiHi = 127
)
2016-02-22 23:47:31 +03:00
func init() {
for i := cacheToAndHigher; i >= cacheFrom; i >>= 1 {
2016-02-22 23:47:31 +03:00
func(i int) {
2016-02-24 19:38:05 +03:00
// segmentsPools[i-1] = &sync.Pool{New: func() interface{} {
// return make([]int, 0, i)
// }}
2016-02-24 19:44:33 +03:00
// segmentsPools[i-1] = newChanPool(func() []int {
// return make([]int, 0, i)
// })
segmentsPools[i-1] = newSyncPool(func() []int {
2016-02-22 23:47:31 +03:00
return make([]int, 0, i)
2016-02-24 19:38:05 +03:00
})
2016-02-22 23:47:31 +03:00
}(i)
}
}
func getTableIndex(c int) int {
2016-02-22 23:47:31 +03:00
p := toPowerOfTwo(c)
switch {
case p >= cacheToAndHigher:
return cacheToAndHigherIndex
case p <= cacheFrom:
return cacheFromIndex
2016-02-22 23:47:31 +03:00
default:
return p - 1
}
}
func acquireSegments(c int) []int {
// make []int with less capacity than cacheFrom
// is faster than acquiring it from pool
if c < cacheFrom {
return make([]int, 0, c)
2016-02-22 23:47:31 +03:00
}
2016-02-24 19:38:05 +03:00
// return segmentsPools[getTableIndex(c)].Get().([]int)[:0]
return segmentsPools[getTableIndex(c)].Get()
2016-02-22 23:47:31 +03:00
}
func releaseSegments(s []int) {
c := cap(s)
2016-02-22 23:47:31 +03:00
// make []int with less capacity than cacheFrom
// is faster than acquiring it from pool
if c < cacheFrom {
2016-02-22 23:47:31 +03:00
return
}
2016-02-24 19:38:05 +03:00
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{
2016-02-24 19:44:33 +03:00
pool: make(chan []int, 16),
2016-02-24 19:38:05 +03:00
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
}
2016-02-22 23:47:31 +03:00
}