Skip to content

Commit e36bb84

Browse files
committed
Squash real path cache with the existing cache
1 parent 548ce9b commit e36bb84

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ class DependencyScanningFilesystemSharedCache {
156156
/// The mutex that needs to be locked before mutation of any member.
157157
mutable std::mutex CacheLock;
158158

159-
/// Map from filenames to cached entries.
160-
llvm::StringMap<const CachedFileSystemEntry *, llvm::BumpPtrAllocator>
161-
EntriesByFilename;
159+
/// Map from filenames to cached entries and real paths.
160+
llvm::StringMap<
161+
std::pair<const CachedFileSystemEntry *, const CachedRealPath *>,
162+
llvm::BumpPtrAllocator>
163+
CacheByFilename;
162164

163165
/// Map from unique IDs to cached entries.
164166
llvm::DenseMap<llvm::sys::fs::UniqueID, const CachedFileSystemEntry *>
@@ -170,9 +172,6 @@ class DependencyScanningFilesystemSharedCache {
170172
/// The backing storage for cached contents.
171173
llvm::SpecificBumpPtrAllocator<CachedFileContents> ContentsStorage;
172174

173-
/// Map from filenames to cached real paths.
174-
llvm::StringMap<const CachedRealPath *> RealPathsByFilename;
175-
176175
/// The backing storage for cached real paths.
177176
llvm::SpecificBumpPtrAllocator<CachedRealPath> RealPathStorage;
178177

@@ -229,16 +228,17 @@ class DependencyScanningFilesystemSharedCache {
229228
/// This class is a local cache, that caches the 'stat' and 'open' calls to the
230229
/// underlying real file system.
231230
class DependencyScanningFilesystemLocalCache {
232-
llvm::StringMap<const CachedFileSystemEntry *, llvm::BumpPtrAllocator> Cache;
233-
234-
llvm::StringMap<const CachedRealPath *, llvm::BumpPtrAllocator> RealPathCache;
231+
llvm::StringMap<
232+
std::pair<const CachedFileSystemEntry *, const CachedRealPath *>,
233+
llvm::BumpPtrAllocator>
234+
Cache;
235235

236236
public:
237237
/// Returns entry associated with the filename or nullptr if none is found.
238238
const CachedFileSystemEntry *findEntryByFilename(StringRef Filename) const {
239239
assert(llvm::sys::path::is_absolute_gnu(Filename));
240240
auto It = Cache.find(Filename);
241-
return It == Cache.end() ? nullptr : It->getValue();
241+
return It == Cache.end() ? nullptr : It->getValue().first;
242242
}
243243

244244
/// Associates the given entry with the filename and returns the given entry
@@ -247,17 +247,17 @@ class DependencyScanningFilesystemLocalCache {
247247
insertEntryForFilename(StringRef Filename,
248248
const CachedFileSystemEntry &Entry) {
249249
assert(llvm::sys::path::is_absolute_gnu(Filename));
250-
const auto *InsertedEntry = Cache.insert({Filename, &Entry}).first->second;
251-
assert(InsertedEntry == &Entry && "entry already present");
252-
return *InsertedEntry;
250+
assert(Cache[Filename].first == nullptr && "entry already present");
251+
Cache[Filename].first = &Entry;
252+
return Entry;
253253
}
254254

255255
/// Returns real path associated with the filename or nullptr if none is
256256
/// found.
257257
const CachedRealPath *findRealPathByFilename(StringRef Filename) const {
258258
assert(llvm::sys::path::is_absolute_gnu(Filename));
259-
auto It = RealPathCache.find(Filename);
260-
return It == RealPathCache.end() ? nullptr : It->getValue();
259+
auto It = Cache.find(Filename);
260+
return It == Cache.end() ? nullptr : It->getValue().second;
261261
}
262262

263263
/// Associates the given real path with the filename and returns the given
@@ -266,10 +266,9 @@ class DependencyScanningFilesystemLocalCache {
266266
insertRealPathForFilename(StringRef Filename,
267267
const CachedRealPath &RealPath) {
268268
assert(llvm::sys::path::is_absolute_gnu(Filename));
269-
const auto *InsertedRealPath =
270-
RealPathCache.insert({Filename, &RealPath}).first->second;
271-
assert(InsertedRealPath == &RealPath && "entry already present");
272-
return *InsertedRealPath;
269+
assert(Cache[Filename].second == nullptr && "entry already present");
270+
Cache[Filename].second = &RealPath;
271+
return RealPath;
273272
}
274273
};
275274

clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ DependencyScanningFilesystemSharedCache::CacheShard::findEntryByFilename(
113113
StringRef Filename) const {
114114
assert(llvm::sys::path::is_absolute_gnu(Filename));
115115
std::lock_guard<std::mutex> LockGuard(CacheLock);
116-
auto It = EntriesByFilename.find(Filename);
117-
return It == EntriesByFilename.end() ? nullptr : It->getValue();
116+
auto It = CacheByFilename.find(Filename);
117+
return It == CacheByFilename.end() ? nullptr : It->getValue().first;
118118
}
119119

120120
const CachedFileSystemEntry *
@@ -130,11 +130,11 @@ DependencyScanningFilesystemSharedCache::CacheShard::
130130
getOrEmplaceEntryForFilename(StringRef Filename,
131131
llvm::ErrorOr<llvm::vfs::Status> Stat) {
132132
std::lock_guard<std::mutex> LockGuard(CacheLock);
133-
auto Insertion = EntriesByFilename.insert({Filename, nullptr});
134-
if (Insertion.second)
135-
Insertion.first->second =
133+
const CachedFileSystemEntry *StoredEntry = CacheByFilename[Filename].first;
134+
if (!StoredEntry)
135+
StoredEntry =
136136
new (EntryStorage.Allocate()) CachedFileSystemEntry(std::move(Stat));
137-
return *Insertion.first->second;
137+
return *StoredEntry;
138138
}
139139

140140
const CachedFileSystemEntry &
@@ -159,36 +159,37 @@ DependencyScanningFilesystemSharedCache::CacheShard::
159159
getOrInsertEntryForFilename(StringRef Filename,
160160
const CachedFileSystemEntry &Entry) {
161161
std::lock_guard<std::mutex> LockGuard(CacheLock);
162-
return *EntriesByFilename.insert({Filename, &Entry}).first->getValue();
162+
CacheByFilename[Filename].first = &Entry;
163+
return Entry;
163164
}
164165

165166
const CachedRealPath *
166167
DependencyScanningFilesystemSharedCache::CacheShard::findRealPathByFilename(
167168
StringRef Filename) const {
168169
assert(llvm::sys::path::is_absolute_gnu(Filename));
169170
std::lock_guard<std::mutex> LockGuard(CacheLock);
170-
auto It = RealPathsByFilename.find(Filename);
171-
return It == RealPathsByFilename.end() ? nullptr : It->getValue();
171+
auto It = CacheByFilename.find(Filename);
172+
return It == CacheByFilename.end() ? nullptr : It->getValue().second;
172173
}
173174

174175
const CachedRealPath &DependencyScanningFilesystemSharedCache::CacheShard::
175176
getOrEmplaceRealPathForFilename(StringRef Filename,
176177
llvm::ErrorOr<llvm::StringRef> RealPath) {
177178
std::lock_guard<std::mutex> LockGuard(CacheLock);
178179

179-
auto Insertion = RealPathsByFilename.insert({Filename, nullptr});
180-
if (Insertion.second) {
180+
const CachedRealPath *StoredRealPath = CacheByFilename[Filename].second;
181+
if (!StoredRealPath) {
181182
auto OwnedRealPath = [&]() -> CachedRealPath {
182183
if (!RealPath)
183184
return RealPath.getError();
184185
return RealPath->str();
185186
}();
186187

187-
Insertion.first->second = new (RealPathStorage.Allocate())
188+
StoredRealPath = new (RealPathStorage.Allocate())
188189
CachedRealPath(std::move(OwnedRealPath));
189190
}
190191

191-
return *Insertion.first->second;
192+
return *StoredRealPath;
192193
}
193194

194195
static bool shouldCacheStatFailures(StringRef Filename) {

0 commit comments

Comments
 (0)