diff options
author | Rob Pike <r@golang.org> | 2009-12-04 00:26:08 -0800 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2009-12-04 00:26:08 -0800 |
commit | 73254331f0aaa06c43895dfe1369d3d83c8a01ad (patch) | |
tree | d71e28fd708f97291244ec86b991877164eef745 /src/pkg/bytes/buffer.go | |
parent | 6e7cfabc5315cedd84adbd620a1d4a9b19b634db (diff) | |
download | golang-73254331f0aaa06c43895dfe1369d3d83c8a01ad.tar.gz |
avoid an allocation inside bytes.Buffer by providing a static array.
R=rsc
http://codereview.appspot.com/165058
Diffstat (limited to 'src/pkg/bytes/buffer.go')
-rw-r--r-- | src/pkg/bytes/buffer.go | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/src/pkg/bytes/buffer.go b/src/pkg/bytes/buffer.go index 8fa64524c..09202506f 100644 --- a/src/pkg/bytes/buffer.go +++ b/src/pkg/bytes/buffer.go @@ -32,9 +32,9 @@ func copyBytes(dst []byte, doff int, src []byte) { // with Read and Write methods. // The zero value for Buffer is an empty buffer ready to use. type Buffer struct { - buf []byte; // contents are the bytes buf[off : len(buf)] - off int; // read at &buf[off], write at &buf[len(buf)] - oneByte []byte; // avoid allocation of slice on each WriteByte + buf []byte; // contents are the bytes buf[off : len(buf)] + off int; // read at &buf[off], write at &buf[len(buf)] + oneByte [1]byte; // avoid allocation of slice on each WriteByte } // Bytes returns the contents of the unread portion of the buffer; @@ -173,12 +173,8 @@ func (b *Buffer) WriteString(s string) (n int, err os.Error) { // The returned error is always nil, but is included // to match bufio.Writer's WriteByte. func (b *Buffer) WriteByte(c byte) os.Error { - if b.oneByte == nil { - // Only happens once per Buffer, and then we have a slice. - b.oneByte = make([]byte, 1) - } b.oneByte[0] = c; - b.Write(b.oneByte); + b.Write(&b.oneByte); return nil; } |