1
0
mirror of https://github.com/golang/go synced 2024-11-13 19:10:22 -07:00

archive/tar: reuse temporary buffer in readHeader

A temporary 512 bytes buffer is allocated for every call to
readHeader. This buffer isn't returned to the caller and it could
be reused to lower the number of memory allocations.

This CL improves it by using a pool and zeroing out the buffer before
putting it back into the pool.

benchmark                  old ns/op     new ns/op     delta
BenchmarkListFiles100k     545249903     538832687     -1.18%

benchmark                  old allocs    new allocs    delta
BenchmarkListFiles100k     2105167       2005692       -4.73%

benchmark                  old bytes     new bytes     delta
BenchmarkListFiles100k     105903472     54831527      -48.22%

This improvement is very important if your code has to deal with a lot
of tarballs which contain a lot of files.

LGTM=dsymonds
R=golang-codereviews, dave, dsymonds, bradfitz
CC=golang-codereviews
https://golang.org/cl/108240044
This commit is contained in:
Cristian Staretu 2014-07-03 09:41:19 +10:00 committed by David Symonds
parent fe5a358aae
commit 61ccc1f05f

View File

@ -29,10 +29,11 @@ const maxNanoSecondIntSize = 9
// The Next method advances to the next file in the archive (including the first), // The Next method advances to the next file in the archive (including the first),
// and then it can be treated as an io.Reader to access the file's data. // and then it can be treated as an io.Reader to access the file's data.
type Reader struct { type Reader struct {
r io.Reader r io.Reader
err error err error
pad int64 // amount of padding (ignored) after current file entry pad int64 // amount of padding (ignored) after current file entry
curr numBytesReader // reader for current file entry curr numBytesReader // reader for current file entry
hdrBuff [blockSize]byte // buffer to use in readHeader
} }
// A numBytesReader is an io.Reader with a numBytes method, returning the number // A numBytesReader is an io.Reader with a numBytes method, returning the number
@ -426,7 +427,9 @@ func (tr *Reader) verifyChecksum(header []byte) bool {
} }
func (tr *Reader) readHeader() *Header { func (tr *Reader) readHeader() *Header {
header := make([]byte, blockSize) header := tr.hdrBuff[:]
copy(header, zeroBlock)
if _, tr.err = io.ReadFull(tr.r, header); tr.err != nil { if _, tr.err = io.ReadFull(tr.r, header); tr.err != nil {
return nil return nil
} }