Skip to content

feat(packagejson): remove RWMutex contention in InfoCache with atomic flag + SyncMap #1576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions internal/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1669,21 +1669,17 @@ func (r *resolutionState) getPackageJsonInfo(packageDirectory string, onlyRecord
Fields: packageJsonContent,
},
}
if !r.resolver.packageJsonInfoCache.IsReadonly {
r.resolver.packageJsonInfoCache.Set(packageJsonPath, result)
}
result = r.resolver.packageJsonInfoCache.Set(packageJsonPath, result)
r.affectingLocations = append(r.affectingLocations, packageJsonPath)
return result
} else {
if directoryExists && r.tracer != nil {
r.tracer.write(diagnostics.File_0_does_not_exist.Format(packageJsonPath))
}
if !r.resolver.packageJsonInfoCache.IsReadonly {
r.resolver.packageJsonInfoCache.Set(packageJsonPath, &packagejson.InfoCacheEntry{
PackageDirectory: packageDirectory,
DirectoryExists: directoryExists,
})
}
_ = r.resolver.packageJsonInfoCache.Set(packageJsonPath, &packagejson.InfoCacheEntry{
PackageDirectory: packageDirectory,
DirectoryExists: directoryExists,
})
r.failedLookupLocations = append(r.failedLookupLocations, packageJsonPath)
}
return nil
Expand Down
23 changes: 7 additions & 16 deletions internal/packagejson/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ func (p *InfoCacheEntry) GetDirectory() string {
}

type InfoCache struct {
mu sync.RWMutex
IsReadonly bool
cache map[tspath.Path]InfoCacheEntry
cache collections.SyncMap[tspath.Path, *InfoCacheEntry]
currentDirectory string
useCaseSensitiveFileNames bool
}
Expand All @@ -136,22 +134,15 @@ func NewInfoCache(currentDirectory string, useCaseSensitiveFileNames bool) *Info
}

func (p *InfoCache) Get(packageJsonPath string) *InfoCacheEntry {
p.mu.RLock()
defer p.mu.RUnlock()
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
entry, ok := p.cache[key]
if !ok {
return nil
if value, ok := p.cache.Load(key); ok {
return value
}
return &entry
return nil
}

func (p *InfoCache) Set(packageJsonPath string, info *InfoCacheEntry) {
p.mu.Lock()
defer p.mu.Unlock()
func (p *InfoCache) Set(packageJsonPath string, info *InfoCacheEntry) *InfoCacheEntry {
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
if p.cache == nil {
p.cache = make(map[tspath.Path]InfoCacheEntry)
}
p.cache[key] = *info
actual, _ := p.cache.LoadOrStore(key, info)
return actual
}
Loading