[PATCH] D87272: [lld] Buffer lld::errs() writes

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 23:30:17 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, jhenderson, mstorsjo, psmith.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
MaskRay requested review of this revision.

llvm::errs() is unbuffered. On a POSIX platform, composing a diagnostic
string may invoke the ::write syscall multiple times, which can be slow.
Buffer consecutive writes of one diagnostic to a temporary SmallString to
reduce the number of ::write syscalls to one (also easier to read under
strace/truss).

For an invocation of ld.lld with 62000+ lines of
`ld.lld: warning: symbol ordering file: no such symbol: ` warnings (D87122 <https://reviews.llvm.org/D87122>),
the buffering decreases the write time from 1s to 0.4s (for /dev/tty) and
from 0.4s to 0.1s (for a tmpfs file).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87272

Files:
  lld/Common/ErrorHandler.cpp
  llvm/include/llvm/Support/raw_ostream.h


Index: llvm/include/llvm/Support/raw_ostream.h
===================================================================
--- llvm/include/llvm/Support/raw_ostream.h
+++ llvm/include/llvm/Support/raw_ostream.h
@@ -301,6 +301,8 @@
   // changeColor() has no effect until enable_colors(true) is called.
   virtual void enable_colors(bool enable) { ColorEnabled = enable; }
 
+  bool colors_enabled() { return ColorEnabled; }
+
   /// Tie this stream to the specified stream. Replaces any existing tied-to
   /// stream. Specifying a nullptr unties the stream.
   void tie(raw_ostream *TieTo) { TiedStream = TieTo; }
Index: lld/Common/ErrorHandler.cpp
===================================================================
--- lld/Common/ErrorHandler.cpp
+++ lld/Common/ErrorHandler.cpp
@@ -155,8 +155,11 @@
 void ErrorHandler::log(const Twine &msg) {
   if (!verbose)
     return;
+  SmallString<256> buf;
+  raw_svector_ostream os(buf);
+  os << logName << ": " << msg << '\n';
   std::lock_guard<std::mutex> lock(mu);
-  lld::errs() << logName << ": " << msg << "\n";
+  lld::errs() << buf;
 }
 
 void ErrorHandler::message(const Twine &msg) {
@@ -171,9 +174,22 @@
     return;
   }
 
-  std::lock_guard<std::mutex> lock(mu);
-  lld::errs() << sep << getLocation(msg) << ": " << Colors::MAGENTA
-              << "warning: " << Colors::RESET << msg << "\n";
+  raw_ostream &errs = lld::errs();
+  SmallString<256> buf;
+  raw_svector_ostream os(buf);
+  os << sep << getLocation(msg) << ": ";
+  if (errs.colors_enabled()) {
+    os.enable_colors(true);
+    os << Colors::MAGENTA << "warning: " << Colors::RESET;
+  } else {
+    os << "warning: ";
+  }
+  os << msg << '\n';
+
+  {
+    std::lock_guard<std::mutex> lock(mu);
+    errs << buf;
+  }
   sep = getSeparator(msg);
 }
 
@@ -196,16 +212,26 @@
 
   bool exit = false;
   {
-    std::lock_guard<std::mutex> lock(mu);
+    if (errorLimit == 0 || errorCount <= errorLimit) {
+      raw_ostream &errs = lld::errs();
+      SmallString<256> buf;
+      raw_svector_ostream os(buf);
+      os << sep << getLocation(msg) << ": ";
+      if (errs.colors_enabled()) {
+        os.enable_colors(true);
+        os << Colors::RED << "error: " << Colors::RESET;
+      } else {
+        os << "error: ";
+      }
+      if (errorLimit == 0 || errorCount < errorLimit) {
+        os << msg << '\n';
+      } else {
+        os << errorLimitExceededMsg << '\n';
+        exit = exitEarly;
+      }
 
-    if (errorLimit == 0 || errorCount < errorLimit) {
-      lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
-                  << "error: " << Colors::RESET << msg << "\n";
-    } else if (errorCount == errorLimit) {
-      lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
-                  << "error: " << Colors::RESET << errorLimitExceededMsg
-                  << "\n";
-      exit = exitEarly;
+      std::lock_guard<std::mutex> lock(mu);
+      lld::errs() << buf;
     }
 
     sep = getSeparator(msg);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87272.290411.patch
Type: text/x-patch
Size: 2977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200908/0789445e/attachment.bin>


More information about the llvm-commits mailing list