Skip to content

Commit

Permalink
cmd/internal/gc: remove namebuf variable
Browse files Browse the repository at this point in the history
namebuf was a global char buffer in the C version of gc, which was
useful for providing common storage for constructing symbol and file
names.  However, now that it's just a global Go string and the string
data is dynamically allocated anyway, it doesn't serve any purpose
except to force extra write barriers everytime it's assigned to.

Also, introduce Lookupf(fmt, args...) as shorthand for
Lookup(fmt.Sprintf(fmt, args...)), which was a very common pattern for
using namebuf.

Passes "go build -toolexec 'toolstash -cmp' -a std".

Notably, this CL shrinks 6g's text section by ~15kB:

$ size toolstash/6g tool/linux_amd64/6g
   text	   data	    bss	    dec	    hex	filename
4600805	 605968	 342988	5549761	 54aec1	toolstash/6g
4585547	 605968	 342956	5534471	 547307	tool/linux_amd64/6g

Change-Id: I98abb44fc7f43a2e2e48425cc9f215cd0be37442
Reviewed-on: https://go-review.googlesource.com/7080
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
mdempsky authored and ianlancetaylor committed Mar 13, 2015
1 parent 211df1d commit 8b3670f
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 95 deletions.
17 changes: 5 additions & 12 deletions src/cmd/internal/gc/closure.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ func closurename(n *Node) *Sym {
} else {
Fatal("closurename called for %v", Nconv(n, obj.FmtShort))
}
namebuf = fmt.Sprintf("%s.%s%d", outer, prefix, gen)
n.Sym = Lookup(namebuf)
n.Sym = Lookupf("%s.%s%d", outer, prefix, gen)
return n.Sym
}

Expand Down Expand Up @@ -333,9 +332,7 @@ func transformclosure(xfunc *Node) {
// we introduce function param &v *T
// and v remains PPARAMREF with &v heapaddr
// (accesses will implicitly deref &v).
namebuf = fmt.Sprintf("&%s", v.Sym.Name)

addr = newname(Lookup(namebuf))
addr = newname(Lookupf("&%s", v.Sym.Name))
addr.Type = Ptrto(v.Type)
addr.Class = PPARAM
v.Heapaddr = addr
Expand Down Expand Up @@ -397,9 +394,7 @@ func transformclosure(xfunc *Node) {
} else {
// Declare variable holding addresses taken from closure
// and initialize in entry prologue.
namebuf = fmt.Sprintf("&%s", v.Sym.Name)

addr = newname(Lookup(namebuf))
addr = newname(Lookupf("&%s", v.Sym.Name))
addr.Ntype = Nod(OIND, typenod(v.Type), nil)
addr.Class = PAUTO
addr.Used = true
Expand Down Expand Up @@ -557,9 +552,8 @@ func makepartialcall(fn *Node, t0 *Type, meth *Node) *Node {
var fld *Node
var n *Node
for t := getinargx(t0).Type; t != nil; t = t.Down {
namebuf = fmt.Sprintf("a%d", i)
n = newname(Lookupf("a%d", i))
i++
n = newname(Lookup(namebuf))
n.Class = PPARAM
xfunc.Dcl = list(xfunc.Dcl, n)
callargs = list(callargs, n)
Expand All @@ -577,9 +571,8 @@ func makepartialcall(fn *Node, t0 *Type, meth *Node) *Node {
l = nil
var retargs *NodeList
for t := getoutargx(t0).Type; t != nil; t = t.Down {
namebuf = fmt.Sprintf("r%d", i)
n = newname(Lookupf("r%d", i))
i++
n = newname(Lookup(namebuf))
n.Class = PPARAMOUT
xfunc.Dcl = list(xfunc.Dcl, n)
retargs = list(retargs, n)
Expand Down
7 changes: 2 additions & 5 deletions src/cmd/internal/gc/dcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,8 @@ func funcargs(nt *Node) {

if n.Left == nil {
// Name so that escape analysis can track it. ~r stands for 'result'.
namebuf = fmt.Sprintf("~r%d", gen)
n.Left = newname(Lookupf("~r%d", gen))
gen++

n.Left = newname(Lookup(namebuf))
}

// TODO: n->left->missing = 1;
Expand All @@ -667,9 +665,8 @@ func funcargs(nt *Node) {

*nn = *n.Left
nn.Orig = nn
namebuf = fmt.Sprintf("~b%d", gen)
nn.Sym = Lookupf("~b%d", gen)
gen++
nn.Sym = Lookup(namebuf)
n.Left = nn
}

Expand Down
4 changes: 1 addition & 3 deletions src/cmd/internal/gc/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,8 @@ func Tempname(nn *Node, t *Type) {

// give each tmp a different name so that there
// a chance to registerizer them
namebuf = fmt.Sprintf("autotmp_%.4d", statuniqgen)

s := Lookupf("autotmp_%.4d", statuniqgen)
statuniqgen++
s := Lookup(namebuf)
n := Nod(ONAME, nil, nil)
n.Sym = s
s.Def = n
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/internal/gc/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,6 @@ var safemode int

var nolocalimports int

var namebuf string

var lexbuf bytes.Buffer
var strbuf bytes.Buffer

Expand Down
16 changes: 4 additions & 12 deletions src/cmd/internal/gc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package gc

import "fmt"

// case OADD:
// if(n->right->op == OLITERAL) {
// v = n->right->vconst;
Expand All @@ -31,8 +29,7 @@ var renameinit_initgen int

func renameinit() *Sym {
renameinit_initgen++
namebuf = fmt.Sprintf("init.%d", renameinit_initgen)
return Lookup(namebuf)
return Lookupf("init.%d", renameinit_initgen)
}

/*
Expand Down Expand Up @@ -112,18 +109,14 @@ func fninit(n *NodeList) {
var r *NodeList

// (1)
namebuf = "initdone·"

gatevar := newname(Lookup(namebuf))
gatevar := newname(Lookup("initdone·"))
addvar(gatevar, Types[TUINT8], PEXTERN)

// (2)
Maxarg = 0

namebuf = "init"

fn := Nod(ODCLFUNC, nil, nil)
initsym := Lookup(namebuf)
initsym := Lookup("init")
fn.Nname = newname(initsym)
fn.Nname.Defn = fn
fn.Nname.Ntype = Nod(OTFUNC, nil, nil)
Expand Down Expand Up @@ -169,8 +162,7 @@ func fninit(n *NodeList) {
// (9)
// could check that it is fn of no args/returns
for i := 1; ; i++ {
namebuf = fmt.Sprintf("init.%d", i)
s := Lookup(namebuf)
s := Lookupf("init.%d", i)
if s.Def == nil {
break
}
Expand Down
9 changes: 3 additions & 6 deletions src/cmd/internal/gc/inl.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,7 @@ func inlvar(var_ *Node) *Node {

// Synthesize a variable to store the inlined function's results in.
func retvar(t *Type, i int) *Node {
namebuf = fmt.Sprintf("~r%d", i)
n := newname(Lookup(namebuf))
n := newname(Lookupf("~r%d", i))
n.Type = t.Type
n.Class = PAUTO
n.Used = true
Expand All @@ -872,8 +871,7 @@ func retvar(t *Type, i int) *Node {
// Synthesize a variable to store the inlined function's arguments
// when they come from a multiple return call.
func argvar(t *Type, i int) *Node {
namebuf = fmt.Sprintf("~arg%d", i)
n := newname(Lookup(namebuf))
n := newname(Lookupf("~arg%d", i))
n.Type = t.Type
n.Class = PAUTO
n.Used = true
Expand All @@ -886,8 +884,7 @@ var newlabel_inl_label int

func newlabel_inl() *Node {
newlabel_inl_label++
namebuf = fmt.Sprintf(".inlret%.6d", newlabel_inl_label)
n := newname(Lookup(namebuf))
n := newname(Lookupf(".inlret%.6d", newlabel_inl_label))
n.Etype = 1 // flag 'safe' for escape analysis (no backjumps)
return n
}
Expand Down
67 changes: 31 additions & 36 deletions src/cmd/internal/gc/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,25 +559,24 @@ func islocalname(name string) bool {
strings.HasPrefix(name, "../") || name == ".."
}

func findpkg(name string) bool {
func findpkg(name string) (file string, ok bool) {
if islocalname(name) {
if safemode != 0 || nolocalimports != 0 {
return false
return "", false
}

// try .a before .6. important for building libraries:
// if there is an array.6 in the array.a library,
// want to find all of array.a, not just array.6.
namebuf = fmt.Sprintf("%s.a", name)

if obj.Access(namebuf, 0) >= 0 {
return true
file = fmt.Sprintf("%s.a", name)
if obj.Access(file, 0) >= 0 {
return file, true
}
namebuf = fmt.Sprintf("%s.%c", name, Thearch.Thechar)
if obj.Access(namebuf, 0) >= 0 {
return true
file = fmt.Sprintf("%s.%c", name, Thearch.Thechar)
if obj.Access(file, 0) >= 0 {
return file, true
}
return false
return "", false
}

// local imports should be canonicalized already.
Expand All @@ -587,17 +586,17 @@ func findpkg(name string) bool {
_ = q
if path.Clean(name) != name {
Yyerror("non-canonical import path %q (should be %q)", name, q)
return false
return "", false
}

for p := idirs; p != nil; p = p.link {
namebuf = fmt.Sprintf("%s/%s.a", p.dir, name)
if obj.Access(namebuf, 0) >= 0 {
return true
file = fmt.Sprintf("%s/%s.a", p.dir, name)
if obj.Access(file, 0) >= 0 {
return file, true
}
namebuf = fmt.Sprintf("%s/%s.%c", p.dir, name, Thearch.Thechar)
if obj.Access(namebuf, 0) >= 0 {
return true
file = fmt.Sprintf("%s/%s.%c", p.dir, name, Thearch.Thechar)
if obj.Access(file, 0) >= 0 {
return file, true
}
}

Expand All @@ -612,17 +611,17 @@ func findpkg(name string) bool {
suffix = "race"
}

namebuf = fmt.Sprintf("%s/pkg/%s_%s%s%s/%s.a", goroot, goos, goarch, suffixsep, suffix, name)
if obj.Access(namebuf, 0) >= 0 {
return true
file = fmt.Sprintf("%s/pkg/%s_%s%s%s/%s.a", goroot, goos, goarch, suffixsep, suffix, name)
if obj.Access(file, 0) >= 0 {
return file, true
}
namebuf = fmt.Sprintf("%s/pkg/%s_%s%s%s/%s.%c", goroot, goos, goarch, suffixsep, suffix, name, Thearch.Thechar)
if obj.Access(namebuf, 0) >= 0 {
return true
file = fmt.Sprintf("%s/pkg/%s_%s%s%s/%s.%c", goroot, goos, goarch, suffixsep, suffix, name, Thearch.Thechar)
if obj.Access(file, 0) >= 0 {
return file, true
}
}

return false
return "", false
}

func fakeimport() {
Expand Down Expand Up @@ -698,7 +697,8 @@ func importfile(f *Val, line int) {
}
}

if !findpkg(path_) {
file, found := findpkg(path_)
if !found {
Yyerror("can't find import: %q", f.U.Sval)
errorexit()
}
Expand All @@ -708,7 +708,6 @@ func importfile(f *Val, line int) {
// If we already saw that package, feed a dummy statement
// to the lexer to avoid parsing export data twice.
if importpkg.Imported != 0 {
file := namebuf
tag := ""
if importpkg.Safe {
tag = "safe"
Expand All @@ -723,16 +722,13 @@ func importfile(f *Val, line int) {

var err error
var imp *obj.Biobuf
imp, err = obj.Bopenr(namebuf)
imp, err = obj.Bopenr(file)
if err != nil {
Yyerror("can't open import: %q: %v", f.U.Sval, err)
errorexit()
}

file := namebuf

n := len(namebuf)
if n > 2 && namebuf[n-2] == '.' && namebuf[n-1] == 'a' {
if strings.HasSuffix(file, ".a") {
if !skiptopkgdef(imp) {
Yyerror("import %s: not a package file", file)
errorexit()
Expand All @@ -757,7 +753,7 @@ func importfile(f *Val, line int) {

// assume files move (get installed)
// so don't record the full path.
linehist(file[n-len(path_)-2:], -1, 1) // acts as #pragma lib
linehist(file[len(file)-len(path_)-2:], -1, 1) // acts as #pragma lib

/*
* position the input right
Expand Down Expand Up @@ -3162,10 +3158,9 @@ func mkpackage(pkgname string) {
p = p[i+1:]
}
}
namebuf = p
if i := strings.LastIndex(namebuf, "."); i >= 0 {
namebuf = namebuf[:i]
if i := strings.LastIndex(p, "."); i >= 0 {
p = p[:i]
}
outfile = fmt.Sprintf("%s.%c", namebuf, Thearch.Thechar)
outfile = fmt.Sprintf("%s.%c", p, Thearch.Thechar)
}
}
15 changes: 8 additions & 7 deletions src/cmd/internal/gc/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func dumpobj() {
obj.Bputc(bout, 0)
}
obj.Bseek(bout, startobj-ArhdrSize, 0)
namebuf = fmt.Sprintf("_go_.%c", Thearch.Thechar)
formathdr(arhdr[:], namebuf, size)
name := fmt.Sprintf("_go_.%c", Thearch.Thechar)
formathdr(arhdr[:], name, size)
obj.Bwrite(bout, arhdr[:])
}

Expand Down Expand Up @@ -199,22 +199,23 @@ func duintptr(s *Sym, off int, v uint64) int {
var stringsym_gen int

func stringsym(s string) *Sym {
var symname string
var pkg *Pkg
if len(s) > 100 {
// huge strings are made static to avoid long names
stringsym_gen++
namebuf = fmt.Sprintf(".gostring.%d", stringsym_gen)
symname = fmt.Sprintf(".gostring.%d", stringsym_gen)

pkg = localpkg
} else {
// small strings get named by their contents,
// so that multiple modules using the same string
// can share it.
namebuf = fmt.Sprintf("%q", s)
symname = fmt.Sprintf("%q", s)
pkg = gostringpkg
}

sym := Pkglookup(namebuf, pkg)
sym := Pkglookup(symname, pkg)

// SymUniq flag indicates that data is generated already
if sym.Flags&SymUniq != 0 {
Expand Down Expand Up @@ -252,8 +253,8 @@ func slicebytes(nam *Node, s string, len int) {
var m int

slicebytes_gen++
namebuf = fmt.Sprintf(".gobytes.%d", slicebytes_gen)
sym := Pkglookup(namebuf, localpkg)
symname := fmt.Sprintf(".gobytes.%d", slicebytes_gen)
sym := Pkglookup(symname, localpkg)
sym.Def = newname(sym)

off := 0
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/internal/gc/pgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ var makefuncdatasym_nsym int32
func makefuncdatasym(namefmt string, funcdatakind int64) *Sym {
var nod Node

namebuf = fmt.Sprintf(namefmt, makefuncdatasym_nsym)
sym := Lookupf(namefmt, makefuncdatasym_nsym)
makefuncdatasym_nsym++
sym := Lookup(namebuf)
pnod := newname(sym)
pnod.Class = PEXTERN
Nodconst(&nod, Types[TINT32], funcdatakind)
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/internal/gc/sinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,8 @@ func staticassign(l *Node, r *Node, out **NodeList) bool {
* part of the composite literal.
*/
func staticname(t *Type, ctxt int) *Node {
namebuf = fmt.Sprintf("statictmp_%.4d", statuniqgen)
n := newname(Lookupf("statictmp_%.4d", statuniqgen))
statuniqgen++
n := newname(Lookup(namebuf))
if ctxt == 0 {
n.Readonly = true
}
Expand Down
Loading

0 comments on commit 8b3670f

Please sign in to comment.