[PATCH] D147960: [lld-macho] Cache discovered dylib paths

Keith Smiley via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 13 09:03:55 PDT 2023


keith updated this revision to Diff 513270.
keith added a comment.

ignore empty entries since this is called multiple times with candidates, and we only want to cache the first hit really. It will still be empty and search in the case nothing can be found


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147960/new/

https://reviews.llvm.org/D147960

Files:
  lld/MachO/DriverUtils.cpp


Index: lld/MachO/DriverUtils.cpp
===================================================================
--- lld/MachO/DriverUtils.cpp
+++ lld/MachO/DriverUtils.cpp
@@ -186,7 +186,16 @@
     depTracker->logFileNotFound(path);
 }
 
+// It's not uncommon to have multiple attempts to load a single dylib,
+// especially if it's a commonly re-exported core library.
+static DenseMap<CachedHashStringRef, StringRef> resolvedDylibPaths;
 std::optional<StringRef> macho::resolveDylibPath(StringRef dylibPath) {
+  CachedHashStringRef key(dylibPath);
+  auto entry = resolvedDylibPaths.find(key);
+  if (entry != resolvedDylibPaths.end() && !entry->second.empty())
+    return entry->second;
+
+  StringRef &path = resolvedDylibPaths[key];
   // TODO: if a tbd and dylib are both present, we should check to make sure
   // they are consistent.
   SmallString<261> tbdPath = dylibPath;
@@ -194,17 +203,15 @@
   bool tbdExists = fs::exists(tbdPath);
   searchedDylib(tbdPath, tbdExists);
   if (tbdExists)
-    return saver().save(tbdPath.str());
+    return path = saver().save(tbdPath.str());
 
   bool dylibExists = fs::exists(dylibPath);
   searchedDylib(dylibPath, dylibExists);
   if (dylibExists)
-    return saver().save(dylibPath);
+    return path = saver().save(dylibPath);
   return {};
 }
 
-// It's not uncommon to have multiple attempts to load a single dylib,
-// especially if it's a commonly re-exported core library.
 static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
 
 DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
@@ -253,7 +260,10 @@
   return newFile;
 }
 
-void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
+void macho::resetLoadedDylibs() {
+  resolvedDylibPaths.clear();
+  loadedDylibs.clear();
+}
 
 std::optional<StringRef>
 macho::findPathCombination(const Twine &name,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147960.513270.patch
Type: text/x-patch
Size: 1837 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230413/3a5aade8/attachment.bin>


More information about the llvm-commits mailing list