stdcopy: improve perf by avoiding buffer growth

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
unclejack 2014-09-17 16:50:56 +03:00
parent c20c59e557
commit 3a38c974cb

View file

@ -29,14 +29,22 @@ type StdWriter struct {
} }
func (w *StdWriter) Write(buf []byte) (n int, err error) { func (w *StdWriter) Write(buf []byte) (n int, err error) {
var n1, n2 int
if w == nil || w.Writer == nil { if w == nil || w.Writer == nil {
return 0, errors.New("Writer not instanciated") return 0, errors.New("Writer not instanciated")
} }
binary.BigEndian.PutUint32(w.prefix[4:], uint32(len(buf))) binary.BigEndian.PutUint32(w.prefix[4:], uint32(len(buf)))
buf = append(w.prefix[:], buf...) n1, err = w.Writer.Write(w.prefix[:])
if err != nil {
n, err = w.Writer.Write(buf) n = n1 - StdWriterPrefixLen
return n - StdWriterPrefixLen, err } else {
n2, err = w.Writer.Write(buf)
n = n1 + n2 - StdWriterPrefixLen
}
if n < 0 {
n = 0
}
return
} }
// NewStdWriter instanciates a new Writer. // NewStdWriter instanciates a new Writer.