mirror of https://github.com/v2ray/v2ray-core
grow buffer size slower
parent
a42a1cba47
commit
4c7c42d39f
|
@ -18,11 +18,14 @@ func NewBytesToBufferReader(reader io.Reader) Reader {
|
|||
}
|
||||
}
|
||||
|
||||
const mediumSize = 8 * 1024
|
||||
const largeSize = 64 * 1024
|
||||
|
||||
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
|
||||
b := New()
|
||||
err := b.Reset(ReadFrom(r.Reader))
|
||||
if b.IsFull() {
|
||||
r.buffer = make([]byte, 32*1024)
|
||||
r.buffer = make([]byte, mediumSize)
|
||||
}
|
||||
if !b.IsEmpty() {
|
||||
return NewMultiBufferValue(b), nil
|
||||
|
@ -41,6 +44,9 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
|
|||
if nBytes > 0 {
|
||||
mb := NewMultiBufferCap(nBytes/Size + 1)
|
||||
mb.Write(r.buffer[:nBytes])
|
||||
if nBytes == len(r.buffer) && len(r.buffer) == mediumSize {
|
||||
r.buffer = make([]byte, largeSize)
|
||||
}
|
||||
return mb, nil
|
||||
}
|
||||
return nil, err
|
||||
|
|
|
@ -21,7 +21,11 @@ func TestAdaptiveReader(t *testing.T) {
|
|||
|
||||
b, err = reader.ReadMultiBuffer()
|
||||
assert(err, IsNil)
|
||||
assert(b.Len(), Equals, 32*1024)
|
||||
assert(b.Len(), Equals, 8*1024)
|
||||
|
||||
b, err = reader.ReadMultiBuffer()
|
||||
assert(err, IsNil)
|
||||
assert(b.Len(), Equals, 64*1024)
|
||||
}
|
||||
|
||||
func TestBytesReaderWriteTo(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue