Skip to content

Commit

Permalink
better grow method
Browse files Browse the repository at this point in the history
previously removed the wrong (effizient) branch
  • Loading branch information
julienschmidt committed Jun 3, 2013
1 parent ab769a4 commit 04653f2
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ type buffer struct {
}

func newBuffer(rd io.Reader) *buffer {
var b [defaultBufSize]byte
return &buffer{
buf: make([]byte, defaultBufSize),
buf: b[:],
rd: rd,
}
}
Expand All @@ -38,14 +39,9 @@ func (b *buffer) fill(need int) (err error) {

// grow buffer if necessary
if need > len(b.buf) {
for {
b.buf = append(b.buf, 0)
b.buf = b.buf[:cap(b.buf)]

if cap(b.buf) >= need {
break
}
}
newBuf := make([]byte, need)
copy(newBuf, b.buf)
b.buf = newBuf
}

b.idx = 0
Expand Down

0 comments on commit 04653f2

Please sign in to comment.