Skip to content

Commit a50225a

Browse files
committed
bufio: make Reader.Reset and Writer.Reset work on the zero value
For batch allocation reasons, it would be useful to nest a bufio.Reader or bufio.Writer in a struct as a value, rather than a pointer. When the Reset method is called, have it use the default buffer size if the buffer is nil. Fixes #45374 Change-Id: I80df18a13575431428a42ed150a1579de1282637 Reviewed-on: https://go-review.googlesource.com/c/go/+/345570 Trust: Joe Tsai <[email protected]> Run-TryBot: Joe Tsai <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent cf2fe5d commit a50225a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/bufio/bufio.go

+10
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ func (b *Reader) Size() int { return len(b.buf) }
6868

6969
// Reset discards any buffered data, resets all state, and switches
7070
// the buffered reader to read from r.
71+
// Calling Reset on the zero value of Reader initializes the internal buffer
72+
// to the default size.
7173
func (b *Reader) Reset(r io.Reader) {
74+
if b.buf == nil {
75+
b.buf = make([]byte, defaultBufSize)
76+
}
7277
b.reset(b.buf, r)
7378
}
7479

@@ -590,7 +595,12 @@ func (b *Writer) Size() int { return len(b.buf) }
590595

591596
// Reset discards any unflushed buffered data, clears any error, and
592597
// resets b to write its output to w.
598+
// Calling Reset on the zero value of Writer initializes the internal buffer
599+
// to the default size.
593600
func (b *Writer) Reset(w io.Writer) {
601+
if b.buf == nil {
602+
b.buf = make([]byte, defaultBufSize)
603+
}
594604
b.err = nil
595605
b.n = 0
596606
b.wr = w

src/bufio/bufio_test.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,7 @@ func TestReaderReset(t *testing.T) {
13121312
if string(buf) != "foo" {
13131313
t.Errorf("buf = %q; want foo", buf)
13141314
}
1315+
13151316
r.Reset(strings.NewReader("bar bar"))
13161317
all, err := io.ReadAll(r)
13171318
if err != nil {
@@ -1320,12 +1321,23 @@ func TestReaderReset(t *testing.T) {
13201321
if string(all) != "bar bar" {
13211322
t.Errorf("ReadAll = %q; want bar bar", all)
13221323
}
1324+
1325+
*r = Reader{} // zero out the Reader
1326+
r.Reset(strings.NewReader("bar bar"))
1327+
all, err = io.ReadAll(r)
1328+
if err != nil {
1329+
t.Fatal(err)
1330+
}
1331+
if string(all) != "bar bar" {
1332+
t.Errorf("ReadAll = %q; want bar bar", all)
1333+
}
13231334
}
13241335

13251336
func TestWriterReset(t *testing.T) {
1326-
var buf1, buf2 bytes.Buffer
1337+
var buf1, buf2, buf3 bytes.Buffer
13271338
w := NewWriter(&buf1)
13281339
w.WriteString("foo")
1340+
13291341
w.Reset(&buf2) // and not flushed
13301342
w.WriteString("bar")
13311343
w.Flush()
@@ -1335,6 +1347,17 @@ func TestWriterReset(t *testing.T) {
13351347
if buf2.String() != "bar" {
13361348
t.Errorf("buf2 = %q; want bar", buf2.String())
13371349
}
1350+
1351+
*w = Writer{} // zero out the Writer
1352+
w.Reset(&buf3) // and not flushed
1353+
w.WriteString("bar")
1354+
w.Flush()
1355+
if buf1.String() != "" {
1356+
t.Errorf("buf1 = %q; want empty", buf1.String())
1357+
}
1358+
if buf3.String() != "bar" {
1359+
t.Errorf("buf3 = %q; want bar", buf3.String())
1360+
}
13381361
}
13391362

13401363
func TestReaderDiscard(t *testing.T) {

0 commit comments

Comments
 (0)