Skip to content

Commit 6e6df72

Browse files
authored
feat(packagejson): remove RWMutex contention in InfoCache with atomic flag + SyncMap (#1576)
1 parent 0faeeca commit 6e6df72

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

internal/module/resolver.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,21 +1669,17 @@ func (r *resolutionState) getPackageJsonInfo(packageDirectory string, onlyRecord
16691669
Fields: packageJsonContent,
16701670
},
16711671
}
1672-
if !r.resolver.packageJsonInfoCache.IsReadonly {
1673-
r.resolver.packageJsonInfoCache.Set(packageJsonPath, result)
1674-
}
1672+
result = r.resolver.packageJsonInfoCache.Set(packageJsonPath, result)
16751673
r.affectingLocations = append(r.affectingLocations, packageJsonPath)
16761674
return result
16771675
} else {
16781676
if directoryExists && r.tracer != nil {
16791677
r.tracer.write(diagnostics.File_0_does_not_exist.Format(packageJsonPath))
16801678
}
1681-
if !r.resolver.packageJsonInfoCache.IsReadonly {
1682-
r.resolver.packageJsonInfoCache.Set(packageJsonPath, &packagejson.InfoCacheEntry{
1683-
PackageDirectory: packageDirectory,
1684-
DirectoryExists: directoryExists,
1685-
})
1686-
}
1679+
_ = r.resolver.packageJsonInfoCache.Set(packageJsonPath, &packagejson.InfoCacheEntry{
1680+
PackageDirectory: packageDirectory,
1681+
DirectoryExists: directoryExists,
1682+
})
16871683
r.failedLookupLocations = append(r.failedLookupLocations, packageJsonPath)
16881684
}
16891685
return nil

internal/packagejson/cache.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ func (p *InfoCacheEntry) GetDirectory() string {
121121
}
122122

123123
type InfoCache struct {
124-
mu sync.RWMutex
125-
IsReadonly bool
126-
cache map[tspath.Path]InfoCacheEntry
124+
cache collections.SyncMap[tspath.Path, *InfoCacheEntry]
127125
currentDirectory string
128126
useCaseSensitiveFileNames bool
129127
}
@@ -136,22 +134,15 @@ func NewInfoCache(currentDirectory string, useCaseSensitiveFileNames bool) *Info
136134
}
137135

138136
func (p *InfoCache) Get(packageJsonPath string) *InfoCacheEntry {
139-
p.mu.RLock()
140-
defer p.mu.RUnlock()
141137
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
142-
entry, ok := p.cache[key]
143-
if !ok {
144-
return nil
138+
if value, ok := p.cache.Load(key); ok {
139+
return value
145140
}
146-
return &entry
141+
return nil
147142
}
148143

149-
func (p *InfoCache) Set(packageJsonPath string, info *InfoCacheEntry) {
150-
p.mu.Lock()
151-
defer p.mu.Unlock()
144+
func (p *InfoCache) Set(packageJsonPath string, info *InfoCacheEntry) *InfoCacheEntry {
152145
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
153-
if p.cache == nil {
154-
p.cache = make(map[tspath.Path]InfoCacheEntry)
155-
}
156-
p.cache[key] = *info
146+
actual, _ := p.cache.LoadOrStore(key, info)
147+
return actual
157148
}

0 commit comments

Comments
 (0)