-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[ProfData] Improve efficiency of reader #169730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-pgo Author: Max (mxms0) ChangesPre-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:
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;
}
|
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.
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.
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.
snehasish
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with some suggested changes.
|
And last time I checked, StringSet was way more efficient for Strings than DenseSet |
The tricky part is that StringSet owns strings, and this loop does not clone strings. Can't tell if it's going to be an improvement. |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
🐧 Linux x64 Test Results
|
snehasish
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice and succint! LGTM.
|
@mxms0 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/138/builds/22537 Here is the relevant piece of the build log for the reference |
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. --------- Co-authored-by: mxms <mxms@google.com> Co-authored-by: Vitaly Buka <vitalybuka@gmail.com>
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.