[PATCH] D115416: [lld-macho] Prevent writing map files on the critical path

Vincent Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 8 18:33:43 PST 2021


thevinster created this revision.
Herald added a project: lld-macho.
Herald added a reviewer: lld-macho.
thevinster edited the summary of this revision.
thevinster published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

For large applications that write to map files, writing map files can take quite
a bit of time. Sorting the biggest contributors to link times, writing map files
ranks in at 2nd place, with load input files being the biggest contributor of 
link times. Avoiding writing map files on the critical path (and having its own
thread) saves saves ~2-3 seconds when linking chromium framework.

             base            diff            difference (95% CI)
  sys_time   1.617 ± 0.034   1.657 ± 0.026   [  +1.5% ..   +3.5%]
  user_time  28.536 ± 0.245  28.609 ± 0.180  [  -0.1% ..   +0.7%]
  wall_time  23.833 ± 0.271  21.684 ± 0.194  [  -9.5% ..   -8.5%]
  samples    31              24


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115416

Files:
  lld/MachO/Writer.cpp


Index: lld/MachO/Writer.cpp
===================================================================
--- lld/MachO/Writer.cpp
+++ lld/MachO/Writer.cpp
@@ -33,6 +33,7 @@
 #include "llvm/Support/xxhash.h"
 
 #include <algorithm>
+#include <future>
 
 using namespace llvm;
 using namespace llvm::MachO;
@@ -1147,9 +1148,13 @@
   sortSegmentsAndSections();
   createLoadCommands<LP>();
   finalizeAddresses();
+  std::future<void> writeMapAsync(std::async(writeMapFile));
   finalizeLinkEditSegment();
-  writeMapFile();
   writeOutputFile();
+
+  // Ensure writeMapFile completes before the main thread returns
+  // to prevent undefined behavior.
+  writeMapAsync.get();
 }
 
 template <class LP> void macho::writeResult() { Writer().run<LP>(); }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115416.393007.patch
Type: text/x-patch
Size: 741 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211209/9c0f2d38/attachment-0001.bin>


More information about the llvm-commits mailing list