-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Optimize Cache Insertion with try_emplace for Reduced Lookups #131402
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
|
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-clang Author: Ayush Pareek (ayushpareek2003) Changesfor the functions- insertEntryForFilename() , insertRealPathForFilename() Replaced Full diff: https://github.com/llvm/llvm-project/pull/131402.diff 1 Files Affected:
diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
index d12814e7c9253..a24ba86dae0ef 100644
--- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
+++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
@@ -247,15 +247,16 @@ class DependencyScanningFilesystemLocalCache {
insertEntryForFilename(StringRef Filename,
const CachedFileSystemEntry &Entry) {
assert(llvm::sys::path::is_absolute_gnu(Filename));
- auto [It, Inserted] = Cache.insert({Filename, {&Entry, nullptr}});
- auto &[CachedEntry, CachedRealPath] = It->getValue();
- if (!Inserted) {
- // The file is already present in the local cache. If we got here, it only
- // contains the real path. Let's make sure the entry is populated too.
- assert((!CachedEntry && CachedRealPath) && "entry already present");
- CachedEntry = &Entry;
- }
- return *CachedEntry;
+
+ auto &[CachedEntry, CachedRealPath] = Cache.try_emplace(
+ Filename, &Entry, nullptr).first->getValue();
+
+ if (!CachedEntry) {
+ assert((!CachedEntry && CachedRealPath) && "entry already present");
+ CachedEntry = &Entry;
+ }
+
+ return *CachedEntry;
}
/// Returns real path associated with the filename or nullptr if none is
@@ -272,14 +273,14 @@ class DependencyScanningFilesystemLocalCache {
insertRealPathForFilename(StringRef Filename,
const CachedRealPath &RealPath) {
assert(llvm::sys::path::is_absolute_gnu(Filename));
- auto [It, Inserted] = Cache.insert({Filename, {nullptr, &RealPath}});
- auto &[CachedEntry, CachedRealPath] = It->getValue();
- if (!Inserted) {
- // The file is already present in the local cache. If we got here, it only
- // contains the entry. Let's make sure the real path is populated too.
+ auto &[CachedEntry, CachedRealPath] = Cache.try_emplace(
+ Filename, nullptr, &RealPath).first->getValue();
+
+ if (!CachedRealPath) {
assert((!CachedRealPath && CachedEntry) && "real path already present");
CachedRealPath = &RealPath;
}
+
return *CachedRealPath;
}
};
|
You can test this locally with the following command:git-clang-format --diff fbf0276b6a7a7a4508c373cf87fc349569652659 28f6d8b6677e32f45f5fa55c7c73df5a841d7127 --extensions h -- clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.hView the diff from clang-format here.diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
index a24ba86dae..08e6a2e678 100644
--- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
+++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
@@ -248,15 +248,15 @@ public:
const CachedFileSystemEntry &Entry) {
assert(llvm::sys::path::is_absolute_gnu(Filename));
- auto &[CachedEntry, CachedRealPath] = Cache.try_emplace(
- Filename, &Entry, nullptr).first->getValue();
-
- if (!CachedEntry) {
- assert((!CachedEntry && CachedRealPath) && "entry already present");
- CachedEntry = &Entry;
- }
-
- return *CachedEntry;
+ auto &[CachedEntry, CachedRealPath] =
+ Cache.try_emplace(Filename, &Entry, nullptr).first->getValue();
+
+ if (!CachedEntry) {
+ assert((!CachedEntry && CachedRealPath) && "entry already present");
+ CachedEntry = &Entry;
+ }
+
+ return *CachedEntry;
}
/// Returns real path associated with the filename or nullptr if none is
@@ -273,14 +273,14 @@ public:
insertRealPathForFilename(StringRef Filename,
const CachedRealPath &RealPath) {
assert(llvm::sys::path::is_absolute_gnu(Filename));
- auto &[CachedEntry, CachedRealPath] = Cache.try_emplace(
- Filename, nullptr, &RealPath).first->getValue();
-
+ auto &[CachedEntry, CachedRealPath] =
+ Cache.try_emplace(Filename, nullptr, &RealPath).first->getValue();
+
if (!CachedRealPath) {
assert((!CachedRealPath && CachedEntry) && "real path already present");
CachedRealPath = &RealPath;
}
-
+
return *CachedRealPath;
}
};
|
ayushpareek2003
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.
Removed unnecessary spaces
for the functions- insertEntryForFilename() , insertRealPathForFilename()
Replaced
Cache.insert()withCache.try_emplace()to reduce redundant lookupsImproved efficiency by avoiding unnecessary copying of values when the key already exists