[llvm] 2b60b6d - [llvm][mustache] Avoid extra allocations in parseSection (#159199)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 4 16:46:12 PST 2025


Author: Paul Kirth
Date: 2025-11-05T00:46:09Z
New Revision: 2b60b6d9639c240ebafc8b517425453e50e14a7b

URL: https://github.com/llvm/llvm-project/commit/2b60b6d9639c240ebafc8b517425453e50e14a7b
DIFF: https://github.com/llvm/llvm-project/commit/2b60b6d9639c240ebafc8b517425453e50e14a7b.diff

LOG: [llvm][mustache] Avoid extra allocations in parseSection (#159199)

We don't need to have extra allocations when concatenating raw bodies.

Added: 
    

Modified: 
    llvm/lib/Support/Mustache.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 9eb1ec2b8425c..6c140be59fc4b 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -599,9 +599,16 @@ void Parser::parseSection(ASTNode *Parent, ASTNode::Type Ty,
   size_t Start = CurrentPtr;
   parseMustache(CurrentNode);
   const size_t End = CurrentPtr - 1;
+
+  size_t RawBodySize = 0;
+  for (size_t I = Start; I < End; ++I)
+    RawBodySize += Tokens[I].RawBody.size();
+
   SmallString<128> RawBody;
-  for (std::size_t I = Start; I < End; I++)
+  RawBody.reserve(RawBodySize);
+  for (std::size_t I = Start; I < End; ++I)
     RawBody += Tokens[I].RawBody;
+
   CurrentNode->setRawBody(Ctx.Saver.save(StringRef(RawBody)));
   Parent->addChild(CurrentNode);
 }


        


More information about the llvm-commits mailing list