Skip to content

Commit

Permalink
stdcopy: improve perf by avoiding buffer growth
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
  • Loading branch information
unclejack committed Sep 17, 2014
1 parent 1f03e60 commit 277aa9c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pkg/stdcopy/stdcopy.go
Expand Up @@ -29,14 +29,22 @@ type StdWriter struct {
}

func (w *StdWriter) Write(buf []byte) (n int, err error) {
var n1, n2 int
if w == nil || w.Writer == nil {
return 0, errors.New("Writer not instanciated")
}
binary.BigEndian.PutUint32(w.prefix[4:], uint32(len(buf)))
buf = append(w.prefix[:], buf...)

n, err = w.Writer.Write(buf)
return n - StdWriterPrefixLen, err
n1, err = w.Writer.Write(w.prefix[:])
if err != nil {
n = n1 - StdWriterPrefixLen
} else {
n2, err = w.Writer.Write(buf)
n = n1 + n2 - StdWriterPrefixLen
}
if n < 0 {
n = 0
}
return
}

// NewStdWriter instanciates a new Writer.
Expand Down

0 comments on commit 277aa9c

Please sign in to comment.