mirror of
https://github.com/golang/go
synced 2024-11-22 00:14:42 -07:00
godoc: remove uses of container/vector
In the process, rewrite index.go to use slices instead of vectors, rewrite for-loops into range loops, and generally simplify code (this code was written before the launch of go and showed its age). Also, fix a wrong import in appinit.go. No significant performance changes (improvements); most of time is spent elsewhere (measured on an stand- alone MacBook Pro with SSD disk, running standard godoc settings: godoc -v -http=:7777 -index). R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/4875056
This commit is contained in:
parent
fd897ffc68
commit
72ddc87681
@ -39,7 +39,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"alt/archive/zip"
|
"archive/zip"
|
||||||
"http"
|
"http"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/vector"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"http"
|
"http"
|
||||||
"io"
|
"io"
|
||||||
@ -183,17 +182,17 @@ func codewalkDir(w http.ResponseWriter, r *http.Request, relpath, abspath string
|
|||||||
serveError(w, r, relpath, err)
|
serveError(w, r, relpath, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var v vector.Vector
|
var v []interface{}
|
||||||
for _, fi := range dir {
|
for _, fi := range dir {
|
||||||
name := fi.Name()
|
name := fi.Name()
|
||||||
if fi.IsDirectory() {
|
if fi.IsDirectory() {
|
||||||
v.Push(&elem{name + "/", ""})
|
v = append(v, &elem{name + "/", ""})
|
||||||
} else if strings.HasSuffix(name, ".xml") {
|
} else if strings.HasSuffix(name, ".xml") {
|
||||||
cw, err := loadCodewalk(abspath + "/" + name)
|
cw, err := loadCodewalk(abspath + "/" + name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
v.Push(&elem{name[0 : len(name)-len(".xml")], cw.Title})
|
v = append(v, &elem{name[0 : len(name)-len(".xml")], cw.Title})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"container/vector"
|
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
@ -52,48 +51,62 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// InterfaceSlice is a helper type for sorting interface
|
||||||
|
// slices according to some slice-specific sort criteria.
|
||||||
|
|
||||||
|
type Comparer func(x, y interface{}) bool
|
||||||
|
|
||||||
|
type InterfaceSlice struct {
|
||||||
|
slice []interface{}
|
||||||
|
less Comparer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *InterfaceSlice) Len() int { return len(p.slice) }
|
||||||
|
func (p *InterfaceSlice) Less(i, j int) bool { return p.less(p.slice[i], p.slice[j]) }
|
||||||
|
func (p *InterfaceSlice) Swap(i, j int) { p.slice[i], p.slice[j] = p.slice[j], p.slice[i] }
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// RunList
|
// RunList
|
||||||
|
|
||||||
// A RunList is a vector of entries that can be sorted according to some
|
// A RunList is a list of entries that can be sorted according to some
|
||||||
// criteria. A RunList may be compressed by grouping "runs" of entries
|
// criteria. A RunList may be compressed by grouping "runs" of entries
|
||||||
// which are equal (according to the sort critera) into a new RunList of
|
// which are equal (according to the sort critera) into a new RunList of
|
||||||
// runs. For instance, a RunList containing pairs (x, y) may be compressed
|
// runs. For instance, a RunList containing pairs (x, y) may be compressed
|
||||||
// into a RunList containing pair runs (x, {y}) where each run consists of
|
// into a RunList containing pair runs (x, {y}) where each run consists of
|
||||||
// a list of y's with the same x.
|
// a list of y's with the same x.
|
||||||
type RunList struct {
|
type RunList []interface{}
|
||||||
vector.Vector
|
|
||||||
less func(x, y interface{}) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *RunList) Less(i, j int) bool { return h.less(h.At(i), h.At(j)) }
|
func (h RunList) sort(less Comparer) {
|
||||||
|
sort.Sort(&InterfaceSlice{h, less})
|
||||||
func (h *RunList) sort(less func(x, y interface{}) bool) {
|
|
||||||
h.less = less
|
|
||||||
sort.Sort(h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compress entries which are the same according to a sort criteria
|
// Compress entries which are the same according to a sort criteria
|
||||||
// (specified by less) into "runs".
|
// (specified by less) into "runs".
|
||||||
func (h *RunList) reduce(less func(x, y interface{}) bool, newRun func(h *RunList, i, j int) interface{}) *RunList {
|
func (h RunList) reduce(less Comparer, newRun func(h RunList) interface{}) RunList {
|
||||||
|
if len(h) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// len(h) > 0
|
||||||
|
|
||||||
// create runs of entries with equal values
|
// create runs of entries with equal values
|
||||||
h.sort(less)
|
h.sort(less)
|
||||||
|
|
||||||
// for each run, make a new run object and collect them in a new RunList
|
// for each run, make a new run object and collect them in a new RunList
|
||||||
var hh RunList
|
var hh RunList
|
||||||
i := 0
|
i, x := 0, h[0]
|
||||||
for j := 0; j < h.Len(); j++ {
|
for j, y := range h {
|
||||||
if less(h.At(i), h.At(j)) {
|
if less(x, y) {
|
||||||
hh.Push(newRun(h, i, j))
|
hh = append(hh, newRun(h[i:j]))
|
||||||
i = j // start a new run
|
i, x = j, h[j] // start a new run
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// add final run, if any
|
// add final run, if any
|
||||||
if i < h.Len() {
|
if i < len(h) {
|
||||||
hh.Push(newRun(h, i, h.Len()))
|
hh = append(hh, newRun(h[i:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &hh
|
return hh
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@ -178,14 +191,12 @@ func (f *KindRun) Swap(i, j int) { f.Infos[i], f.Infos[j] = f.Infos[j], f.I
|
|||||||
// FileRun contents are sorted by Kind for the reduction into KindRuns.
|
// FileRun contents are sorted by Kind for the reduction into KindRuns.
|
||||||
func lessKind(x, y interface{}) bool { return x.(SpotInfo).Kind() < y.(SpotInfo).Kind() }
|
func lessKind(x, y interface{}) bool { return x.(SpotInfo).Kind() < y.(SpotInfo).Kind() }
|
||||||
|
|
||||||
// newKindRun allocates a new KindRun from the SpotInfo run [i, j) in h.
|
// newKindRun allocates a new KindRun from the SpotInfo run h.
|
||||||
func newKindRun(h *RunList, i, j int) interface{} {
|
func newKindRun(h RunList) interface{} {
|
||||||
kind := h.At(i).(SpotInfo).Kind()
|
kind := h[0].(SpotInfo).Kind()
|
||||||
infos := make([]SpotInfo, j-i)
|
infos := make([]SpotInfo, len(h))
|
||||||
k := 0
|
for i, x := range h {
|
||||||
for ; i < j; i++ {
|
infos[i] = x.(SpotInfo)
|
||||||
infos[k] = h.At(i).(SpotInfo)
|
|
||||||
k++
|
|
||||||
}
|
}
|
||||||
run := &KindRun{kind, infos}
|
run := &KindRun{kind, infos}
|
||||||
|
|
||||||
@ -248,24 +259,21 @@ type FileRun struct {
|
|||||||
// Spots are sorted by path for the reduction into FileRuns.
|
// Spots are sorted by path for the reduction into FileRuns.
|
||||||
func lessSpot(x, y interface{}) bool { return x.(Spot).File.Path < y.(Spot).File.Path }
|
func lessSpot(x, y interface{}) bool { return x.(Spot).File.Path < y.(Spot).File.Path }
|
||||||
|
|
||||||
// newFileRun allocates a new FileRun from the Spot run [i, j) in h.
|
// newFileRun allocates a new FileRun from the Spot run h.
|
||||||
func newFileRun(h0 *RunList, i, j int) interface{} {
|
func newFileRun(h RunList) interface{} {
|
||||||
file := h0.At(i).(Spot).File
|
file := h[0].(Spot).File
|
||||||
|
|
||||||
// reduce the list of Spots into a list of KindRuns
|
// reduce the list of Spots into a list of KindRuns
|
||||||
var h1 RunList
|
h1 := make(RunList, len(h))
|
||||||
h1.Vector.Resize(j-i, 0)
|
for i, x := range h {
|
||||||
k := 0
|
h1[i] = x.(Spot).Info
|
||||||
for ; i < j; i++ {
|
|
||||||
h1.Set(k, h0.At(i).(Spot).Info)
|
|
||||||
k++
|
|
||||||
}
|
}
|
||||||
h2 := h1.reduce(lessKind, newKindRun)
|
h2 := h1.reduce(lessKind, newKindRun)
|
||||||
|
|
||||||
// create the FileRun
|
// create the FileRun
|
||||||
groups := make([]*KindRun, h2.Len())
|
groups := make([]*KindRun, len(h2))
|
||||||
for i := 0; i < h2.Len(); i++ {
|
for i, x := range h2 {
|
||||||
groups[i] = h2.At(i).(*KindRun)
|
groups[i] = x.(*KindRun)
|
||||||
}
|
}
|
||||||
return &FileRun{file, groups}
|
return &FileRun{file, groups}
|
||||||
}
|
}
|
||||||
@ -289,14 +297,12 @@ func lessFileRun(x, y interface{}) bool {
|
|||||||
return x.(*FileRun).File.Pak.less(&y.(*FileRun).File.Pak)
|
return x.(*FileRun).File.Pak.less(&y.(*FileRun).File.Pak)
|
||||||
}
|
}
|
||||||
|
|
||||||
// newPakRun allocates a new PakRun from the *FileRun run [i, j) in h.
|
// newPakRun allocates a new PakRun from the *FileRun run h.
|
||||||
func newPakRun(h *RunList, i, j int) interface{} {
|
func newPakRun(h RunList) interface{} {
|
||||||
pak := h.At(i).(*FileRun).File.Pak
|
pak := h[0].(*FileRun).File.Pak
|
||||||
files := make([]*FileRun, j-i)
|
files := make([]*FileRun, len(h))
|
||||||
k := 0
|
for i, x := range h {
|
||||||
for ; i < j; i++ {
|
files[i] = x.(*FileRun)
|
||||||
files[k] = h.At(i).(*FileRun)
|
|
||||||
k++
|
|
||||||
}
|
}
|
||||||
run := &PakRun{pak, files}
|
run := &PakRun{pak, files}
|
||||||
sort.Sort(run) // files were sorted by package; sort them by file now
|
sort.Sort(run) // files were sorted by package; sort them by file now
|
||||||
@ -312,7 +318,7 @@ type HitList []*PakRun
|
|||||||
// PakRuns are sorted by package.
|
// PakRuns are sorted by package.
|
||||||
func lessPakRun(x, y interface{}) bool { return x.(*PakRun).Pak.less(&y.(*PakRun).Pak) }
|
func lessPakRun(x, y interface{}) bool { return x.(*PakRun).Pak.less(&y.(*PakRun).Pak) }
|
||||||
|
|
||||||
func reduce(h0 *RunList) HitList {
|
func reduce(h0 RunList) HitList {
|
||||||
// reduce a list of Spots into a list of FileRuns
|
// reduce a list of Spots into a list of FileRuns
|
||||||
h1 := h0.reduce(lessSpot, newFileRun)
|
h1 := h0.reduce(lessSpot, newFileRun)
|
||||||
// reduce a list of FileRuns into a list of PakRuns
|
// reduce a list of FileRuns into a list of PakRuns
|
||||||
@ -320,28 +326,18 @@ func reduce(h0 *RunList) HitList {
|
|||||||
// sort the list of PakRuns by package
|
// sort the list of PakRuns by package
|
||||||
h2.sort(lessPakRun)
|
h2.sort(lessPakRun)
|
||||||
// create a HitList
|
// create a HitList
|
||||||
h := make(HitList, h2.Len())
|
h := make(HitList, len(h2))
|
||||||
for i := 0; i < h2.Len(); i++ {
|
for i, p := range h2 {
|
||||||
h[i] = h2.At(i).(*PakRun)
|
h[i] = p.(*PakRun)
|
||||||
}
|
}
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h HitList) filter(pakname string) HitList {
|
func (h HitList) filter(pakname string) HitList {
|
||||||
// determine number of matching packages (most of the time just one)
|
var hh HitList
|
||||||
n := 0
|
|
||||||
for _, p := range h {
|
for _, p := range h {
|
||||||
if p.Pak.Name == pakname {
|
if p.Pak.Name == pakname {
|
||||||
n++
|
hh = append(hh, p)
|
||||||
}
|
|
||||||
}
|
|
||||||
// create filtered HitList
|
|
||||||
hh := make(HitList, n)
|
|
||||||
i := 0
|
|
||||||
for _, p := range h {
|
|
||||||
if p.Pak.Name == pakname {
|
|
||||||
hh[i] = p
|
|
||||||
i++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hh
|
return hh
|
||||||
@ -365,34 +361,27 @@ type AltWords struct {
|
|||||||
// wordPairs are sorted by their canonical spelling.
|
// wordPairs are sorted by their canonical spelling.
|
||||||
func lessWordPair(x, y interface{}) bool { return x.(*wordPair).canon < y.(*wordPair).canon }
|
func lessWordPair(x, y interface{}) bool { return x.(*wordPair).canon < y.(*wordPair).canon }
|
||||||
|
|
||||||
// newAltWords allocates a new AltWords from the *wordPair run [i, j) in h.
|
// newAltWords allocates a new AltWords from the *wordPair run h.
|
||||||
func newAltWords(h *RunList, i, j int) interface{} {
|
func newAltWords(h RunList) interface{} {
|
||||||
canon := h.At(i).(*wordPair).canon
|
canon := h[0].(*wordPair).canon
|
||||||
alts := make([]string, j-i)
|
alts := make([]string, len(h))
|
||||||
k := 0
|
for i, x := range h {
|
||||||
for ; i < j; i++ {
|
alts[i] = x.(*wordPair).alt
|
||||||
alts[k] = h.At(i).(*wordPair).alt
|
|
||||||
k++
|
|
||||||
}
|
}
|
||||||
return &AltWords{canon, alts}
|
return &AltWords{canon, alts}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AltWords) filter(s string) *AltWords {
|
func (a *AltWords) filter(s string) *AltWords {
|
||||||
if len(a.Alts) == 1 && a.Alts[0] == s {
|
var alts []string
|
||||||
// there are no different alternatives
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// make a new AltWords with the current spelling removed
|
|
||||||
alts := make([]string, len(a.Alts))
|
|
||||||
i := 0
|
|
||||||
for _, w := range a.Alts {
|
for _, w := range a.Alts {
|
||||||
if w != s {
|
if w != s {
|
||||||
alts[i] = w
|
alts = append(alts, w)
|
||||||
i++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &AltWords{a.Canon, alts[0:i]}
|
if len(alts) > 0 {
|
||||||
|
return &AltWords{a.Canon, alts}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@ -424,7 +413,7 @@ type Indexer struct {
|
|||||||
fset *token.FileSet // file set for all indexed files
|
fset *token.FileSet // file set for all indexed files
|
||||||
sources bytes.Buffer // concatenated sources
|
sources bytes.Buffer // concatenated sources
|
||||||
words map[string]*IndexResult // RunLists of Spots
|
words map[string]*IndexResult // RunLists of Spots
|
||||||
snippets vector.Vector // vector of *Snippets, indexed by snippet indices
|
snippets []*Snippet // indices are stored in SpotInfos
|
||||||
current *token.File // last file added to file set
|
current *token.File // last file added to file set
|
||||||
file *File // AST for current file
|
file *File // AST for current file
|
||||||
decl ast.Decl // AST for current decl
|
decl ast.Decl // AST for current decl
|
||||||
@ -432,8 +421,8 @@ type Indexer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (x *Indexer) addSnippet(s *Snippet) int {
|
func (x *Indexer) addSnippet(s *Snippet) int {
|
||||||
index := x.snippets.Len()
|
index := len(x.snippets)
|
||||||
x.snippets.Push(s)
|
x.snippets = append(x.snippets, s)
|
||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -454,12 +443,12 @@ func (x *Indexer) visitIdent(kind SpotKind, id *ast.Ident) {
|
|||||||
if kind == Use || x.decl == nil {
|
if kind == Use || x.decl == nil {
|
||||||
// not a declaration or no snippet required
|
// not a declaration or no snippet required
|
||||||
info := makeSpotInfo(kind, x.current.Line(id.Pos()), false)
|
info := makeSpotInfo(kind, x.current.Line(id.Pos()), false)
|
||||||
lists.Others.Push(Spot{x.file, info})
|
lists.Others = append(lists.Others, Spot{x.file, info})
|
||||||
} else {
|
} else {
|
||||||
// a declaration with snippet
|
// a declaration with snippet
|
||||||
index := x.addSnippet(NewSnippet(x.fset, x.decl, id))
|
index := x.addSnippet(NewSnippet(x.fset, x.decl, id))
|
||||||
info := makeSpotInfo(kind, index, true)
|
info := makeSpotInfo(kind, index, true)
|
||||||
lists.Decls.Push(Spot{x.file, info})
|
lists.Decls = append(lists.Decls, Spot{x.file, info})
|
||||||
}
|
}
|
||||||
|
|
||||||
x.stats.Spots++
|
x.stats.Spots++
|
||||||
@ -782,13 +771,13 @@ func NewIndex(dirnames <-chan string, fulltextIndex bool) *Index {
|
|||||||
words := make(map[string]*LookupResult)
|
words := make(map[string]*LookupResult)
|
||||||
var wlist RunList
|
var wlist RunList
|
||||||
for w, h := range x.words {
|
for w, h := range x.words {
|
||||||
decls := reduce(&h.Decls)
|
decls := reduce(h.Decls)
|
||||||
others := reduce(&h.Others)
|
others := reduce(h.Others)
|
||||||
words[w] = &LookupResult{
|
words[w] = &LookupResult{
|
||||||
Decls: decls,
|
Decls: decls,
|
||||||
Others: others,
|
Others: others,
|
||||||
}
|
}
|
||||||
wlist.Push(&wordPair{canonical(w), w})
|
wlist = append(wlist, &wordPair{canonical(w), w})
|
||||||
}
|
}
|
||||||
x.stats.Words = len(words)
|
x.stats.Words = len(words)
|
||||||
|
|
||||||
@ -798,24 +787,18 @@ func NewIndex(dirnames <-chan string, fulltextIndex bool) *Index {
|
|||||||
|
|
||||||
// convert alist into a map of alternative spellings
|
// convert alist into a map of alternative spellings
|
||||||
alts := make(map[string]*AltWords)
|
alts := make(map[string]*AltWords)
|
||||||
for i := 0; i < alist.Len(); i++ {
|
for i := 0; i < len(alist); i++ {
|
||||||
a := alist.At(i).(*AltWords)
|
a := alist[i].(*AltWords)
|
||||||
alts[a.Canon] = a
|
alts[a.Canon] = a
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert snippet vector into a list
|
|
||||||
snippets := make([]*Snippet, x.snippets.Len())
|
|
||||||
for i := 0; i < x.snippets.Len(); i++ {
|
|
||||||
snippets[i] = x.snippets.At(i).(*Snippet)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create text index
|
// create text index
|
||||||
var suffixes *suffixarray.Index
|
var suffixes *suffixarray.Index
|
||||||
if fulltextIndex {
|
if fulltextIndex {
|
||||||
suffixes = suffixarray.New(x.sources.Bytes())
|
suffixes = suffixarray.New(x.sources.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Index{x.fset, suffixes, words, alts, snippets, x.stats}
|
return &Index{x.fset, suffixes, words, alts, x.snippets, x.stats}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stats() returns index statistics.
|
// Stats() returns index statistics.
|
||||||
|
Loading…
Reference in New Issue
Block a user