From 34b4f00686ffa030fb0fed3bdd24b5e8588dae89 Mon Sep 17 00:00:00 2001 From: Marek Kurdej Date: Tue, 1 Feb 2022 14:10:19 +0100 Subject: [PATCH] [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. --- clang/lib/Format/WhitespaceManager.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 7709fe8..4c130ab 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( -- 2.7.4