From d8ecf8629561d8049bccb3a1c9a0180d68ed4c26 Mon Sep 17 00:00:00 2001 From: Xinliang David Li Date: Mon, 4 Jan 2016 22:01:02 +0000 Subject: [PATCH] [PGO] Refactor string writer code For readability and code sharing. (Adapted from Suggestions by Vedant). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256784 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/ProfileData/InstrProf.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/ProfileData/InstrProf.cpp b/lib/ProfileData/InstrProf.cpp index 58f70af39f6..bcb248e8305 100644 --- a/lib/ProfileData/InstrProf.cpp +++ b/lib/ProfileData/InstrProf.cpp @@ -173,26 +173,32 @@ int collectPGOFuncNameStrings(const std::vector &NameStrs, unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P); P += EncLen; - if (!doCompression) { - EncLen = encodeULEB128(0, P); + + auto WriteStringToResult = [&](size_t CompressedLen, + const std::string &InputStr) { + EncLen = encodeULEB128(CompressedLen, P); P += EncLen; - Result.append(reinterpret_cast(&Header[0]), P - &Header[0]); - Result += UncompressedNameStrings; + char *HeaderStr = reinterpret_cast(&Header[0]); + unsigned HeaderLen = P - &Header[0]; + Result.append(HeaderStr, HeaderLen); + Result += InputStr; return 0; - } + }; + + if (!doCompression) + return WriteStringToResult(0, UncompressedNameStrings); + SmallVector CompressedNameStrings; zlib::Status Success = zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings, zlib::BestSizeCompression); - assert(Success == zlib::StatusOK); + if (Success != zlib::StatusOK) return 1; - EncLen = encodeULEB128(CompressedNameStrings.size(), P); - P += EncLen; - Result.append(reinterpret_cast(&Header[0]), P - &Header[0]); - Result += - std::string(CompressedNameStrings.data(), CompressedNameStrings.size()); - return 0; + + return WriteStringToResult( + CompressedNameStrings.size(), + std::string(CompressedNameStrings.data(), CompressedNameStrings.size())); } StringRef getPGOFuncNameInitializer(GlobalVariable *NameVar) { -- 2.34.1