Skip to content

Commit bdf8995

Browse files
committed
Revert "Multi-threaded file opening/mapping."
This reverts commit be7af98.
1 parent be7af98 commit bdf8995

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
lines changed

lld/MachO/Driver.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ static void saveThinArchiveToRepro(ArchiveFile const *file) {
292292
struct DeferredFile {
293293
StringRef path;
294294
bool isLazy;
295-
std::optional<MemoryBufferRef> buffer;
295+
MemoryBufferRef buffer;
296296
};
297297
using DeferredFiles = std::vector<DeferredFile>;
298298

@@ -346,10 +346,8 @@ void multiThreadedPageInBackground(DeferredFiles &deferred) {
346346
std::atomic_int numDeferedFilesAdvised = 0;
347347
auto t0 = high_resolution_clock::now();
348348

349-
auto preloadDeferredFile = [&](DeferredFile &deferredFile) {
350-
if (!deferredFile.buffer.has_value())
351-
deferredFile.buffer = *readFile(deferredFile.path);
352-
const StringRef &buff = (*deferredFile.buffer).getBuffer();
349+
auto preloadDeferredFile = [&](const DeferredFile &deferredFile) {
350+
const StringRef &buff = deferredFile.buffer.getBuffer();
353351
if (buff.size() > largeArchive)
354352
return;
355353

@@ -396,9 +394,12 @@ void multiThreadedPageInBackground(DeferredFiles &deferred) {
396394
<< duration_cast<milliseconds>(dt).count() / 1000. << "\n";
397395
}
398396

399-
static void multiThreadedPageIn(DeferredFiles &deferred) {
397+
static void multiThreadedPageIn(const DeferredFiles &deferred) {
400398
static SerialBackgroundQueue pageInQueue;
401-
pageInQueue.queueWork([&]() { multiThreadedPageInBackground(deferred); });
399+
pageInQueue.queueWork([=]() {
400+
DeferredFiles files = deferred;
401+
multiThreadedPageInBackground(files);
402+
});
402403
}
403404

404405
static InputFile *processFile(std::optional<MemoryBufferRef> buffer,
@@ -573,12 +574,13 @@ static InputFile *addFile(StringRef path, LoadType loadType,
573574
}
574575

575576
static void deferFile(StringRef path, bool isLazy, DeferredFiles &deferred) {
576-
if (config->readWorkers)
577-
return deferred.push_back({path, isLazy, std::nullopt});
578577
std::optional<MemoryBufferRef> buffer = readFile(path);
579578
if (!buffer)
580579
return;
581-
processFile(buffer, nullptr, path, LoadType::CommandLine, isLazy);
580+
if (config->readWorkers)
581+
deferred.push_back({path, isLazy, *buffer});
582+
else
583+
processFile(buffer, nullptr, path, LoadType::CommandLine, isLazy);
582584
}
583585

584586
static std::vector<StringRef> missingAutolinkWarnings;
@@ -1363,8 +1365,7 @@ static void createFiles(const InputArgList &args) {
13631365
bool isLazy = false;
13641366
// If we've processed an opening --start-lib, without a matching --end-lib
13651367
bool inLib = false;
1366-
static DeferredFiles deferredFiles;
1367-
deferredFiles.clear();
1368+
DeferredFiles deferredFiles;
13681369

13691370
for (const Arg *arg : args) {
13701371
const Option &opt = arg->getOption();
@@ -1443,13 +1444,9 @@ static void createFiles(const InputArgList &args) {
14431444
if (config->readWorkers) {
14441445
multiThreadedPageIn(deferredFiles);
14451446

1446-
static DeferredFiles archiveContents;
1447+
DeferredFiles archiveContents;
14471448
std::vector<ArchiveFile *> archives;
1448-
archiveContents.clear();
1449-
14501449
for (auto &file : deferredFiles) {
1451-
if (!file.buffer.has_value())
1452-
file.buffer = readFile(file.path);
14531450
auto inputFile = processFile(file.buffer, &archiveContents, file.path,
14541451
LoadType::CommandLine, file.isLazy);
14551452
if (ArchiveFile *archive = dyn_cast<ArchiveFile>(inputFile))

lld/MachO/InputFiles.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,10 @@ static bool compatWithTargetArch(const InputFile *file, const Header *hdr) {
212212
DenseMap<CachedHashStringRef, MemoryBufferRef> macho::cachedReads;
213213
// Open a given file path and return it as a memory-mapped file.
214214
std::optional<MemoryBufferRef> macho::readFile(StringRef path) {
215-
static std::mutex mutex;
216-
mutex.lock();
217215
CachedHashStringRef key(path);
218216
auto entry = cachedReads.find(key);
219-
if (entry != cachedReads.end()) {
220-
mutex.unlock();
217+
if (entry != cachedReads.end())
221218
return entry->second;
222-
}
223-
mutex.unlock();
224219

225220
ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr =
226221
MemoryBuffer::getFile(path, false, /*RequiresNullTerminator=*/false);
@@ -229,23 +224,18 @@ std::optional<MemoryBufferRef> macho::readFile(StringRef path) {
229224
return std::nullopt;
230225
}
231226

232-
mutex.lock();
233227
std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
234228
MemoryBufferRef mbref = mb->getMemBufferRef();
235229
make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership
236-
mutex.unlock();
237230

238231
// If this is a regular non-fat file, return it.
239232
const char *buf = mbref.getBufferStart();
240233
const auto *hdr = reinterpret_cast<const fat_header *>(buf);
241234
if (mbref.getBufferSize() < sizeof(uint32_t) ||
242235
read32be(&hdr->magic) != FAT_MAGIC) {
243-
mutex.lock();
244236
if (tar)
245237
tar->append(relativeToRoot(path), mbref.getBuffer());
246-
cachedReads[key] = mbref;
247-
mutex.unlock();
248-
return mbref;
238+
return cachedReads[key] = mbref;
249239
}
250240

251241
llvm::BumpPtrAllocator &bAlloc = lld::bAlloc();
@@ -282,13 +272,10 @@ std::optional<MemoryBufferRef> macho::readFile(StringRef path) {
282272
uint32_t size = read32be(&arch[i].size);
283273
if (offset + size > mbref.getBufferSize())
284274
error(path + ": slice extends beyond end of file");
285-
mutex.lock();
286275
if (tar)
287276
tar->append(relativeToRoot(path), mbref.getBuffer());
288-
mbref = MemoryBufferRef(StringRef(buf + offset, size), path.copy(bAlloc));
289-
cachedReads[key] = mbref;
290-
mutex.unlock();
291-
return mbref;
277+
return cachedReads[key] = MemoryBufferRef(StringRef(buf + offset, size),
278+
path.copy(bAlloc));
292279
}
293280

294281
auto targetArchName = getArchName(target->cpuType, target->cpuSubtype);

0 commit comments

Comments
 (0)