Browse Source

Merge pull request #7602 from unclejack/tarsum_garbage_reduction

pkg/tarsum: avoid buf2 allocation in Read
Vincent Batts 11 years ago
parent
commit
487af281bf
1 changed files with 7 additions and 1 deletions
  1. 7 1
      pkg/tarsum/tarsum.go

+ 7 - 1
pkg/tarsum/tarsum.go

@@ -23,6 +23,7 @@ type TarSum struct {
 	gz                 writeCloseFlusher
 	gz                 writeCloseFlusher
 	bufTar             *bytes.Buffer
 	bufTar             *bytes.Buffer
 	bufGz              *bytes.Buffer
 	bufGz              *bytes.Buffer
+	bufData            [8192]byte
 	h                  hash.Hash
 	h                  hash.Hash
 	sums               map[string]string
 	sums               map[string]string
 	currentFile        string
 	currentFile        string
@@ -92,7 +93,12 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
 	if ts.finished {
 	if ts.finished {
 		return ts.bufGz.Read(buf)
 		return ts.bufGz.Read(buf)
 	}
 	}
-	buf2 := make([]byte, len(buf), cap(buf))
+	var buf2 []byte
+	if len(buf) > 8192 {
+		buf2 = make([]byte, len(buf), cap(buf))
+	} else {
+		buf2 = ts.bufData[:len(buf)-1]
+	}
 
 
 	n, err := ts.tarR.Read(buf2)
 	n, err := ts.tarR.Read(buf2)
 	if err != nil {
 	if err != nil {