Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit ca3d62d

Browse files
committed
gps: source cache: optimize and fix mem cache getAllVersions() via slice store-and-copy and proper locking
1 parent 203c059 commit ca3d62d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

internal/gps/source_cache.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ type singleSourceCache interface {
6161
}
6262

6363
type singleSourceCacheMemory struct {
64-
mut sync.RWMutex // protects all maps
64+
mut sync.RWMutex // protects all fields
6565
infos map[ProjectAnalyzerInfo]map[Revision]projectInfo
6666
ptrees map[Revision]pkgtree.PackageTree
67+
vList []PairedVersion // replaced, never modified
6768
vMap map[UnpairedVersion]Revision
6869
rMap map[Revision][]UnpairedVersion
6970
}
@@ -136,13 +137,14 @@ func (c *singleSourceCacheMemory) getPackageTree(r Revision) (pkgtree.PackageTre
136137

137138
func (c *singleSourceCacheMemory) setVersionMap(versionList []PairedVersion) {
138139
c.mut.Lock()
140+
c.vList = versionList
139141
// TODO(sdboyer) how do we handle cache consistency here - revs that may
140142
// be out of date vis-a-vis the ptrees or infos maps?
141143
for r := range c.rMap {
142144
c.rMap[r] = nil
143145
}
144146

145-
c.vMap = make(map[UnpairedVersion]Revision)
147+
c.vMap = make(map[UnpairedVersion]Revision, len(versionList))
146148

147149
for _, pv := range versionList {
148150
u, r := pv.Unpair(), pv.Revision()
@@ -168,14 +170,16 @@ func (c *singleSourceCacheMemory) getVersionsFor(r Revision) ([]UnpairedVersion,
168170
}
169171

170172
func (c *singleSourceCacheMemory) getAllVersions() []PairedVersion {
171-
if len(c.vMap) == 0 {
173+
c.mut.Lock()
174+
vList := c.vList
175+
c.mut.Unlock()
176+
177+
if vList == nil {
172178
return nil
173179
}
174-
vlist := make([]PairedVersion, 0, len(c.vMap))
175-
for v, r := range c.vMap {
176-
vlist = append(vlist, v.Pair(r))
177-
}
178-
return vlist
180+
cp := make([]PairedVersion, len(vList))
181+
copy(cp, vList)
182+
return cp
179183
}
180184

181185
func (c *singleSourceCacheMemory) getRevisionFor(uv UnpairedVersion) (Revision, bool) {

0 commit comments

Comments
 (0)