1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

remove unnecessary cleanup routine

This commit is contained in:
v2ray 2016-01-09 20:36:23 +01:00
parent ca71c0300f
commit 89175198ee

View File

@ -1,9 +1,5 @@
package alloc package alloc
import (
"time"
)
// Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
// the buffer into an internal buffer pool, in order to recreate a buffer more // the buffer into an internal buffer pool, in order to recreate a buffer more
// quickly. // quickly.
@ -78,16 +74,14 @@ type bufferPool struct {
buffers2Keep int buffers2Keep int
} }
func newBufferPool(bufferSize, buffers2Keep, poolSize int) *bufferPool { func newBufferPool(bufferSize, poolSize int) *bufferPool {
pool := &bufferPool{ pool := &bufferPool{
chain: make(chan []byte, poolSize), chain: make(chan []byte, poolSize),
bufferSize: bufferSize, bufferSize: bufferSize,
buffers2Keep: buffers2Keep,
} }
for i := 0; i < buffers2Keep; i++ { for i := 0; i < poolSize; i++ {
pool.chain <- make([]byte, bufferSize) pool.chain <- make([]byte, bufferSize)
} }
go pool.cleanup(time.Tick(1 * time.Second))
return pool return pool
} }
@ -112,26 +106,9 @@ func (p *bufferPool) free(buffer *Buffer) {
} }
} }
func (p *bufferPool) cleanup(tick <-chan time.Time) { var smallPool = newBufferPool(1024, 256)
for range tick { var mediumPool = newBufferPool(8*1024, 512)
pSize := len(p.chain) var largePool = newBufferPool(64*1024, 128)
if pSize > p.buffers2Keep {
<-p.chain
continue
}
for delta := p.buffers2Keep - pSize; delta > 0; delta-- {
select {
case p.chain <- make([]byte, p.bufferSize):
default:
break
}
}
}
}
var smallPool = newBufferPool(1024, 64, 512)
var mediumPool = newBufferPool(8*1024, 256, 2048)
var largePool = newBufferPool(64*1024, 128, 1024)
// NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
func NewSmallBuffer() *Buffer { func NewSmallBuffer() *Buffer {