From 7911965fe922a4412aa5e56eb32a2a15c67e67b1 Mon Sep 17 00:00:00 2001 From: Mike Solomon Date: Wed, 20 Jul 2011 14:34:49 -0700 Subject: [PATCH] io/ioutil: improve performance of ioutil.Discard Fixes #2084. R=bradfitz, rsc CC=golang-dev https://golang.org/cl/4817041 --- src/pkg/io/ioutil/ioutil.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/pkg/io/ioutil/ioutil.go b/src/pkg/io/ioutil/ioutil.go index f79bf87f5cc..fffa1320f59 100644 --- a/src/pkg/io/ioutil/ioutil.go +++ b/src/pkg/io/ioutil/ioutil.go @@ -108,6 +108,23 @@ func (devNull) Write(p []byte) (int, os.Error) { return len(p), nil } +var blackHole = make([]byte, 8192) + +func (devNull) ReadFrom(r io.Reader) (n int64, err os.Error) { + readSize := 0 + for { + readSize, err = r.Read(blackHole) + n += int64(readSize) + if err != nil { + if err == os.EOF { + return n, nil + } + return + } + } + panic("unreachable") +} + // Discard is an io.Writer on which all Write calls succeed // without doing anything. var Discard io.Writer = devNull(0)