From 2a85c62e141fda1fde7b136b6a83fc7e156127da Mon Sep 17 00:00:00 2001 From: V2Ray Date: Sun, 11 Oct 2015 11:43:31 +0200 Subject: [PATCH] Use large buffer if download size is huge --- common/alloc/buffer.go | 2 +- common/net/transport.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index a1edf1705..5dcc766e5 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -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() diff --git a/common/net/transport.go b/common/net/transport.go index d6cc80fb0..99c6e64a8 100644 --- a/common/net/transport.go +++ b/common/net/transport.go @@ -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 + } } }