Skip to content

Commit 41621a5

Browse files
committed
feat: optimize packagejson cache with concurrent-safe sync/atomic and SyncMap
- Replaced sync.RWMutex with atomic.Bool for thread-safe readonly flag - Switched to collections.SyncMap for concurrent cache access - Updated getPackageJsonInfo to use new IsReadonly() method - Added SetReadonly method for efficient readonly state management - Enhanced Get/Set operations for improved performance and thread safety
1 parent 1ee2ba9 commit 41621a5

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

internal/module/resolver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ func (r *resolutionState) getPackageJsonInfo(packageDirectory string, onlyRecord
16691669
Fields: packageJsonContent,
16701670
},
16711671
}
1672-
if !r.resolver.packageJsonInfoCache.IsReadonly {
1672+
if !r.resolver.packageJsonInfoCache.IsReadonly() {
16731673
r.resolver.packageJsonInfoCache.Set(packageJsonPath, result)
16741674
}
16751675
r.affectingLocations = append(r.affectingLocations, packageJsonPath)
@@ -1678,7 +1678,7 @@ func (r *resolutionState) getPackageJsonInfo(packageDirectory string, onlyRecord
16781678
if directoryExists && r.tracer != nil {
16791679
r.tracer.write(diagnostics.File_0_does_not_exist.Format(packageJsonPath))
16801680
}
1681-
if !r.resolver.packageJsonInfoCache.IsReadonly {
1681+
if !r.resolver.packageJsonInfoCache.IsReadonly() {
16821682
r.resolver.packageJsonInfoCache.Set(packageJsonPath, &packagejson.InfoCacheEntry{
16831683
PackageDirectory: packageDirectory,
16841684
DirectoryExists: directoryExists,

internal/packagejson/cache.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package packagejson
22

33
import (
44
"sync"
5+
"sync/atomic"
56

67
"github.com/microsoft/typescript-go/internal/collections"
78
"github.com/microsoft/typescript-go/internal/core"
@@ -121,9 +122,8 @@ func (p *InfoCacheEntry) GetDirectory() string {
121122
}
122123

123124
type InfoCache struct {
124-
mu sync.RWMutex
125-
IsReadonly bool
126-
cache map[tspath.Path]InfoCacheEntry
125+
cache collections.SyncMap[tspath.Path, *InfoCacheEntry]
126+
isReadonly atomic.Bool
127127
currentDirectory string
128128
useCaseSensitiveFileNames bool
129129
}
@@ -136,22 +136,25 @@ func NewInfoCache(currentDirectory string, useCaseSensitiveFileNames bool) *Info
136136
}
137137

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

149146
func (p *InfoCache) Set(packageJsonPath string, info *InfoCacheEntry) {
150-
p.mu.Lock()
151-
defer p.mu.Unlock()
152-
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
153-
if p.cache == nil {
154-
p.cache = make(map[tspath.Path]InfoCacheEntry)
147+
if p.isReadonly.Load() {
148+
return
155149
}
156-
p.cache[key] = *info
150+
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
151+
p.cache.Store(key, info)
157152
}
153+
154+
func (p *InfoCache) SetReadonly(readonly bool) {
155+
p.isReadonly.Store(readonly)
156+
}
157+
158+
func (p *InfoCache) IsReadonly() bool {
159+
return p.isReadonly.Load()
160+
}

0 commit comments

Comments
 (0)