cmd/compile/internal/syntax: replace inlined statement lists with syntax.BlockStmt

This simplifies the code and removes a premature optimization.
It increases the amount of allocated syntax.Node space by ~0.4%
for parsing all of std lib, which is negligible.

Before the change (best of 5 runs):

  $ go test -run StdLib -fast
  parsed 1517022 lines (3394 files) in 793.487886ms (1911840 lines/s)
  allocated 387.086Mb (267B/line, 487.828Mb/s)

After the change (best of 5 runs):

  $ go test -run StdLib -fast
  parsed 1516911 lines (3392 files) in 805.028655ms (1884294 lines/s)
  allocated 388.466Mb (268B/line, 482.549Mb/s)

Change-Id: Id19d6210fdc62393862ba3b04913352d95c599be
Reviewed-on: https://go-review.googlesource.com/38439
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2017-03-21 22:23:15 -07:00
parent e0329248d5
commit b5f81eae17
6 changed files with 80 additions and 111 deletions

View file

@ -297,7 +297,7 @@ func (p *noder) funcDecl(fun *syntax.FuncDecl) *Node {
var body []*Node
if fun.Body != nil {
body = p.stmts(fun.Body)
body = p.stmts(fun.Body.List)
if body == nil {
body = []*Node{p.nod(fun, OEMPTY, nil, nil)}
}
@ -314,7 +314,11 @@ func (p *noder) funcDecl(fun *syntax.FuncDecl) *Node {
yyerror("go:nosplit and go:systemstack cannot be combined")
}
f.Func.Pragma = pragma
lineno = Ctxt.PosTable.XPos(fun.Rbrace)
var rbrace src.Pos
if fun.Body != nil {
rbrace = fun.Body.Rbrace
}
lineno = Ctxt.PosTable.XPos(rbrace)
f.Func.Endlineno = lineno
funcbody(f)
@ -450,8 +454,8 @@ func (p *noder) expr(expr syntax.Expr) *Node {
return p.nod(expr, OKEY, p.expr(expr.Key), p.wrapname(expr.Value, p.expr(expr.Value)))
case *syntax.FuncLit:
closurehdr(p.typeExpr(expr.Type))
body := p.stmts(expr.Body)
lineno = Ctxt.PosTable.XPos(expr.Rbrace)
body := p.stmts(expr.Body.List)
lineno = Ctxt.PosTable.XPos(expr.Body.Rbrace)
return p.setlineno(expr, closurebody(body))
case *syntax.ParenExpr:
return p.nod(expr, OPAREN, p.expr(expr.X), nil)
@ -676,7 +680,12 @@ func (p *noder) stmt(stmt syntax.Stmt) *Node {
case *syntax.LabeledStmt:
return p.labeledStmt(stmt)
case *syntax.BlockStmt:
return p.body(stmt.Body)
l := p.blockStmt(stmt)
if len(l) == 0 {
// TODO(mdempsky): Line number?
return nod(OEMPTY, nil, nil)
}
return liststmt(l)
case *syntax.ExprStmt:
return p.wrapname(stmt, p.expr(stmt.X))
case *syntax.SendStmt:
@ -781,18 +790,9 @@ func (p *noder) stmt(stmt syntax.Stmt) *Node {
panic("unhandled Stmt")
}
func (p *noder) body(body []syntax.Stmt) *Node {
l := p.bodyList(body)
if len(l) == 0 {
// TODO(mdempsky): Line number?
return nod(OEMPTY, nil, nil)
}
return liststmt(l)
}
func (p *noder) bodyList(body []syntax.Stmt) []*Node {
func (p *noder) blockStmt(stmt *syntax.BlockStmt) []*Node {
markdcl()
nodes := p.stmts(body)
nodes := p.stmts(stmt.List)
popdcl()
return nodes
}
@ -806,7 +806,7 @@ func (p *noder) ifStmt(stmt *syntax.IfStmt) *Node {
if stmt.Cond != nil {
n.Left = p.expr(stmt.Cond)
}
n.Nbody.Set(p.bodyList(stmt.Then))
n.Nbody.Set(p.blockStmt(stmt.Then))
if stmt.Else != nil {
e := p.stmt(stmt.Else)
if e.Op == OBLOCK && e.Ninit.Len() == 0 {
@ -848,7 +848,7 @@ func (p *noder) forStmt(stmt *syntax.ForStmt) *Node {
n.Right = p.stmt(stmt.Post)
}
}
n.Nbody.Set(p.bodyList(stmt.Body))
n.Nbody.Set(p.blockStmt(stmt.Body))
popdcl()
return n
}