[llvm] [ProfData] Improve efficiency of reader (PR #169730)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 26 13:07:48 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-pgo
Author: Max (mxms0)
<details>
<summary>Changes</summary>
Pre-reserve space in the map before inserting. In release builds, 9.4% of all CPU time is spent in llvm::sampleprof::ProfileSymbolList::add. Of that 9.4%, roughly half is in llvm::DenseMapBase::grow.
---
Full diff: https://github.com/llvm/llvm-project/pull/169730.diff
2 Files Affected:
- (modified) llvm/include/llvm/ProfileData/SampleProf.h (+1)
- (modified) llvm/lib/ProfileData/SampleProf.cpp (+20-5)
``````````diff
diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h
index 3dd34aba2d716..6bce5c718e598 100644
--- a/llvm/include/llvm/ProfileData/SampleProf.h
+++ b/llvm/include/llvm/ProfileData/SampleProf.h
@@ -1658,6 +1658,7 @@ class ProfileSymbolList {
}
unsigned size() { return Syms.size(); }
+ void reserve(size_t Size) { Syms.reserve(Size); }
void setToCompress(bool TC) { ToCompress = TC; }
bool toCompress() { return ToCompress; }
diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp
index ac7513ef2cb49..5f0af79ef33df 100644
--- a/llvm/lib/ProfileData/SampleProf.cpp
+++ b/llvm/lib/ProfileData/SampleProf.cpp
@@ -399,15 +399,30 @@ LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
std::error_code ProfileSymbolList::read(const uint8_t *Data,
uint64_t ListSize) {
const char *ListStart = reinterpret_cast<const char *>(Data);
- uint64_t Size = 0;
+ uint64_t Offset = 0;
uint64_t StrNum = 0;
- while (Size < ListSize && StrNum < ProfileSymbolListCutOff) {
- StringRef Str(ListStart + Size);
+ uint64_t ExpectedCount = 0;
+
+ Scan forward to see how many elements we expect.
+ while (Offset < ListSize) {
+ if (ListStart[Offset] == '\0') ExpectedCount++;
+ Offset++;
+ }
+
+ reserve(ExpectedCount);
+
+ Offset = 0;
+
+ while (Offset < ListSize && StrNum < ProfileSymbolListCutOff) {
+ StringRef Str(ListStart + Offset);
add(Str);
- Size += Str.size() + 1;
+ Offset += Str.size() + 1;
StrNum++;
}
- if (Size != ListSize && StrNum != ProfileSymbolListCutOff)
+
+ assert(ExpectedCount == StrNum);
+
+ if (Offset != ListSize && StrNum != ProfileSymbolListCutOff)
return sampleprof_error::malformed;
return sampleprof_error::success;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/169730
More information about the llvm-commits
mailing list