]> Cypherpunks repositories - gostls13.git/commitdiff
archive/tar: reuse temporary buffer in writeHeader
authorCristian Staretu <unclejacksons@gmail.com>
Wed, 2 Jul 2014 23:40:53 +0000 (09:40 +1000)
committerDavid Symonds <dsymonds@golang.org>
Wed, 2 Jul 2014 23:40:53 +0000 (09:40 +1000)
A temporary 512 bytes buffer is allocated for every call to
writeHeader. This buffer could be reused the lower the number
of memory allocations.

benchmark                   old ns/op     new ns/op     delta
BenchmarkWriteFiles100k     634622051     583810847     -8.01%

benchmark                   old allocs     new allocs     delta
BenchmarkWriteFiles100k     2701920        2602621        -3.68%

benchmark                   old bytes     new bytes     delta
BenchmarkWriteFiles100k     115383884     64349922      -44.23%

This change is very important if your code has to write a lot of
tarballs with a lot of files.

LGTM=dsymonds
R=golang-codereviews, dave, dsymonds
CC=golang-codereviews
https://golang.org/cl/107440043

src/pkg/archive/tar/writer.go

index 6eff6f6f84d8eaa903b87c135ec4f5f6684b4667..d107dbbb51cf3f158306f94adb68eec4c6302294 100644 (file)
@@ -37,8 +37,9 @@ type Writer struct {
        nb         int64 // number of unwritten bytes for current file entry
        pad        int64 // amount of padding to write after current file entry
        closed     bool
-       usedBinary bool // whether the binary numeric field extension was used
-       preferPax  bool // use pax header instead of binary numeric header
+       usedBinary bool            // whether the binary numeric field extension was used
+       preferPax  bool            // use pax header instead of binary numeric header
+       hdrBuff    [blockSize]byte // buffer to use in writeHeader
 }
 
 // NewWriter creates a new Writer writing to w.
@@ -160,7 +161,8 @@ func (tw *Writer) writeHeader(hdr *Header, allowPax bool) error {
        // subsecond time resolution, but for now let's just capture
        // too long fields or non ascii characters
 
-       header := make([]byte, blockSize)
+       header := tw.hdrBuff[:]
+       copy(header, zeroBlock)
        s := slicer(header)
 
        // keep a reference to the filename to allow to overwrite it later if we detect that we can use ustar longnames instead of pax