Skip to content

[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

Merged

Conversation

kazutakahirata
Copy link
Contributor

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.
@llvmbot
Copy link
Member

llvmbot commented May 18, 2025

@llvm/pr-subscribers-bolt

Author: Kazu Hirata (kazutakahirata)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/140426.diff

2 Files Affected:

  • (modified) bolt/include/bolt/Profile/DataReader.h (+2-2)
  • (modified) bolt/lib/Profile/DataReader.cpp (+2-17)
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()) {

Copy link
Contributor

@aaupov aaupov left a 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?

@kazutakahirata
Copy link
Contributor Author

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!

Copy link
Contributor

@aaupov aaupov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@kazutakahirata kazutakahirata merged commit 465e0da into llvm:main May 18, 2025
10 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_repeated_hash_lookups_bolt branch May 18, 2025 02:44
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants