[clang] 34b4f00 - [clang-format] De-pessimize appending newlines. NFC.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 1 05:11:04 PST 2022


Author: Marek Kurdej
Date: 2022-02-01T14:10:48+01:00
New Revision: 34b4f00686ffa030fb0fed3bdd24b5e8588dae89

URL: https://github.com/llvm/llvm-project/commit/34b4f00686ffa030fb0fed3bdd24b5e8588dae89
DIFF: https://github.com/llvm/llvm-project/commit/34b4f00686ffa030fb0fed3bdd24b5e8588dae89.diff

LOG: [clang-format] De-pessimize appending newlines. NFC.

* Avoid repeatedly calling std::string::append(char) in a loop.
* Reserve before calling std::string::append(const char *) in a loop.

Added: 
    

Modified: 
    clang/lib/Format/WhitespaceManager.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 7709fe814864..4c130abd83c3 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1347,8 +1347,13 @@ void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
 
 void WhitespaceManager::appendNewlineText(std::string &Text,
                                           unsigned Newlines) {
-  for (unsigned i = 0; i < Newlines; ++i)
-    Text.append(UseCRLF ? "\r\n" : "\n");
+  if (UseCRLF) {
+    Text.reserve(Text.size() + 2 * Newlines);
+    for (unsigned i = 0; i < Newlines; ++i)
+      Text.append("\r\n");
+  } else {
+    Text.append(Newlines, '\n');
+  }
 }
 
 void WhitespaceManager::appendEscapedNewlineText(


        


More information about the cfe-commits mailing list