diff options
author | Rob Pike <r@golang.org> | 2009-12-14 13:13:01 +1100 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2009-12-14 13:13:01 +1100 |
commit | efa170114c56247e155f10284e0f5a8cce81fd08 (patch) | |
tree | f929e56f5b5f328a8c1458feabeca60702eaf90a /src/pkg/bytes/buffer.go | |
parent | 1c301697c70a2ba18fabd239115789bce9f3be02 (diff) | |
download | golang-efa170114c56247e155f10284e0f5a8cce81fd08.tar.gz |
When the buffer is empty, reset b.off to the beginning of the buffer
to avoid growing unnecessarily.
R=rsc
CC=golang-dev
http://codereview.appspot.com/176071
Diffstat (limited to 'src/pkg/bytes/buffer.go')
-rw-r--r-- | src/pkg/bytes/buffer.go | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/pkg/bytes/buffer.go b/src/pkg/bytes/buffer.go index 41838d490..b302b65fa 100644 --- a/src/pkg/bytes/buffer.go +++ b/src/pkg/bytes/buffer.go @@ -84,6 +84,10 @@ func (b *Buffer) resize(n int) { // value n is the length of p; err is always nil. func (b *Buffer) Write(p []byte) (n int, err os.Error) { m := b.Len(); + // If buffer is empty, reset to recover space. + if m == 0 && b.off != 0 { + b.Truncate(0) + } n = len(p); if len(b.buf)+n > cap(b.buf) { b.resize(n) @@ -97,6 +101,10 @@ func (b *Buffer) Write(p []byte) (n int, err os.Error) { // value n is the length of s; err is always nil. func (b *Buffer) WriteString(s string) (n int, err os.Error) { m := b.Len(); + // If buffer is empty, reset to recover space. + if m == 0 && b.off != 0 { + b.Truncate(0) + } n = len(s); if len(b.buf)+n > cap(b.buf) { b.resize(n) @@ -117,6 +125,10 @@ const MinRead = 512 // Any error except os.EOF encountered during the read // is also returned. func (b *Buffer) ReadFrom(r io.Reader) (n int64, err os.Error) { + // If buffer is empty, reset to recover space. + if b.off >= len(b.buf) { + b.Truncate(0) + } for { if cap(b.buf)-len(b.buf) < MinRead { var newBuf []byte; @@ -157,6 +169,8 @@ func (b *Buffer) WriteTo(w io.Writer) (n int64, err os.Error) { return n, e } } + // Buffer is now empty; reset. + b.Truncate(0); return; } @@ -175,7 +189,9 @@ func (b *Buffer) WriteByte(c byte) os.Error { // otherwise it is nil. func (b *Buffer) Read(p []byte) (n int, err os.Error) { if b.off >= len(b.buf) { - return 0, os.EOF + // Buffer is empty, reset to recover space. + b.Truncate(0); + return 0, os.EOF; } m := b.Len(); n = len(p); @@ -194,7 +210,9 @@ func (b *Buffer) Read(p []byte) (n int, err os.Error) { // If no byte is available, it returns error os.EOF. func (b *Buffer) ReadByte() (c byte, err os.Error) { if b.off >= len(b.buf) { - return 0, os.EOF + // Buffer is empty, reset to recover space. + b.Truncate(0); + return 0, os.EOF; } c = b.buf[b.off]; b.off++; |