|
1 | 1 | package project |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "sync" |
5 | | - |
6 | | - "github.com/microsoft/typescript-go/internal/collections" |
7 | 4 | "github.com/microsoft/typescript-go/internal/tsoptions" |
8 | 5 | "github.com/microsoft/typescript-go/internal/tspath" |
9 | 6 | "github.com/zeebo/xxh3" |
10 | 7 | ) |
11 | 8 |
|
12 | | -type ExtendedConfigCache struct { |
13 | | - entries collections.SyncMap[tspath.Path, *extendedConfigCacheEntry] |
| 9 | +type ExtendedConfigParseArgs struct { |
| 10 | + FileName string |
| 11 | + Content string |
| 12 | + FS FileSource |
| 13 | + ResolutionStack []string |
| 14 | + Host tsoptions.ParseConfigHost |
| 15 | + Cache tsoptions.ExtendedConfigCache |
14 | 16 | } |
15 | 17 |
|
16 | | -type extendedConfigCacheEntry struct { |
17 | | - mu sync.Mutex |
18 | | - entry *tsoptions.ExtendedConfigCacheEntry |
19 | | - hash xxh3.Uint128 |
20 | | - refCount int |
| 18 | +type ExtendedConfigCacheEntry struct { |
| 19 | + *tsoptions.ExtendedConfigCacheEntry |
| 20 | + Hash xxh3.Uint128 |
21 | 21 | } |
22 | 22 |
|
23 | | -func (c *ExtendedConfigCache) Acquire(fh FileHandle, path tspath.Path, parse func() *tsoptions.ExtendedConfigCacheEntry) *tsoptions.ExtendedConfigCacheEntry { |
24 | | - entry, loaded := c.loadOrStoreNewLockedEntry(path) |
25 | | - defer entry.mu.Unlock() |
26 | | - var hash xxh3.Uint128 |
27 | | - if fh != nil { |
28 | | - hash = fh.Hash() |
29 | | - } |
30 | | - if !loaded || entry.hash != hash { |
31 | | - // Reparse the config if the hash has changed, or parse for the first time. |
32 | | - entry.entry = parse() |
33 | | - entry.hash = hash |
34 | | - } |
35 | | - return entry.entry |
36 | | -} |
| 23 | +type ExtendedConfigCache = RefCountCache[tspath.Path, *ExtendedConfigCacheEntry, ExtendedConfigParseArgs] |
37 | 24 |
|
38 | | -func (c *ExtendedConfigCache) Ref(path tspath.Path) { |
39 | | - if entry, ok := c.entries.Load(path); ok { |
40 | | - entry.mu.Lock() |
41 | | - if entry.refCount <= 0 { |
42 | | - // Entry was deleted while we were acquiring the lock |
43 | | - newEntry, loaded := c.loadOrStoreNewLockedEntry(path) |
44 | | - if !loaded { |
45 | | - newEntry.entry = entry.entry |
46 | | - newEntry.hash = entry.hash |
| 25 | +func NewExtendedConfigCache() *ExtendedConfigCache { |
| 26 | + return NewRefCountCache( |
| 27 | + RefCountCacheOptions{}, |
| 28 | + func(path tspath.Path, args ExtendedConfigParseArgs) *ExtendedConfigCacheEntry { |
| 29 | + result := &ExtendedConfigCacheEntry{ |
| 30 | + ExtendedConfigCacheEntry: tsoptions.ParseExtendedConfig(args.FileName, path, args.ResolutionStack, args.Host, args.Cache), |
47 | 31 | } |
48 | | - entry.mu.Unlock() |
49 | | - newEntry.mu.Unlock() |
50 | | - return |
51 | | - } |
52 | | - entry.refCount++ |
53 | | - entry.mu.Unlock() |
54 | | - } |
55 | | -} |
56 | | - |
57 | | -func (c *ExtendedConfigCache) Deref(path tspath.Path) { |
58 | | - if entry, ok := c.entries.Load(path); ok { |
59 | | - entry.mu.Lock() |
60 | | - entry.refCount-- |
61 | | - remove := entry.refCount <= 0 |
62 | | - if remove { |
63 | | - c.entries.Delete(path) |
64 | | - } |
65 | | - entry.mu.Unlock() |
66 | | - } |
67 | | -} |
68 | | - |
69 | | -func (c *ExtendedConfigCache) Has(path tspath.Path) bool { |
70 | | - _, ok := c.entries.Load(path) |
71 | | - return ok |
| 32 | + result.Hash = hash(result.ExtendedConfigCacheEntry, args) |
| 33 | + return result |
| 34 | + }, |
| 35 | + func(path tspath.Path, entry *ExtendedConfigCacheEntry, args ExtendedConfigParseArgs) bool { |
| 36 | + return entry.Hash == xxh3.Uint128{} || entry.Hash != hash(entry.ExtendedConfigCacheEntry, args) |
| 37 | + }, |
| 38 | + ) |
72 | 39 | } |
73 | 40 |
|
74 | | -// loadOrStoreNewLockedEntry loads an existing entry or creates a new one. The returned |
75 | | -// entry's mutex is locked and its refCount is incremented (or initialized to 1 |
76 | | -// in the case of a new entry). |
77 | | -func (c *ExtendedConfigCache) loadOrStoreNewLockedEntry(path tspath.Path) (*extendedConfigCacheEntry, bool) { |
78 | | - entry := &extendedConfigCacheEntry{refCount: 1} |
79 | | - entry.mu.Lock() |
80 | | - if existing, loaded := c.entries.LoadOrStore(path, entry); loaded { |
81 | | - existing.mu.Lock() |
82 | | - if existing.refCount <= 0 { |
83 | | - // Entry was deleted while we were acquiring the lock |
84 | | - existing.mu.Unlock() |
85 | | - return c.loadOrStoreNewLockedEntry(path) |
| 41 | +func hash(entry *tsoptions.ExtendedConfigCacheEntry, args ExtendedConfigParseArgs) xxh3.Uint128 { |
| 42 | + hasher := xxh3.New() |
| 43 | + _, _ = hasher.WriteString(args.Content) |
| 44 | + for _, fileName := range entry.ExtendedFileNames() { |
| 45 | + fh := args.FS.GetFile(fileName) |
| 46 | + if fh == nil { |
| 47 | + return xxh3.Uint128{} |
86 | 48 | } |
87 | | - existing.refCount++ |
88 | | - return existing, true |
| 49 | + _, _ = hasher.WriteString(fh.Content()) |
89 | 50 | } |
90 | | - return entry, false |
| 51 | + return hasher.Sum128() |
91 | 52 | } |
0 commit comments