1
0
mirror of https://github.com/golang/go synced 2024-11-25 16:57:58 -07:00

avoid an allocation inside bytes.Buffer by providing a static array.

R=rsc
https://golang.org/cl/165058
This commit is contained in:
Rob Pike 2009-12-04 00:26:08 -08:00
parent f2c7a20142
commit 4ed57173b4

View File

@ -34,7 +34,7 @@ func copyBytes(dst []byte, doff int, src []byte) {
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
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;
}