Skip to content

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Sep 16, 2025

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

Copy link
Contributor Author

ilovepi commented Sep 16, 2025

@llvmbot
Copy link
Member

llvmbot commented Sep 16, 2025

@llvm/pr-subscribers-llvm-support

Author: Paul Kirth (ilovepi)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/159199.diff

1 Files Affected:

  • (modified) llvm/lib/Support/Mustache.cpp (+8-1)
diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 8139e9eb4717f..829643d1f35d2 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -590,9 +590,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);
 }

@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from 410fdc8 to f5b3210 Compare September 22, 2025 17:07
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch 2 times, most recently from ac3d77f to 5c65ae1 Compare September 22, 2025 17:56
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from f5b3210 to 2de3866 Compare September 22, 2025 17:56
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from 2de3866 to c6e7926 Compare September 25, 2025 22:12
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch 2 times, most recently from 05bfb24 to f37d180 Compare September 26, 2025 01:55
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from c6e7926 to 28deddc Compare September 26, 2025 01:55
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch from f37d180 to f93d738 Compare September 29, 2025 17:39
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch 2 times, most recently from ca0de5e to 919e389 Compare September 29, 2025 22:28
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch from f93d738 to 2a7ebd4 Compare September 29, 2025 22:28
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from 919e389 to 943c634 Compare September 30, 2025 01:54
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch 2 times, most recently from 057142b to 92378de Compare September 30, 2025 03:47
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch 2 times, most recently from 0cd6777 to d5cdf06 Compare September 30, 2025 03:48
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch 2 times, most recently from baee147 to f048f53 Compare October 1, 2025 00:04
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from d5cdf06 to 66ea253 Compare October 1, 2025 00:04
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch from f048f53 to a81f574 Compare October 1, 2025 00:15
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from 66ea253 to 520cdc1 Compare October 1, 2025 00:15
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch from a81f574 to 228b984 Compare October 6, 2025 17:21
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch 2 times, most recently from 37926e7 to 7adaaa3 Compare October 6, 2025 20:26
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch from 228b984 to 3456d1b Compare October 6, 2025 20:26
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from 7adaaa3 to 9afa122 Compare October 9, 2025 17:51
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch from 3456d1b to 7909f20 Compare October 9, 2025 17:51
We don't need to have extra allocations when concatenating raw bodies.
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-parse-section-opt branch from 7909f20 to f276893 Compare October 10, 2025 16:21
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-single-pass branch from 9afa122 to 7d30130 Compare October 10, 2025 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants