mirror of https://github.com/v2ray/v2ray-core
align large size
parent
053d03349a
commit
b3ecb9f766
|
@ -15,14 +15,19 @@ func createAllocFunc(size uint32) func() interface{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The following parameters controls the size of buffer pools.
|
||||||
|
// There are numPools pools. Starting from 2k size, the size of each pool is sizeMulti of the previous one.
|
||||||
|
// Package buf is guaranteed to not use buffers larger than the largest pool.
|
||||||
|
// Other packets may use larger buffers.
|
||||||
const (
|
const (
|
||||||
numPools = 5
|
numPools = 5
|
||||||
sizeMulti = 4
|
sizeMulti = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
pool [numPools]sync.Pool
|
pool [numPools]sync.Pool
|
||||||
poolSize [numPools]uint32
|
poolSize [numPools]uint32
|
||||||
|
largeSize uint32
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -32,6 +37,7 @@ func init() {
|
||||||
New: createAllocFunc(size),
|
New: createAllocFunc(size),
|
||||||
}
|
}
|
||||||
poolSize[i] = size
|
poolSize[i] = size
|
||||||
|
largeSize = size
|
||||||
size *= sizeMulti
|
size *= sizeMulti
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,6 @@ func NewBytesToBufferReader(reader io.Reader) Reader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const xlSize = 128 * 1024
|
|
||||||
|
|
||||||
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
|
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
|
||||||
b := New()
|
b := New()
|
||||||
for i := 0; i < 64; i++ {
|
for i := 0; i < 64; i++ {
|
||||||
|
@ -55,7 +53,7 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
|
||||||
if nBytes > 0 {
|
if nBytes > 0 {
|
||||||
mb := NewMultiBufferCap(nBytes/Size + 1)
|
mb := NewMultiBufferCap(nBytes/Size + 1)
|
||||||
mb.Write(r.buffer[:nBytes])
|
mb.Write(r.buffer[:nBytes])
|
||||||
if nBytes == len(r.buffer) && nBytes < xlSize {
|
if nBytes == len(r.buffer) && nBytes < int(largeSize) {
|
||||||
freeBytes(r.buffer)
|
freeBytes(r.buffer)
|
||||||
r.buffer = newBytes(uint32(nBytes) + 1)
|
r.buffer = newBytes(uint32(nBytes) + 1)
|
||||||
} else if nBytes < Size {
|
} else if nBytes < Size {
|
||||||
|
|
|
@ -33,7 +33,7 @@ func TestAdaptiveReader(t *testing.T) {
|
||||||
|
|
||||||
b, err = reader.ReadMultiBuffer()
|
b, err = reader.ReadMultiBuffer()
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
assert(b.Len(), Equals, 128*1024)
|
assert(b.Len(), Equals, 512*1024)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBytesReaderWriteTo(t *testing.T) {
|
func TestBytesReaderWriteTo(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue