Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(524)

Unified Diff: src/pkg/bufio/bufio.go

Issue 6842078: code review 6842078: bufio: Optimize (*Writer).ReadFrom when the buffer is n... (Closed)
Patch Set: diff -r 1315abc581ed https://code.google.com/p/go/ Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/pkg/bufio/bufio_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pkg/bufio/bufio.go
===================================================================
--- a/src/pkg/bufio/bufio.go
+++ b/src/pkg/bufio/bufio.go
@@ -569,32 +569,42 @@
// ReadFrom implements io.ReaderFrom.
func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) {
- if b.Buffered() == 0 {
- if w, ok := b.wr.(io.ReaderFrom); ok {
- return w.ReadFrom(r)
- }
- }
- var m int
- for {
- m, err = r.Read(b.buf[b.n:])
- if m == 0 {
- break
- }
+ fillFlush := func() error {
bradfitz 2012/11/26 02:58:22 I'd make this be a (private) method on *Writer ins
+ m, err := io.ReadFull(r, b.buf[b.n:])
b.n += m
n += int64(m)
if b.Available() == 0 {
if err1 := b.Flush(); err1 != nil {
- return n, err1
+ return err1
}
}
- if err != nil {
- break
+ return err
+ }
+ if w, ok := b.wr.(io.ReaderFrom); ok {
+ // Want to call Write with a full buffer, so
+ // if the buffer has data in it, fill it then use ReadFrom instead
+ // of flushing immediately
+ if b.Buffered() != 0 {
+ err = fillFlush()
+ }
+ if err == nil {
+ var m int64
+ m, err = w.ReadFrom(r)
+ return n + m, err
+ }
+ } else {
+ for {
+ if err = fillFlush(); err != nil {
+ break
+ }
}
}
- if err == io.EOF {
+ // Don't care that we didn't read the entire buf, so
+ // unexpected EOF should be treated the same as EOF
+ if err == io.EOF || err == io.ErrUnexpectedEOF {
err = nil
}
- return n, err
+ return
}
// buffered input and output
« no previous file with comments | « no previous file | src/pkg/bufio/bufio_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b