Debian Codegraph @

gopls/internal/cache/cache.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package cache
6
7 import (
8 "reflect"
9 "strconv"
10 "sync/atomic"
11
12 "golang.org/x/tools/gopls/internal/protocol/command"
13 "golang.org/x/tools/gopls/internal/util/memoize"
14 "golang.org/x/tools/internal/imports"
15 )
16
17 // ballast is a 100MB unused byte slice that exists only to reduce garbage
18 // collector CPU in small workspaces and at startup.
19 //
20 // The redesign of gopls described at https://go.dev/blog/gopls-scalability
21 // moved gopls to a model where it has a significantly smaller heap, yet still
22 // allocates many short-lived data structures during parsing and type checking.
23 // As a result, for some workspaces, particularly when opening a low-level
24 // package, the steady-state heap may be a small fraction of total allocation
25 // while rechecking the workspace, paradoxically causing the GC to consume much
26 // more CPU. For example, in one benchmark that analyzes the starlark
27 // repository, the steady-state heap was ~10MB, and the process of diagnosing
28 // the workspace allocated 100-200MB.
29 //
30 // The reason for this paradoxical behavior is that GC pacing
31 // (https://tip.golang.org/doc/gc-guide#GOGC) causes the collector to trigger
32 // at some multiple of the steady-state heap size, so a small steady-state heap
33 // causes GC to trigger sooner and more often when allocating the ephemeral
34 // structures.
35 //
36 // Allocating a 100MB ballast avoids this problem by ensuring a minimum heap
37 // size. The value of 100MB was chosen to be proportional to the in-memory
38 // cache in front the filecache package, and the throughput of type checking.
39 // Gopls already requires hundreds of megabytes of RAM to function.
40 //
41 // Note that while other use cases for a ballast were made obsolete by
42 // GOMEMLIMIT, ours is not. GOMEMLIMIT helps in cases where you have a
43 // containerized service and want to optimize its latency and throughput by
44 // taking advantage of available memory. However, in our case gopls is running
45 // on the developer's machine alongside other applications, and can have a wide
46 // range of memory footprints depending on the size of the user's workspace.
47 // Setting GOMEMLIMIT to too low a number would make gopls perform poorly on
48 // large repositories, and setting it to too high a number would make gopls a
49 // badly behaved tenant. Short of calibrating GOMEMLIMIT based on the user's
50 // workspace (an intractible problem), there is no way for gopls to use
51 // GOMEMLIMIT to solve its GC CPU problem.
52 //
53 // Because this allocation is large and occurs early, there is a good chance
54 // that rather than being recycled, it comes directly from the OS already
55 // zeroed, and since it is never accessed, the memory region may avoid being
56 // backed by pages of RAM. But see
57 // https://groups.google.com/g/golang-nuts/c/66d0cItfkjY/m/3NvgzL_sAgAJ
58 //
59 // For more details on this technique, see:
60 // https://blog.twitch.tv/en/2019/04/10/go-memory-ballast-how-i-learnt-to-stop-worrying-and-love-the-heap/
61 var ballast = make([]byte, 100*1e6)
62
63 // New Creates a new cache for gopls operation results, using the given file
64 // set, shared store, and session options.
65 //
66 // Both the fset and store may be nil, but if store is non-nil so must be fset
67 // (and they must always be used together), otherwise it may be possible to get
68 // cached data referencing token.Pos values not mapped by the FileSet.
69 func New(store *memoize.Store) *Cache {
70 index := atomic.AddInt64(&cacheIndex, 1)
71
72 if store == nil {
73 store = &memoize.Store{}
74 }
75
76 c := &Cache{
77 id: strconv.FormatInt(index, 10),
78 store: store,
79 memoizedFS: newMemoizedFS(),
80 modCache: &sharedModCache{
81 caches: make(map[string]*imports.DirInfoCache),
82 timers: make(map[string]*refreshTimer),
83 },
84 }
85 return c
86 }
87
88 // A Cache holds content that is shared across multiple gopls sessions.
89 type Cache struct {
90 id string
91
92 // store holds cached calculations.
93 //
94 // TODO(rfindley): at this point, these are not important, as we've moved our
95 // content-addressable cache to the file system (the filecache package). It
96 // is unlikely that this shared cache provides any shared value. We should
97 // consider removing it, replacing current uses with a simpler futures cache,
98 // as we've done for e.g. type-checked packages.
99 store *memoize.Store
100
101 // memoizedFS holds a shared file.Source that caches reads.
102 //
103 // Reads are invalidated when *any* session gets a didChangeWatchedFile
104 // notification. This is fine: it is the responsibility of memoizedFS to hold
105 // our best knowledge of the current file system state.
106 *memoizedFS
107
108 // modCache holds the shared goimports state for GOMODCACHE directories
109 modCache *sharedModCache
110 }
111
112 var cacheIndex, sessionIndex, viewIndex int64
113
114 func (c *Cache) ID() string { return c.id }
115 func (c *Cache) MemStats() map[reflect.Type]int { return c.store.Stats() }
116
117 // FileStats returns information about the set of files stored in the cache.
118 // It is intended for debugging only.
119 func (c *Cache) FileStats() (stats command.FileStats) {
120 stats.Total, stats.Largest, stats.Errs = c.fileStats()
121 return
122 }