syncthing/vendor/github.com/syndtr/goleveldb/leveldb/util/buffer_pool.go

240 lines
4.3 KiB
Go
Raw Normal View History

2014-08-15 07:16:30 +00:00
// Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package util
import (
"fmt"
2014-11-18 12:24:42 +00:00
"sync"
2014-08-15 07:16:30 +00:00
"sync/atomic"
2014-08-19 21:49:03 +00:00
"time"
2014-08-15 07:16:30 +00:00
)
type buffer struct {
b []byte
miss int
}
// BufferPool is a 'buffer pool'.
type BufferPool struct {
2014-11-18 12:24:42 +00:00
pool [6]chan []byte
size [5]uint32
sizeMiss [5]uint32
sizeHalf [5]uint32
baseline [4]int
baseline0 int
mu sync.RWMutex
closed bool
closeC chan struct{}
2014-08-15 07:16:30 +00:00
2014-08-29 10:36:45 +00:00
get uint32
put uint32
2014-09-02 07:43:42 +00:00
half uint32
2014-08-15 07:16:30 +00:00
less uint32
equal uint32
greater uint32
miss uint32
}
func (p *BufferPool) poolNum(n int) int {
2014-09-02 07:43:42 +00:00
if n <= p.baseline0 && n > p.baseline0/2 {
2014-08-15 07:16:30 +00:00
return 0
}
2014-09-02 07:43:42 +00:00
for i, x := range p.baseline {
if n <= x {
return i + 1
}
}
return len(p.baseline) + 1
2014-08-15 07:16:30 +00:00
}
// Get returns buffer with length of n.
func (p *BufferPool) Get(n int) []byte {
2014-10-06 20:07:33 +00:00
if p == nil {
return make([]byte, n)
}
2014-11-18 12:24:42 +00:00
p.mu.RLock()
defer p.mu.RUnlock()
if p.closed {
return make([]byte, n)
}
2014-08-29 10:36:45 +00:00
atomic.AddUint32(&p.get, 1)
2014-08-15 07:16:30 +00:00
poolNum := p.poolNum(n)
pool := p.pool[poolNum]
if poolNum == 0 {
// Fast path.
select {
case b := <-pool:
switch {
case cap(b) > n:
2014-09-02 07:43:42 +00:00
if cap(b)-n >= n {
atomic.AddUint32(&p.half, 1)
select {
case pool <- b:
default:
}
return make([]byte, n)
} else {
atomic.AddUint32(&p.less, 1)
return b[:n]
}
2014-08-15 07:16:30 +00:00
case cap(b) == n:
atomic.AddUint32(&p.equal, 1)
return b[:n]
default:
2014-09-02 07:43:42 +00:00
atomic.AddUint32(&p.greater, 1)
2014-08-15 07:16:30 +00:00
}
default:
atomic.AddUint32(&p.miss, 1)
}
return make([]byte, n, p.baseline0)
} else {
sizePtr := &p.size[poolNum-1]
select {
case b := <-pool:
switch {
case cap(b) > n:
2014-09-02 07:43:42 +00:00
if cap(b)-n >= n {
atomic.AddUint32(&p.half, 1)
sizeHalfPtr := &p.sizeHalf[poolNum-1]
if atomic.AddUint32(sizeHalfPtr, 1) == 20 {
atomic.StoreUint32(sizePtr, uint32(cap(b)/2))
atomic.StoreUint32(sizeHalfPtr, 0)
} else {
select {
case pool <- b:
default:
}
}
return make([]byte, n)
} else {
atomic.AddUint32(&p.less, 1)
return b[:n]
}
2014-08-15 07:16:30 +00:00
case cap(b) == n:
atomic.AddUint32(&p.equal, 1)
return b[:n]
default:
atomic.AddUint32(&p.greater, 1)
if uint32(cap(b)) >= atomic.LoadUint32(sizePtr) {
select {
case pool <- b:
default:
}
}
}
default:
atomic.AddUint32(&p.miss, 1)
}
if size := atomic.LoadUint32(sizePtr); uint32(n) > size {
if size == 0 {
atomic.CompareAndSwapUint32(sizePtr, 0, uint32(n))
} else {
sizeMissPtr := &p.sizeMiss[poolNum-1]
if atomic.AddUint32(sizeMissPtr, 1) == 20 {
atomic.StoreUint32(sizePtr, uint32(n))
atomic.StoreUint32(sizeMissPtr, 0)
}
}
return make([]byte, n)
} else {
return make([]byte, n, size)
}
}
}
// Put adds given buffer to the pool.
func (p *BufferPool) Put(b []byte) {
2014-10-06 20:07:33 +00:00
if p == nil {
return
}
2014-11-18 12:24:42 +00:00
p.mu.RLock()
defer p.mu.RUnlock()
if p.closed {
return
}
2014-08-29 10:36:45 +00:00
atomic.AddUint32(&p.put, 1)
2014-08-15 07:16:30 +00:00
pool := p.pool[p.poolNum(cap(b))]
select {
case pool <- b:
default:
}
}
2014-09-02 07:43:42 +00:00
func (p *BufferPool) Close() {
2014-10-06 20:07:33 +00:00
if p == nil {
return
}
2014-11-18 12:24:42 +00:00
p.mu.Lock()
if !p.closed {
p.closed = true
p.closeC <- struct{}{}
2014-09-02 07:43:42 +00:00
}
2014-11-18 12:24:42 +00:00
p.mu.Unlock()
2014-09-02 07:43:42 +00:00
}
2014-08-15 07:16:30 +00:00
func (p *BufferPool) String() string {
2014-10-06 20:07:33 +00:00
if p == nil {
return "<nil>"
}
2014-09-02 07:43:42 +00:00
return fmt.Sprintf("BufferPool{B·%d Z·%v Zm·%v Zh·%v G·%d P·%d H·%d <·%d =·%d >·%d M·%d}",
p.baseline0, p.size, p.sizeMiss, p.sizeHalf, p.get, p.put, p.half, p.less, p.equal, p.greater, p.miss)
2014-08-15 07:16:30 +00:00
}
2014-08-19 21:49:03 +00:00
func (p *BufferPool) drain() {
2014-09-02 07:43:42 +00:00
ticker := time.NewTicker(2 * time.Second)
2015-08-23 13:57:26 +00:00
defer ticker.Stop()
2014-08-19 21:49:03 +00:00
for {
select {
2014-09-02 07:43:42 +00:00
case <-ticker.C:
for _, ch := range p.pool {
select {
case <-ch:
default:
}
}
2014-11-18 12:24:42 +00:00
case <-p.closeC:
close(p.closeC)
2014-09-02 07:43:42 +00:00
for _, ch := range p.pool {
close(ch)
}
return
2014-08-19 21:49:03 +00:00
}
}
}
2014-08-15 07:16:30 +00:00
// NewBufferPool creates a new initialized 'buffer pool'.
func NewBufferPool(baseline int) *BufferPool {
if baseline <= 0 {
panic("baseline can't be <= 0")
}
p := &BufferPool{
baseline0: baseline,
2014-09-02 07:43:42 +00:00
baseline: [...]int{baseline / 4, baseline / 2, baseline * 2, baseline * 4},
2014-11-18 12:24:42 +00:00
closeC: make(chan struct{}, 1),
2014-08-15 07:16:30 +00:00
}
2014-09-02 07:43:42 +00:00
for i, cap := range []int{2, 2, 4, 4, 2, 1} {
2014-08-15 07:16:30 +00:00
p.pool[i] = make(chan []byte, cap)
}
2014-08-19 21:49:03 +00:00
go p.drain()
2014-08-15 07:16:30 +00:00
return p
}