compiler: do not allocate slotes for unused "_" vars
This commit is contained in:
parent
0b44a43043
commit
18369c489e
3 changed files with 29 additions and 7 deletions
|
@ -112,7 +112,11 @@ func countGlobals(f ast.Node) (i int) {
|
|||
case *ast.GenDecl:
|
||||
if n.Tok == token.VAR {
|
||||
for _, s := range n.Specs {
|
||||
i += len(s.(*ast.ValueSpec).Names)
|
||||
for _, id := range s.(*ast.ValueSpec).Names {
|
||||
if id.Name != "_" {
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
|
|
@ -434,13 +434,15 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
|||
switch t := spec.(type) {
|
||||
case *ast.ValueSpec:
|
||||
for _, id := range t.Names {
|
||||
if c.scope == nil {
|
||||
// it is a global declaration
|
||||
c.newGlobal("", id.Name)
|
||||
} else {
|
||||
c.scope.newLocal(id.Name)
|
||||
if id.Name != "_" {
|
||||
if c.scope == nil {
|
||||
// it is a global declaration
|
||||
c.newGlobal("", id.Name)
|
||||
} else {
|
||||
c.scope.newLocal(id.Name)
|
||||
}
|
||||
c.registerDebugVariable(id.Name, t.Type)
|
||||
}
|
||||
c.registerDebugVariable(id.Name, t.Type)
|
||||
}
|
||||
for i := range t.Names {
|
||||
if len(t.Values) != 0 {
|
||||
|
|
|
@ -255,3 +255,19 @@ func TestConstDontUseSlots(t *testing.T) {
|
|||
src := buf.String()
|
||||
eval(t, src, big.NewInt(count))
|
||||
}
|
||||
|
||||
func TestUnderscoreVarsDontUseSlots(t *testing.T) {
|
||||
const count = 128
|
||||
buf := bytes.NewBufferString("package foo\n")
|
||||
for i := 0; i < count; i++ {
|
||||
buf.WriteString(fmt.Sprintf("var _, n%d = 1, 1\n", i))
|
||||
}
|
||||
buf.WriteString("func Main() int { sum := 0\n")
|
||||
for i := 0; i < count; i++ {
|
||||
buf.WriteString(fmt.Sprintf("sum += n%d\n", i))
|
||||
}
|
||||
buf.WriteString("return sum }")
|
||||
|
||||
src := buf.String()
|
||||
eval(t, src, big.NewInt(count))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue