[dev.inline] cmd/internal/src: replace src.Pos with syntax.Pos

This replaces the src.Pos LineHist-based position tracking with
the syntax.Pos implementation and updates all uses.

The LineHist table is not used anymore - the respective code is still
there but should be removed eventually. CL forthcoming.

Passes toolstash -cmp when comparing to the master repo (with the
exception of a couple of swapped assembly instructions, likely due
to different instruction scheduling because the line-based sorting
has changed; though this is won't affect correctness).

The sizes of various important compiler data structures have increased
significantly (see the various sizes_test.go files); this is probably
the reason for an increase of compilation times (to be addressed). Here
are the results of compilebench -count 5, run on a "quiet" machine (no
apps running besides a terminal):

name       old time/op     new time/op     delta
Template       256ms ± 1%      280ms ±15%  +9.54%          (p=0.008 n=5+5)
Unicode        132ms ± 1%      132ms ± 1%    ~             (p=0.690 n=5+5)
GoTypes        891ms ± 1%      917ms ± 2%  +2.88%          (p=0.008 n=5+5)
Compiler       3.84s ± 2%      3.99s ± 2%  +3.95%          (p=0.016 n=5+5)
MakeBash       47.1s ± 1%      47.2s ± 2%    ~             (p=0.841 n=5+5)

name       old user-ns/op  new user-ns/op  delta
Template        309M ± 1%       326M ± 2%  +5.18%          (p=0.008 n=5+5)
Unicode         165M ± 1%       168M ± 4%    ~             (p=0.421 n=5+5)
GoTypes        1.14G ± 2%      1.18G ± 1%  +3.47%          (p=0.008 n=5+5)
Compiler       5.00G ± 1%      5.16G ± 1%  +3.12%          (p=0.008 n=5+5)

Change-Id: I241c4246cdff627d7ecb95cac23060b38f9775ec
Reviewed-on: https://go-review.googlesource.com/34273
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 2016-12-09 17:15:05 -08:00
parent 2d429f01bd
commit 4808fc4443
38 changed files with 376 additions and 394 deletions

View file

@ -5,6 +5,7 @@
package syntax
import (
"cmd/internal/src"
"fmt"
"io"
"strconv"
@ -20,7 +21,7 @@ const trace = false
const gcCompat = true
type parser struct {
base *PosBase
base *src.PosBase
errh ErrorHandler
scanner
@ -32,11 +33,11 @@ type parser struct {
indent []byte // tracing support
}
func (p *parser) init(filename string, src io.Reader, errh ErrorHandler, pragh PragmaHandler) {
p.base = NewFileBase(filename)
func (p *parser) init(base *src.PosBase, r io.Reader, errh ErrorHandler, pragh PragmaHandler) {
p.base = base
p.errh = errh
p.scanner.init(
src,
r,
// Error and pragma handlers for scanner.
// Because the (line, col) positions passed to these
// handlers are always at or after the current reading
@ -48,6 +49,7 @@ func (p *parser) init(filename string, src io.Reader, errh ErrorHandler, pragh P
func(line, col uint, text string) {
if strings.HasPrefix(text, "line ") {
p.updateBase(line, col+5, text[5:])
return
}
if pragh != nil {
p.pragma |= pragh(p.pos_at(line, col), text)
@ -63,6 +65,8 @@ func (p *parser) init(filename string, src io.Reader, errh ErrorHandler, pragh P
p.indent = nil
}
const lineMax = 1<<24 - 1 // TODO(gri) this limit is defined for src.Pos - fix
func (p *parser) updateBase(line, col uint, text string) {
// Want to use LastIndexByte below but it's not defined in Go1.4 and bootstrap fails.
i := strings.LastIndex(text, ":") // look from right (Windows filenames may contain ':')
@ -75,7 +79,7 @@ func (p *parser) updateBase(line, col uint, text string) {
p.error_at(p.pos_at(line, col+uint(i+1)), "invalid line number: "+nstr)
return
}
p.base = NewLinePragmaBase(MakePos(p.base.Pos().Base(), line, col), text[:i], uint(n))
p.base = src.NewLinePragmaBase(src.MakePos(p.base.Pos().Base(), line, col), text[:i], uint(n))
}
func (p *parser) got(tok token) bool {
@ -97,12 +101,12 @@ func (p *parser) want(tok token) {
// Error handling
// pos_at returns the Pos value for (line, col) and the current position base.
func (p *parser) pos_at(line, col uint) Pos {
return MakePos(p.base, line, col)
func (p *parser) pos_at(line, col uint) src.Pos {
return src.MakePos(p.base, line, col)
}
// error reports an error at the given position.
func (p *parser) error_at(pos Pos, msg string) {
func (p *parser) error_at(pos src.Pos, msg string) {
err := Error{pos, msg}
if p.first == nil {
p.first = err
@ -114,7 +118,7 @@ func (p *parser) error_at(pos Pos, msg string) {
}
// syntax_error_at reports a syntax error at the given position.
func (p *parser) syntax_error_at(pos Pos, msg string) {
func (p *parser) syntax_error_at(pos src.Pos, msg string) {
if trace {
defer p.trace("syntax_error (" + msg + ")")()
}
@ -159,7 +163,7 @@ func (p *parser) syntax_error_at(pos Pos, msg string) {
}
// Convenience methods using the current token position.
func (p *parser) pos() Pos { return p.pos_at(p.line, p.col) }
func (p *parser) pos() src.Pos { return p.pos_at(p.line, p.col) }
func (p *parser) error(msg string) { p.error_at(p.pos(), msg) }
func (p *parser) syntax_error(msg string) { p.syntax_error_at(p.pos(), msg) }