From 23cd87eb0a2d49a3208824feaf34d8b852da422f Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Sat, 12 Aug 2017 00:53:45 +0530 Subject: [PATCH] archive/tar: optimize formatPAXRecord() call MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/archive/tar/strconv.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/archive/tar/strconv.go b/src/archive/tar/strconv.go index 7629c43c65..929770c25c 100644 --- a/src/archive/tar/strconv.go +++ b/src/archive/tar/strconv.go @@ -6,7 +6,6 @@ package tar import ( "bytes" - "fmt" "strconv" "strings" "time" @@ -266,12 +265,12 @@ func formatPAXRecord(k, v string) (string, error) { const padding = 3 // Extra padding for ' ', '=', and '\n' size := len(k) + len(v) + padding size += len(strconv.Itoa(size)) - record := fmt.Sprintf("%d %s=%s\n", size, k, v) + record := strconv.Itoa(size) + " " + k + "=" + v + "\n" // Final adjustment if adding size field increased the record size. if len(record) != size { size = len(record) - record = fmt.Sprintf("%d %s=%s\n", size, k, v) + record = strconv.Itoa(size) + " " + k + "=" + v + "\n" } return record, nil } -- 2.48.1