[lld] [lld][MachO]Multi-threaded i/o. Twice as fast linking a large project. (PR #147134)
Ellis Hoag via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 25 16:51:01 PDT 2025
================
@@ -282,11 +286,119 @@ 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:
+ DeferredFile(StringRef path, bool isLazy, MemoryBufferRef buffer)
+ : path(path), isLazy(isLazy), buffer(buffer) {}
+ StringRef path;
+ bool isLazy;
+ MemoryBufferRef buffer;
+};
+using DeferredFiles = std::vector<DeferredFile>;
+
+class SerialBackgroundQueue {
+ std::deque<std::function<void()>> queue;
+ std::thread *running;
+ std::mutex mutex;
+
+public:
+ void queueWork(std::function<void()> work, bool reap) {
+ mutex.lock();
+ if (running && (queue.empty() || reap)) {
+ mutex.unlock();
+ running->join();
+ mutex.lock();
+ delete running;
+ running = nullptr;
+ }
+
+ if (!reap) {
+ queue.emplace_back(std::move(work));
+ if (!running)
+ running = new std::thread([&]() {
+ bool shouldPop = false;
+ while (true) {
+ mutex.lock();
+ if (shouldPop)
+ queue.pop_front();
+ if (queue.empty()) {
+ mutex.unlock();
+ break;
+ }
+ auto work = std::move(queue.front());
+ shouldPop = true;
+ mutex.unlock();
+ work();
+ }
+ });
+ }
+ mutex.unlock();
+ }
+};
+
+#ifndef NDEBUG
+#include <iomanip>
+#include <iostream>
+#endif
+
+// 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(DeferredFiles &deferred) {
+ using namespace std::chrono;
+ static const size_t pageSize = Process::getPageSizeEstimate();
+ static const size_t largeArchive = 10 * 1024 * 1024;
+ static std::atomic_uint64_t totalBytes = 0;
+ std::atomic_int index = 0, included = 0;
----------------
ellishg wrote:
NIT: Can we make `included` more specific? Something like `numDeferedFilesTouched`? Also, let's guard it (and `totalBytes`) behind `NDEBUG` because it won't be used in the release binary.
https://github.com/llvm/llvm-project/pull/147134
More information about the llvm-commits
mailing list