mirror of
https://github.com/golang/go
synced 2024-10-01 05:18:33 -06:00
ecd3fc4348
We use file identities pervasively throughout gopls. Prior to this change, the identity is the modification date of an unopened file, or the hash of an opened file. That means that opening a file changes its identity, which causes unnecessary churn in the cache. Unfortunately, there isn't an easy way to fix this. Changing the cache key to something else, such as the modification time, means that we won't unify cache entries if a change is made and then undone. The approach here is to read files eagerly in GetFile, so that we know their hashes immediately. That resolves the churn, but means that we do a ton of file IO at startup. Incidental changes: Remove the FileSystem interface; there was only one implementation and it added a fair amount of cruft. We have many other places that assume os.Stat and such work. Add direct accessors to FileHandle for URI, Kind, and Version. Most uses of (FileHandle).Identity were for stuff that we derive solely from the URI, and this helped me disentangle them. It is a *ton* of churn, though. I can revert it if you want. Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
158 lines
3.4 KiB
Go
158 lines
3.4 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha1"
|
|
"fmt"
|
|
"go/token"
|
|
"io/ioutil"
|
|
"os"
|
|
"reflect"
|
|
"strconv"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"golang.org/x/tools/internal/event"
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/memoize"
|
|
"golang.org/x/tools/internal/span"
|
|
errors "golang.org/x/xerrors"
|
|
)
|
|
|
|
func New(ctx context.Context, options func(*source.Options)) *Cache {
|
|
index := atomic.AddInt64(&cacheIndex, 1)
|
|
c := &Cache{
|
|
id: strconv.FormatInt(index, 10),
|
|
fset: token.NewFileSet(),
|
|
options: options,
|
|
}
|
|
return c
|
|
}
|
|
|
|
type Cache struct {
|
|
id string
|
|
fset *token.FileSet
|
|
options func(*source.Options)
|
|
|
|
store memoize.Store
|
|
}
|
|
|
|
type fileKey struct {
|
|
uri span.URI
|
|
modTime time.Time
|
|
}
|
|
|
|
type fileHandle struct {
|
|
uri span.URI
|
|
memoize.NoCopy
|
|
bytes []byte
|
|
hash string
|
|
err error
|
|
}
|
|
|
|
func (c *Cache) GetFile(ctx context.Context, uri span.URI) (source.FileHandle, error) {
|
|
var modTime time.Time
|
|
if fi, err := os.Stat(uri.Filename()); err == nil {
|
|
modTime = fi.ModTime()
|
|
}
|
|
|
|
key := fileKey{
|
|
uri: uri,
|
|
modTime: modTime,
|
|
}
|
|
h := c.store.Bind(key, func(ctx context.Context) interface{} {
|
|
return readFile(ctx, uri, modTime)
|
|
})
|
|
v := h.Get(ctx)
|
|
if v == nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
return v.(*fileHandle), nil
|
|
}
|
|
|
|
// ioLimit limits the number of parallel file reads per process.
|
|
var ioLimit = make(chan struct{}, 128)
|
|
|
|
func readFile(ctx context.Context, uri span.URI, origTime time.Time) *fileHandle {
|
|
ctx, done := event.Start(ctx, "cache.getFile", tag.File.Of(uri.Filename()))
|
|
_ = ctx
|
|
defer done()
|
|
|
|
ioLimit <- struct{}{}
|
|
defer func() { <-ioLimit }()
|
|
|
|
var modTime time.Time
|
|
if fi, err := os.Stat(uri.Filename()); err == nil {
|
|
modTime = fi.ModTime()
|
|
}
|
|
|
|
if modTime != origTime {
|
|
return &fileHandle{err: errors.Errorf("%s: file has been modified", uri.Filename())}
|
|
}
|
|
data, err := ioutil.ReadFile(uri.Filename())
|
|
if err != nil {
|
|
return &fileHandle{err: err}
|
|
}
|
|
return &fileHandle{
|
|
uri: uri,
|
|
bytes: data,
|
|
hash: hashContents(data),
|
|
}
|
|
}
|
|
|
|
func (c *Cache) NewSession(ctx context.Context) *Session {
|
|
index := atomic.AddInt64(&sessionIndex, 1)
|
|
s := &Session{
|
|
cache: c,
|
|
id: strconv.FormatInt(index, 10),
|
|
options: source.DefaultOptions(),
|
|
overlays: make(map[span.URI]*overlay),
|
|
}
|
|
event.Log(ctx, "New session", KeyCreateSession.Of(s))
|
|
return s
|
|
}
|
|
|
|
func (c *Cache) FileSet() *token.FileSet {
|
|
return c.fset
|
|
}
|
|
|
|
func (h *fileHandle) URI() span.URI {
|
|
return h.uri
|
|
}
|
|
|
|
func (h *fileHandle) Kind() source.FileKind {
|
|
return source.DetectLanguage("", h.uri.Filename())
|
|
}
|
|
|
|
func (h *fileHandle) Version() float64 {
|
|
return 0
|
|
}
|
|
|
|
func (h *fileHandle) Identity() source.FileIdentity {
|
|
return source.FileIdentity{
|
|
URI: h.uri,
|
|
Identifier: h.hash,
|
|
Kind: h.Kind(),
|
|
}
|
|
}
|
|
|
|
func (h *fileHandle) Read() ([]byte, error) {
|
|
return h.bytes, h.err
|
|
}
|
|
|
|
func hashContents(contents []byte) string {
|
|
// TODO: consider whether sha1 is the best choice here
|
|
// This hash is used for internal identity detection only
|
|
return fmt.Sprintf("%x", sha1.Sum(contents))
|
|
}
|
|
|
|
var cacheIndex, sessionIndex, viewIndex int64
|
|
|
|
func (c *Cache) ID() string { return c.id }
|
|
func (c *Cache) MemStats() map[reflect.Type]int { return c.store.Stats() }
|