-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[BOLT] Avoid repeated hash lookups (NFC) #140426
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
[BOLT] Avoid repeated hash lookups (NFC) #140426
Conversation
We can use try_emplace to succinctly implement GetOrCreateFuncEntry and GetOrCreateFuncMemEntry. Since it's a bit mouthful to say FuncBasicSampleData::ContainerTy(), this patch changes the second parameters to default ones.
@llvm/pr-subscribers-bolt Author: Kazu Hirata (kazutakahirata) ChangesWe can use try_emplace to succinctly implement GetOrCreateFuncEntry Full diff: https://github.com/llvm/llvm-project/pull/140426.diff 2 Files Affected:
diff --git a/bolt/include/bolt/Profile/DataReader.h b/bolt/include/bolt/Profile/DataReader.h
index 80031f8f6ef4a..c91498214e62a 100644
--- a/bolt/include/bolt/Profile/DataReader.h
+++ b/bolt/include/bolt/Profile/DataReader.h
@@ -205,7 +205,7 @@ struct FuncMemData {
FuncMemData() {}
- FuncMemData(StringRef Name, ContainerTy Data)
+ FuncMemData(StringRef Name, ContainerTy Data = ContainerTy())
: Name(Name), Data(std::move(Data)) {}
};
@@ -241,7 +241,7 @@ struct FuncBasicSampleData {
StringRef Name;
ContainerTy Data;
- FuncBasicSampleData(StringRef Name, ContainerTy Data)
+ FuncBasicSampleData(StringRef Name, ContainerTy Data = ContainerTy())
: Name(Name), Data(std::move(Data)) {}
/// Get the number of samples recorded in [Start, End)
diff --git a/bolt/lib/Profile/DataReader.cpp b/bolt/lib/Profile/DataReader.cpp
index 198f7d8642738..079cdc1785eaf 100644
--- a/bolt/lib/Profile/DataReader.cpp
+++ b/bolt/lib/Profile/DataReader.cpp
@@ -1088,26 +1088,11 @@ bool DataReader::hasMemData() {
std::error_code DataReader::parseInNoLBRMode() {
auto GetOrCreateFuncEntry = [&](StringRef Name) {
- auto I = NamesToBasicSamples.find(Name);
- if (I == NamesToBasicSamples.end()) {
- bool Success;
- std::tie(I, Success) = NamesToBasicSamples.insert(std::make_pair(
- Name, FuncBasicSampleData(Name, FuncBasicSampleData::ContainerTy())));
-
- assert(Success && "unexpected result of insert");
- }
- return I;
+ return NamesToBasicSamples.try_emplace(Name, Name).first;
};
auto GetOrCreateFuncMemEntry = [&](StringRef Name) {
- auto I = NamesToMemEvents.find(Name);
- if (I == NamesToMemEvents.end()) {
- bool Success;
- std::tie(I, Success) = NamesToMemEvents.insert(
- std::make_pair(Name, FuncMemData(Name, FuncMemData::ContainerTy())));
- assert(Success && "unexpected result of insert");
- }
- return I;
+ return NamesToMemEvents.try_emplace(Name, Name).first;
};
while (hasBranchData()) {
|
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.
Thank you for the cleanup. We have similar helpers in DataReader::parse
– would you mind covering them as well?
@aaupov Sure. I've revised this PR instead of creating another one. Please take a look. Thanks! |
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, thank you!
We can use try_emplace to succinctly implement GetOrCreateFuncEntry and GetOrCreateFuncMemEntry. Since it's a bit mouthful to say FuncBasicSampleData::ContainerTy(), this patch changes the second parameters to default ones.
We can use try_emplace to succinctly implement GetOrCreateFuncEntry
and GetOrCreateFuncMemEntry. Since it's a bit mouthful to say
FuncBasicSampleData::ContainerTy(), this patch changes the second
parameters to default ones.