1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 09:55:22 +00:00

align large size

This commit is contained in:
Darien Raymond 2018-04-01 12:20:32 +02:00
parent 053d03349a
commit b3ecb9f766
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 10 additions and 6 deletions

View File

@ -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 (
numPools = 5
sizeMulti = 4
)
var (
pool [numPools]sync.Pool
poolSize [numPools]uint32
pool [numPools]sync.Pool
poolSize [numPools]uint32
largeSize uint32
)
func init() {
@ -32,6 +37,7 @@ func init() {
New: createAllocFunc(size),
}
poolSize[i] = size
largeSize = size
size *= sizeMulti
}
}

View File

@ -19,8 +19,6 @@ func NewBytesToBufferReader(reader io.Reader) Reader {
}
}
const xlSize = 128 * 1024
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
b := New()
for i := 0; i < 64; i++ {
@ -55,7 +53,7 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
if nBytes > 0 {
mb := NewMultiBufferCap(nBytes/Size + 1)
mb.Write(r.buffer[:nBytes])
if nBytes == len(r.buffer) && nBytes < xlSize {
if nBytes == len(r.buffer) && nBytes < int(largeSize) {
freeBytes(r.buffer)
r.buffer = newBytes(uint32(nBytes) + 1)
} else if nBytes < Size {

View File

@ -33,7 +33,7 @@ func TestAdaptiveReader(t *testing.T) {
b, err = reader.ReadMultiBuffer()
assert(err, IsNil)
assert(b.Len(), Equals, 128*1024)
assert(b.Len(), Equals, 512*1024)
}
func TestBytesReaderWriteTo(t *testing.T) {