From ecff94311d793dcb4c590690473aa1421f549f03 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 27 Sep 2017 23:30:43 +0530 Subject: [PATCH] cmd/cgo: simplify a call to strip spaces MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Combined the Split and Join call with a Replace. This simplifies the code as well as makes it fast. Micro-benchmarks show good improvements - func BenchmarkJoinSplit(b *testing.B) { for n := 0; n < b.N; n++ { strings.Join(strings.Split("this string has some spaces", " "), "") } } func BenchmarkReplace(b *testing.B) { for n := 0; n < b.N; n++ { strings.Replace("this string has some spaces", " ", "", -1) } } name old time/op new time/op delta JoinSplit-4 308ns ± 2% 192ns ± 4% -37.60% (p=0.008 n=5+5) name old alloc/op new alloc/op delta JoinSplit-4 144B ± 0% 64B ± 0% -55.56% (p=0.008 n=5+5) name old allocs/op new allocs/op delta JoinSplit-4 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5) Change-Id: I1dc32105ae7a0be5a43ab0bedde992cefbed5d7d Reviewed-on: https://go-review.googlesource.com/66590 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/cmd/cgo/gcc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index 867db4f114..dc80159ef3 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -2141,7 +2141,7 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type { if ss, ok := dwarfToName[s]; ok { s = ss } - s = strings.Join(strings.Split(s, " "), "") // strip spaces + s = strings.Replace(s, " ", "", -1) name := c.Ident("_Ctype_" + s) tt := *t typedef[name.Name] = &tt -- 2.48.1