From: Josh Bleecher Snyder Date: Mon, 22 May 2017 13:40:06 +0000 (-0700) Subject: cmd/compile: collapse runs of string constants in walkprint X-Git-Tag: go1.10beta1~1534 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a14a8a3eb99658b6a0856b664cf900c861a2e306;p=gostls13.git cmd/compile: collapse runs of string constants in walkprint This reduces the code footprint of code like: println("foo=", foo, "bar=", bar) which is fairly common in the runtime. Prior to this change, this makes function calls to print each of: "foo=", " ", foo, " ", "bar=", " ", bar, "\n" After this change, this prints: "foo= ", foo, " bar= ", bar, "\n" This shrinks the hello world binary by 0.4%. More importantly, this improves the instruction density of important runtime routines. Change-Id: I8971bdf5382fbaaf4a82bad4442f9da07c28d395 Reviewed-on: https://go-review.googlesource.com/55098 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go index 667dd2acb1..b7fabc1ebf 100644 --- a/src/cmd/compile/internal/gc/walk.go +++ b/src/cmd/compile/internal/gc/walk.go @@ -2036,6 +2036,25 @@ func walkprint(nn *Node, init *Nodes) *Node { nn.List.Set(t) } + // Collapse runs of constant strings. + s := nn.List.Slice() + t := make([]*Node, 0, len(s)) + for i := 0; i < len(s); { + var strs []string + for i < len(s) && Isconst(s[i], CTSTR) { + strs = append(strs, s[i].Val().U.(string)) + i++ + } + if len(strs) > 0 { + t = append(t, nodstr(strings.Join(strs, ""))) + } + if i < len(s) { + t = append(t, s[i]) + i++ + } + } + nn.List.Set(t) + calls := []*Node{mkcall("printlock", nil, init)} for i, n := range nn.List.Slice() { if n.Op == OLITERAL {