pgreco / rpms / golang

Forked from rpms/golang 5 years ago
Clone

Blame SOURCES/go1.3-tar_reuse_buffer_readHeader.patch

9b5743
# HG changeset patch
9b5743
# User Cristian Staretu <unclejacksons@gmail.com>
9b5743
# Date 1404344479 -36000
9b5743
#      Thu Jul 03 09:41:19 2014 +1000
9b5743
# Node ID 17404efd6b02d4b3acd17070e3f89de97a145877
9b5743
# Parent  837348e418f33fc7a242f56dbe2feff829532526
9b5743
archive/tar: reuse temporary buffer in readHeader
9b5743
9b5743
A temporary 512 bytes buffer is allocated for every call to
9b5743
readHeader. This buffer isn't returned to the caller and it could
9b5743
be reused to lower the number of memory allocations.
9b5743
9b5743
This CL improves it by using a pool and zeroing out the buffer before
9b5743
putting it back into the pool.
9b5743
9b5743
benchmark                  old ns/op     new ns/op     delta
9b5743
BenchmarkListFiles100k     545249903     538832687     -1.18%
9b5743
9b5743
benchmark                  old allocs    new allocs    delta
9b5743
BenchmarkListFiles100k     2105167       2005692       -4.73%
9b5743
9b5743
benchmark                  old bytes     new bytes     delta
9b5743
BenchmarkListFiles100k     105903472     54831527      -48.22%
9b5743
9b5743
This improvement is very important if your code has to deal with a lot
9b5743
of tarballs which contain a lot of files.
9b5743
9b5743
LGTM=dsymonds
9b5743
R=golang-codereviews, dave, dsymonds, bradfitz
9b5743
CC=golang-codereviews
9b5743
https://codereview.appspot.com/108240044
9b5743
9b5743
Committer: David Symonds <dsymonds@golang.org>
9b5743
9b5743
diff -r 837348e418f3 -r 17404efd6b02 src/pkg/archive/tar/reader.go
9b5743
--- a/src/pkg/archive/tar/reader.go	Thu Jul 03 09:40:53 2014 +1000
9b5743
+++ b/src/pkg/archive/tar/reader.go	Thu Jul 03 09:41:19 2014 +1000
9b5743
@@ -29,10 +29,11 @@
9b5743
 // The Next method advances to the next file in the archive (including the first),
9b5743
 // and then it can be treated as an io.Reader to access the file's data.
9b5743
 type Reader struct {
9b5743
-	r    io.Reader
9b5743
-	err  error
9b5743
-	pad  int64          // amount of padding (ignored) after current file entry
9b5743
-	curr numBytesReader // reader for current file entry
9b5743
+	r       io.Reader
9b5743
+	err     error
9b5743
+	pad     int64           // amount of padding (ignored) after current file entry
9b5743
+	curr    numBytesReader  // reader for current file entry
9b5743
+	hdrBuff [blockSize]byte // buffer to use in readHeader
9b5743
 }
9b5743
 
9b5743
 // A numBytesReader is an io.Reader with a numBytes method, returning the number
9b5743
@@ -426,7 +427,9 @@
9b5743
 }
9b5743
 
9b5743
 func (tr *Reader) readHeader() *Header {
9b5743
-	header := make([]byte, blockSize)
9b5743
+	header := tr.hdrBuff[:]
9b5743
+	copy(header, zeroBlock)
9b5743
+
9b5743
 	if _, tr.err = io.ReadFull(tr.r, header); tr.err != nil {
9b5743
 		return nil
9b5743
 	}