Skip to content

Commit

Permalink
cmd/internal/obj: reimplement line history
Browse files Browse the repository at this point in the history
In addition to possibly being clearer code,
this replaces an O(n) lookup with an O(log n) lookup.

Change-Id: I0a574c536a965a87f7ad6dcdcc30f737bc771cd5
Reviewed-on: https://go-review.googlesource.com/7623
Reviewed-by: Rob Pike <r@golang.org>
  • Loading branch information
rsc authored and Gerrit Code Review committed Mar 17, 2015
1 parent ebe3d69 commit 8615465
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 276 deletions.
22 changes: 8 additions & 14 deletions src/cmd/internal/gc/lex.go
Expand Up @@ -1544,14 +1544,15 @@ func getlinepragma() int {
}
cp.WriteByte(byte(c))
}

cp = nil

if strings.HasPrefix(lexbuf.String(), "go:cgo_") {
pragcgo(lexbuf.String())
text := lexbuf.String()

if strings.HasPrefix(text, "go:cgo_") {
pragcgo(text)
}

cmd = lexbuf.String()
cmd = text
verb = cmd
if i := strings.Index(verb, " "); i >= 0 {
verb = verb[:i]
Expand Down Expand Up @@ -1630,8 +1631,9 @@ func getlinepragma() int {
if linep == 0 {
return c
}
text := lexbuf.String()
n := 0
for _, c := range lexbuf.String()[linep:] {
for _, c := range text[linep:] {
if c < '0' || c > '9' {
goto out
}
Expand All @@ -1646,15 +1648,7 @@ func getlinepragma() int {
return c
}

// try to avoid allocating file name over and over
name = lexbuf.String()[:linep-1]
for h := Ctxt.Hist; h != nil; h = h.Link {
if h.Name != "" && h.Name == name {
linehist(h.Name, int32(n), 0)
return c
}
}

name = text[:linep-1]
linehist(name, int32(n), 0)
return c

Expand Down
21 changes: 11 additions & 10 deletions src/cmd/internal/obj/line_test.go
Expand Up @@ -11,6 +11,7 @@ import (

func TestLineHist(t *testing.T) {
ctxt := new(Link)
ctxt.Hash = make(map[SymVer]*LSym)

Linklinehist(ctxt, 1, "a.c", 0)
Linklinehist(ctxt, 3, "a.h", 0)
Expand All @@ -22,18 +23,18 @@ func TestLineHist(t *testing.T) {

var expect = []string{
0: "??:0",
1: "/a.c:1",
2: "/a.c:2",
3: "/a.h:1",
4: "/a.h:2",
5: "/a.c:3",
6: "/a.c:4",
7: "/linedir:2",
8: "/linedir:3",
1: "a.c:1",
2: "a.c:2",
3: "a.h:1",
4: "a.h:2",
5: "a.c:3",
6: "a.c:4",
7: "linedir:2",
8: "linedir:3",
9: "??:0",
10: "??:0",
11: "/b.c:1",
12: "/b.c:2",
11: "b.c:1",
12: "b.c:2",
13: "??:0",
14: "??:0",
}
Expand Down
20 changes: 18 additions & 2 deletions src/cmd/internal/obj/link.go
Expand Up @@ -183,8 +183,8 @@ type Link struct {
Hash map[SymVer]*LSym
Allsym *LSym
Nsymbol int32
Hist *Hist
Ehist *Hist
LineHist LineHist
Imports []string
Plist *Plist
Plast *Plist
Sym_div *LSym
Expand Down Expand Up @@ -580,3 +580,19 @@ const (
)

var linkbasepointer int

/*
* start a new Prog list.
*/
func Linknewplist(ctxt *Link) *Plist {
pl := new(Plist)
*pl = Plist{}
if ctxt.Plist == nil {
ctxt.Plist = pl
} else {
ctxt.Plast.Link = pl
}
ctxt.Plast = pl

return pl
}

0 comments on commit 8615465

Please sign in to comment.