Skip to content

Commit 04653f2

Browse files
committed
better grow method
previously removed the wrong (effizient) branch
1 parent ab769a4 commit 04653f2

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

buffer.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ type buffer struct {
2323
}
2424

2525
func newBuffer(rd io.Reader) *buffer {
26+
var b [defaultBufSize]byte
2627
return &buffer{
27-
buf: make([]byte, defaultBufSize),
28+
buf: b[:],
2829
rd: rd,
2930
}
3031
}
@@ -38,14 +39,9 @@ func (b *buffer) fill(need int) (err error) {
3839

3940
// grow buffer if necessary
4041
if need > len(b.buf) {
41-
for {
42-
b.buf = append(b.buf, 0)
43-
b.buf = b.buf[:cap(b.buf)]
44-
45-
if cap(b.buf) >= need {
46-
break
47-
}
48-
}
42+
newBuf := make([]byte, need)
43+
copy(newBuf, b.buf)
44+
b.buf = newBuf
4945
}
5046

5147
b.idx = 0

0 commit comments

Comments
 (0)