Skip to content

Conversation

@ayushpareek2003
Copy link

for the functions- insertEntryForFilename() , insertRealPathForFilename()

Replaced Cache.insert() with Cache.try_emplace() to reduce redundant lookups
Improved efficiency by avoiding unnecessary copying of values when the key already exists

@github-actions
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Mar 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 14, 2025

@llvm/pr-subscribers-clang

Author: Ayush Pareek (ayushpareek2003)

Changes

for the functions- insertEntryForFilename() , insertRealPathForFilename()

Replaced Cache.insert() with Cache.try_emplace() to reduce redundant lookups
Improved efficiency by avoiding unnecessary copying of values when the key already exists


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

1 Files Affected:

  • (modified) clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h (+15-14)
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;
   }
 };

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff fbf0276b6a7a7a4508c373cf87fc349569652659 28f6d8b6677e32f45f5fa55c7c73df5a841d7127 --extensions h -- clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
View 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;
   }
 };

Copy link
Author

@ayushpareek2003 ayushpareek2003 left a comment

Choose a reason for hiding this comment

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

Removed unnecessary spaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants