grow buffer size slower

pull/1712/head
Darien Raymond 2017-12-08 13:23:15 +01:00
parent a42a1cba47
commit 4c7c42d39f
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 12 additions and 2 deletions

View File

@ -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

View File

@ -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) {