Skip to content

Commit 6f567c8

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/lsp/filecache: reduce lifespan of os.FileInfos
These objects accounted for 30MB in a recent heap profile since they are large structs. This change selects just the two fields we need. Change-Id: I4b1ab713d82a73e851785c42784b1bfff75341c3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/500635 Run-TryBot: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 726c727 commit 6f567c8

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

gopls/internal/lsp/filecache/filecache.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,9 @@ func gc(goplsDir string) {
427427
for {
428428
// Enumerate all files in the cache.
429429
type item struct {
430-
path string
431-
stat os.FileInfo
430+
path string
431+
mtime time.Time
432+
size int64
432433
}
433434
var files []item
434435
start := time.Now()
@@ -454,7 +455,7 @@ func gc(goplsDir string) {
454455
}
455456
os.Remove(path) // ignore error
456457
} else {
457-
files = append(files, item{path, stat})
458+
files = append(files, item{path, stat.ModTime(), stat.Size()})
458459
total += stat.Size()
459460
if debug && len(files)%1000 == 0 {
460461
log.Printf("filecache: checked %d files in %v", len(files), time.Since(start))
@@ -469,7 +470,7 @@ func gc(goplsDir string) {
469470

470471
// Sort oldest files first.
471472
sort.Slice(files, func(i, j int) bool {
472-
return files[i].stat.ModTime().Before(files[j].stat.ModTime())
473+
return files[i].mtime.Before(files[j].mtime)
473474
})
474475

475476
// Delete oldest files until we're under budget.
@@ -479,13 +480,14 @@ func gc(goplsDir string) {
479480
break
480481
}
481482
if debug {
482-
age := time.Since(file.stat.ModTime())
483+
age := time.Since(file.mtime)
483484
log.Printf("budget: deleting stale file %s (%dB, age %v)",
484-
file.path, file.stat.Size(), age)
485+
file.path, file.size, age)
485486
}
486487
os.Remove(file.path) // ignore error
487-
total -= file.stat.Size()
488+
total -= file.size
488489
}
490+
files = nil // release memory before sleep
489491

490492
time.Sleep(period)
491493

0 commit comments

Comments
 (0)