1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 23:47:07 -05:00

Use large buffer if download size is huge

This commit is contained in:
V2Ray 2015-10-11 11:43:31 +02:00
parent a80093a727
commit 2a85c62e14
2 changed files with 11 additions and 2 deletions

View File

@ -113,7 +113,7 @@ func (p *bufferPool) cleanup(tick <-chan time.Time) {
var smallPool = newBufferPool(1024, 16, 64)
var mediumPool = newBufferPool(8*1024, 256, 2048)
var largePool = newBufferPool(64*1024, 16, 64)
var largePool = newBufferPool(64*1024, 128, 1024)
func NewSmallBuffer() *Buffer {
return smallPool.allocate()

View File

@ -17,8 +17,10 @@ func ReadFrom(reader io.Reader, buffer *alloc.Buffer) (*alloc.Buffer, error) {
// ReaderToChan dumps all content from a given reader to a chan by constantly reading it until EOF.
func ReaderToChan(stream chan<- *alloc.Buffer, reader io.Reader) error {
allocate := alloc.NewBuffer
large := false
for {
buffer, err := ReadFrom(reader, nil)
buffer, err := ReadFrom(reader, allocate())
if buffer.Len() > 0 {
stream <- buffer
} else {
@ -27,6 +29,13 @@ func ReaderToChan(stream chan<- *alloc.Buffer, reader io.Reader) error {
if err != nil {
return err
}
if buffer.IsFull() && !large {
allocate = alloc.NewLargeBuffer
large = true
} else if !buffer.IsFull() {
allocate = alloc.NewBuffer
large = false
}
}
}