diff --git a/llvm/tools/dsymutil/BinaryHolder.h b/llvm/tools/dsymutil/BinaryHolder.h --- a/llvm/tools/dsymutil/BinaryHolder.h +++ b/llvm/tools/dsymutil/BinaryHolder.h @@ -118,7 +118,7 @@ private: std::vector> Archives; - DenseMap MemberCache; + DenseMap> MemberCache; std::mutex MemberCacheMutex; }; @@ -130,11 +130,11 @@ private: /// Cache of static archives. Objects that are part of a static archive are /// stored under this object, rather than in the map below. - StringMap ArchiveCache; + StringMap> ArchiveCache; std::mutex ArchiveCacheMutex; /// Object entries for objects that are not in a static archive. - StringMap ObjectCache; + StringMap> ObjectCache; std::mutex ObjectCacheMutex; /// Virtual File System instance. diff --git a/llvm/tools/dsymutil/BinaryHolder.cpp b/llvm/tools/dsymutil/BinaryHolder.cpp --- a/llvm/tools/dsymutil/BinaryHolder.cpp +++ b/llvm/tools/dsymutil/BinaryHolder.cpp @@ -168,20 +168,17 @@ StringRef ArchiveFilename; StringRef ObjectFilename; std::tie(ArchiveFilename, ObjectFilename) = getArchiveAndObjectName(Filename); - - // Try the cache first. KeyTy Key = {ObjectFilename, Timestamp}; - { - std::lock_guard Lock(MemberCacheMutex); - if (MemberCache.count(Key)) - return MemberCache[Key]; - } + // Try the cache first. + std::lock_guard Lock(MemberCacheMutex); + if (MemberCache.count(Key)) + return *MemberCache[Key].get(); // Create a new ObjectEntry, but don't add it to the cache yet. Loading of // the archive members might fail and we don't want to lock the whole archive // during this operation. - ObjectEntry OE; + auto OE = std::make_unique(); for (const auto &Archive : Archives) { Error Err = Error::success(); @@ -216,7 +213,7 @@ if (!ErrOrObjectFile) return ErrOrObjectFile.takeError(); - OE.Objects.push_back(std::move(*ErrOrObjectFile)); + OE->Objects.push_back(std::move(*ErrOrObjectFile)); } } } @@ -224,12 +221,11 @@ return std::move(Err); } - if (OE.Objects.empty()) + if (OE->Objects.empty()) return errorCodeToError(errc::no_such_file_or_directory); - std::lock_guard Lock(MemberCacheMutex); - MemberCache.try_emplace(Key, std::move(OE)); - return MemberCache[Key]; + MemberCache[Key] = std::move(OE); + return *MemberCache[Key]; } Expected @@ -243,18 +239,18 @@ StringRef ArchiveFilename = getArchiveAndObjectName(Filename).first; std::lock_guard Lock(ArchiveCacheMutex); if (ArchiveCache.count(ArchiveFilename)) { - return ArchiveCache[ArchiveFilename].getObjectEntry(Filename, Timestamp, - Verbose); + return ArchiveCache[ArchiveFilename]->getObjectEntry(Filename, Timestamp, + Verbose); } else { - ArchiveEntry &AE = ArchiveCache[ArchiveFilename]; - auto Err = AE.load(VFS, Filename, Timestamp, Verbose); + auto AE = std::make_unique(); + auto Err = AE->load(VFS, Filename, Timestamp, Verbose); if (Err) { - ArchiveCache.erase(ArchiveFilename); // Don't return the error here: maybe the file wasn't an archive. llvm::consumeError(std::move(Err)); } else { - return ArchiveCache[ArchiveFilename].getObjectEntry(Filename, Timestamp, - Verbose); + ArchiveCache[ArchiveFilename] = std::move(AE); + return ArchiveCache[ArchiveFilename]->getObjectEntry( + Filename, Timestamp, Verbose); } } } @@ -263,15 +259,14 @@ // it from the file system and cache it now. std::lock_guard Lock(ObjectCacheMutex); if (!ObjectCache.count(Filename)) { - ObjectEntry &OE = ObjectCache[Filename]; - auto Err = OE.load(VFS, Filename, Timestamp, Verbose); - if (Err) { - ObjectCache.erase(Filename); + auto OE = std::make_unique(); + auto Err = OE->load(VFS, Filename, Timestamp, Verbose); + if (Err) return std::move(Err); - } + ObjectCache[Filename] = std::move(OE); } - return ObjectCache[Filename]; + return *ObjectCache[Filename]; } void BinaryHolder::clear() {