[PGO]: reserve space for string to avoid excessive memory realloc/copy (non linear)
authorXinliang David Li <davidxl@google.com>
Mon, 4 Jan 2016 20:26:05 +0000 (20:26 +0000)
committerXinliang David Li <davidxl@google.com>
Mon, 4 Jan 2016 20:26:05 +0000 (20:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256776 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ProfileData/InstrProf.cpp

index 3c75269..dd5f04b 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;