[PGO]: reserve space for string to avoid excessive memory realloc/copy (non linear)
[oota-llvm.git] / lib / ProfileData / InstrProf.cpp
index 3c752699a2750e7b7f3508d7c387abe0f21e71e3..dd5f04bee324b72d57bdd2455055d48f7cb5b47f 100644 (file)
@@ -168,6 +168,12 @@ int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
                               bool doCompression, std::string &Result) {
   uint8_t Header[16], *P = Header;
   std::string UncompressedNameStrings;
+  size_t UncompressedStringLen = 0;
+
+  for (auto NameStr : NameStrs)
+    UncompressedStringLen += (NameStr.length() + 1);
+
+  UncompressedNameStrings.reserve(UncompressedStringLen + 1);
 
   for (auto NameStr : NameStrs) {
     UncompressedNameStrings += NameStr;