Skip to content

Commit 23cd87e

Browse files
agnivadedsnet
authored andcommitted
archive/tar: optimize formatPAXRecord() call
By replacing fmt.Sprintf with a simple string concat, we see pretty good improvements across the board on time and memory. name old time/op new time/op delta FormatPAXRecord 683ns ± 2% 210ns ± 5% -69.22% (p=0.000 n=10+10) name old alloc/op new alloc/op delta FormatPAXRecord 112B ± 0% 32B ± 0% -71.43% (p=0.000 n=10+10) name old allocs/op new allocs/op delta FormatPAXRecord 8.00 ± 0% 2.00 ± 0% -75.00% (p=0.000 n=10+10) Ran with - -cpu=1 -count=10 on an AMD64 i5-5200U CPU @ 2.20GHz Using the following benchmark: func BenchmarkFormatPAXRecord(b *testing.B) { for n := 0; n < b.N; n++ { formatPAXRecord("foo", "bar") } } Change-Id: I828ddbafad2e5d937f0cf5f777b512638344acfc Reviewed-on: https://go-review.googlesource.com/55210 Reviewed-by: Joe Tsai <[email protected]>
1 parent 0d1a8f6 commit 23cd87e

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/archive/tar/strconv.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package tar
66

77
import (
88
"bytes"
9-
"fmt"
109
"strconv"
1110
"strings"
1211
"time"
@@ -266,12 +265,12 @@ func formatPAXRecord(k, v string) (string, error) {
266265
const padding = 3 // Extra padding for ' ', '=', and '\n'
267266
size := len(k) + len(v) + padding
268267
size += len(strconv.Itoa(size))
269-
record := fmt.Sprintf("%d %s=%s\n", size, k, v)
268+
record := strconv.Itoa(size) + " " + k + "=" + v + "\n"
270269

271270
// Final adjustment if adding size field increased the record size.
272271
if len(record) != size {
273272
size = len(record)
274-
record = fmt.Sprintf("%d %s=%s\n", size, k, v)
273+
record = strconv.Itoa(size) + " " + k + "=" + v + "\n"
275274
}
276275
return record, nil
277276
}

0 commit comments

Comments
 (0)