[lld] [lld][MachO]Multi-threaded i/o. Twice as fast linking a large project. (PR #147134)

John Holdsworth via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 12 05:07:50 PDT 2025


================
@@ -282,11 +284,83 @@ static void saveThinArchiveToRepro(ArchiveFile const *file) {
           ": Archive::children failed: " + toString(std::move(e)));
 }
 
-static InputFile *addFile(StringRef path, LoadType loadType,
-                          bool isLazy = false, bool isExplicit = true,
-                          bool isBundleLoader = false,
-                          bool isForceHidden = false) {
-  std::optional<MemoryBufferRef> buffer = readFile(path);
+class DeferredFile {
+public:
+  StringRef path;
+  bool isLazy;
+  std::optional<MemoryBufferRef> buffer;
+  const char *start;
+  size_t size;
+};
+using DeferredFiles = std::vector<DeferredFile>;
+
+class PageInState {
+  DeferredFiles deferred;
+  size_t counter = 0, total = 0, pageSize;
+  std::mutex mutex, *busy;
+
+public:
+  PageInState(DeferredFiles &deferred, std::mutex *busy) {
+    this->deferred = deferred;
+    this->busy = busy;
+    pageSize = llvm::sys::Process::getPageSizeEstimate();
+  }
+
+  // Most input files have been mapped but not yet paged in.
+  // This code forces the page-ins on multiple threads so
+  // the process is not stalled waiting on disk buffer i/o.
+  void multiThreadedPageInBackground() {
+    static size_t totalBytes;
+
+    parallelFor(0, config->readThreads, [&](size_t I) {
+      while (true) {
+        mutex.lock();
+        if (counter >= deferred.size()) {
+          mutex.unlock();
+          return;
+        }
+        DeferredFile &file = deferred[counter];
+        totalBytes += file.size;
+        counter += 1;
+        mutex.unlock();
+
+        int t = 0; // Reference each page to load it into memory.
+        for (const char *page = file.start, *end = page + file.size; page < end;
+             page += pageSize)
+          t += *page;
+        total += t; // Avoids the loop being optimised out.
+      }
+    });
----------------
johnno1962 wrote:

I tried a few things including your suggestion but couldn't come up with an alternative that didn't seem slower.

https://github.com/llvm/llvm-project/pull/147134


More information about the llvm-commits mailing list