| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"container/heap" |
| 10 |
"context" |
| 11 |
"fmt" |
| 12 |
"go/parser" |
| 13 |
"go/token" |
| 14 |
"math/bits" |
| 15 |
"runtime" |
| 16 |
"sync" |
| 17 |
"time" |
| 18 |
|
| 19 |
"golang.org/x/sync/errgroup" |
| 20 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 21 |
"golang.org/x/tools/gopls/internal/file" |
| 22 |
"golang.org/x/tools/gopls/internal/protocol" |
| 23 |
"golang.org/x/tools/gopls/internal/util/memoize" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
const reservedForParsing = 1 << (bits.UintSize - 4) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func fileSetWithBase(base int) *token.FileSet { |
| 55 |
fset := token.NewFileSet() |
| 56 |
if base > 1 { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
fset.AddFile("", base-1, 0) |
| 63 |
} |
| 64 |
if fset.Base() != base { |
| 65 |
panic("unexpected FileSet.Base") |
| 66 |
} |
| 67 |
return fset |
| 68 |
} |
| 69 |
|
| 70 |
const ( |
| 71 |
|
| 72 |
|
| 73 |
parseCacheMinFiles = 100 |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
var parsePadding = 1000 |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
type parseCache struct { |
| 98 |
expireAfter time.Duration |
| 99 |
done chan struct{} |
| 100 |
|
| 101 |
mu sync.Mutex |
| 102 |
m map[parseKey]*parseCacheEntry |
| 103 |
lru queue |
| 104 |
clock uint64 |
| 105 |
nextBase int |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
func newParseCache(expireAfter time.Duration) *parseCache { |
| 113 |
c := &parseCache{ |
| 114 |
expireAfter: expireAfter, |
| 115 |
m: make(map[parseKey]*parseCacheEntry), |
| 116 |
done: make(chan struct{}), |
| 117 |
} |
| 118 |
go c.gc() |
| 119 |
return c |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
func (c *parseCache) stop() { |
| 124 |
close(c.done) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
type parseKey struct { |
| 129 |
uri protocol.DocumentURI |
| 130 |
mode parser.Mode |
| 131 |
purgeFuncBodies bool |
| 132 |
} |
| 133 |
|
| 134 |
type parseCacheEntry struct { |
| 135 |
key parseKey |
| 136 |
hash file.Hash |
| 137 |
promise *memoize.Promise |
| 138 |
atime uint64 |
| 139 |
walltime time.Time |
| 140 |
lruIndex int |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
func (c *parseCache) startParse(mode parser.Mode, purgeFuncBodies bool, fhs ...file.Handle) ([]*memoize.Promise, error) { |
| 150 |
c.mu.Lock() |
| 151 |
defer c.mu.Unlock() |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
c.clock++ |
| 158 |
walltime := time.Now() |
| 159 |
|
| 160 |
|
| 161 |
var ( |
| 162 |
data = make([][]byte, len(fhs)) |
| 163 |
promises = make([]*memoize.Promise, len(fhs)) |
| 164 |
firstReadError error |
| 165 |
) |
| 166 |
for i, fh := range fhs { |
| 167 |
content, err := fh.Content() |
| 168 |
if err != nil { |
| 169 |
if firstReadError == nil { |
| 170 |
firstReadError = err |
| 171 |
} |
| 172 |
continue |
| 173 |
} |
| 174 |
data[i] = content |
| 175 |
|
| 176 |
key := parseKey{ |
| 177 |
uri: fh.URI(), |
| 178 |
mode: mode, |
| 179 |
purgeFuncBodies: purgeFuncBodies, |
| 180 |
} |
| 181 |
|
| 182 |
if e, ok := c.m[key]; ok { |
| 183 |
if e.hash == fh.Identity().Hash { |
| 184 |
e.atime = c.clock |
| 185 |
e.walltime = walltime |
| 186 |
heap.Fix(&c.lru, e.lruIndex) |
| 187 |
promises[i] = e.promise |
| 188 |
continue |
| 189 |
} else { |
| 190 |
|
| 191 |
delete(c.m, e.key) |
| 192 |
heap.Remove(&c.lru, e.lruIndex) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
uri := fh.URI() |
| 197 |
promise := memoize.NewPromise("parseCache.parse", func(ctx context.Context, _ any) any { |
| 198 |
|
| 199 |
|
| 200 |
base, nextBase := c.allocateSpace(2*len(content) + parsePadding) |
| 201 |
|
| 202 |
pgf, fixes1 := parsego.Parse(ctx, fileSetWithBase(base), uri, content, mode, purgeFuncBodies) |
| 203 |
file := pgf.Tok |
| 204 |
if file.Base()+file.Size()+1 > nextBase { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
actual := file.Base() + file.Size() - base |
| 213 |
base2, nextBase2 := c.allocateSpace(actual) |
| 214 |
pgf2, fixes2 := parsego.Parse(ctx, fileSetWithBase(base2), uri, content, mode, purgeFuncBodies) |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if end := pgf2.Tok.Base() + pgf2.Tok.Size(); end != nextBase2-1 { |
| 220 |
var errBuf bytes.Buffer |
| 221 |
fmt.Fprintf(&errBuf, "internal error: non-deterministic parsing result:\n") |
| 222 |
fmt.Fprintf(&errBuf, "\t%q (%d-%d) does not span %d-%d\n", uri, pgf2.Tok.Base(), base2, end, nextBase2-1) |
| 223 |
fmt.Fprintf(&errBuf, "\tfirst %q (%d-%d)\n", pgf.URI, pgf.Tok.Base(), pgf.Tok.Base()+pgf.Tok.Size()) |
| 224 |
fmt.Fprintf(&errBuf, "\tfirst space: (%d-%d), second space: (%d-%d)\n", base, nextBase, base2, nextBase2) |
| 225 |
fmt.Fprintf(&errBuf, "\tfirst mode: %v, second mode: %v", pgf.Mode, pgf2.Mode) |
| 226 |
fmt.Fprintf(&errBuf, "\tfirst err: %v, second err: %v", pgf.ParseErr, pgf2.ParseErr) |
| 227 |
fmt.Fprintf(&errBuf, "\tfirst fixes: %v, second fixes: %v", fixes1, fixes2) |
| 228 |
panic(errBuf.String()) |
| 229 |
} |
| 230 |
pgf = pgf2 |
| 231 |
} |
| 232 |
return pgf |
| 233 |
}) |
| 234 |
promises[i] = promise |
| 235 |
|
| 236 |
|
| 237 |
e := &parseCacheEntry{ |
| 238 |
key: key, |
| 239 |
hash: fh.Identity().Hash, |
| 240 |
promise: promise, |
| 241 |
atime: c.clock, |
| 242 |
walltime: walltime, |
| 243 |
} |
| 244 |
c.m[e.key] = e |
| 245 |
heap.Push(&c.lru, e) |
| 246 |
} |
| 247 |
|
| 248 |
if len(c.m) != len(c.lru) { |
| 249 |
panic("map and LRU are inconsistent") |
| 250 |
} |
| 251 |
|
| 252 |
return promises, firstReadError |
| 253 |
} |
| 254 |
|
| 255 |
func (c *parseCache) gc() { |
| 256 |
const period = 10 * time.Second |
| 257 |
timer := time.NewTicker(period) |
| 258 |
defer timer.Stop() |
| 259 |
|
| 260 |
for { |
| 261 |
select { |
| 262 |
case <-c.done: |
| 263 |
return |
| 264 |
case <-timer.C: |
| 265 |
} |
| 266 |
|
| 267 |
c.gcOnce() |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
func (c *parseCache) gcOnce() { |
| 272 |
now := time.Now() |
| 273 |
c.mu.Lock() |
| 274 |
defer c.mu.Unlock() |
| 275 |
|
| 276 |
for len(c.m) > parseCacheMinFiles { |
| 277 |
e := heap.Pop(&c.lru).(*parseCacheEntry) |
| 278 |
if now.Sub(e.walltime) >= c.expireAfter { |
| 279 |
delete(c.m, e.key) |
| 280 |
} else { |
| 281 |
heap.Push(&c.lru, e) |
| 282 |
break |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
func (c *parseCache) allocateSpace(size int) (int, int) { |
| 293 |
c.mu.Lock() |
| 294 |
defer c.mu.Unlock() |
| 295 |
|
| 296 |
if c.nextBase == 0 { |
| 297 |
|
| 298 |
c.nextBase = 1 |
| 299 |
} |
| 300 |
base := c.nextBase |
| 301 |
c.nextBase += size + 1 |
| 302 |
return base, c.nextBase |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
func (c *parseCache) parseFiles(ctx context.Context, fset *token.FileSet, mode parser.Mode, purgeFuncBodies bool, fhs ...file.Handle) ([]*parsego.File, error) { |
| 320 |
pgfs := make([]*parsego.File, len(fhs)) |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if bits.UintSize == 32 { |
| 327 |
for i, fh := range fhs { |
| 328 |
var err error |
| 329 |
pgfs[i], err = parseGoImpl(ctx, fset, fh, mode, purgeFuncBodies) |
| 330 |
if err != nil { |
| 331 |
return pgfs, err |
| 332 |
} |
| 333 |
} |
| 334 |
return pgfs, nil |
| 335 |
} |
| 336 |
|
| 337 |
promises, firstErr := c.startParse(mode, purgeFuncBodies, fhs...) |
| 338 |
|
| 339 |
|
| 340 |
var g errgroup.Group |
| 341 |
g.SetLimit(runtime.GOMAXPROCS(-1)) |
| 342 |
for i, promise := range promises { |
| 343 |
if promise == nil { |
| 344 |
continue |
| 345 |
} |
| 346 |
i := i |
| 347 |
promise := promise |
| 348 |
g.Go(func() error { |
| 349 |
result, err := promise.Get(ctx, nil) |
| 350 |
if err != nil { |
| 351 |
return err |
| 352 |
} |
| 353 |
pgfs[i] = result.(*parsego.File) |
| 354 |
return nil |
| 355 |
}) |
| 356 |
} |
| 357 |
|
| 358 |
if err := g.Wait(); err != nil && firstErr == nil { |
| 359 |
firstErr = err |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
var tokenFiles []*token.File |
| 364 |
for _, pgf := range pgfs { |
| 365 |
if pgf == nil { |
| 366 |
continue |
| 367 |
} |
| 368 |
tokenFiles = append(tokenFiles, pgf.Tok) |
| 369 |
} |
| 370 |
fset.AddExistingFiles(tokenFiles...) |
| 371 |
|
| 372 |
const debugIssue59080 = true |
| 373 |
if debugIssue59080 { |
| 374 |
for _, f := range tokenFiles { |
| 375 |
pos := token.Pos(f.Base()) |
| 376 |
f2 := fset.File(pos) |
| 377 |
if f2 != f { |
| 378 |
panic(fmt.Sprintf("internal error: File(%d (start)) = %v, not %v", pos, f2, f)) |
| 379 |
} |
| 380 |
pos = token.Pos(f.Base() + f.Size()) |
| 381 |
f2 = fset.File(pos) |
| 382 |
if f2 != f { |
| 383 |
panic(fmt.Sprintf("internal error: File(%d (end)) = %v, not %v", pos, f2, f)) |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
return pgfs, firstErr |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
type queue []*parseCacheEntry |
| 395 |
|
| 396 |
func (q queue) Len() int { return len(q) } |
| 397 |
|
| 398 |
func (q queue) Less(i, j int) bool { return q[i].atime < q[j].atime } |
| 399 |
|
| 400 |
func (q queue) Swap(i, j int) { |
| 401 |
q[i], q[j] = q[j], q[i] |
| 402 |
q[i].lruIndex = i |
| 403 |
q[j].lruIndex = j |
| 404 |
} |
| 405 |
|
| 406 |
func (q *queue) Push(x any) { |
| 407 |
e := x.(*parseCacheEntry) |
| 408 |
e.lruIndex = len(*q) |
| 409 |
*q = append(*q, e) |
| 410 |
} |
| 411 |
|
| 412 |
func (q *queue) Pop() any { |
| 413 |
last := len(*q) - 1 |
| 414 |
e := (*q)[last] |
| 415 |
(*q)[last] = nil |
| 416 |
*q = (*q)[:last] |
| 417 |
return e |
| 418 |
} |