https://github.com/ayushpareek2003 updated 
https://github.com/llvm/llvm-project/pull/131402

>From 28f6d8b6677e32f45f5fa55c7c73df5a841d7127 Mon Sep 17 00:00:00 2001
From: Ayush Pareek <ayushpareek1...@gmail.com>
Date: Sat, 15 Mar 2025 03:43:18 +0530
Subject: [PATCH 1/2] Optimize Cache Insertion with try_emplace for Reduced
 Lookups

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
---
 .../DependencyScanningFilesystem.h            | 29 ++++++++++---------
 1 file changed, 15 insertions(+), 14 deletions(-)

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;
   }
 };

>From 3f54a94b7204d049a9a20fb058c485761653f93d Mon Sep 17 00:00:00 2001
From: Ayush Pareek <ayushpareek1...@gmail.com>
Date: Sun, 16 Mar 2025 00:40:54 +0530
Subject: [PATCH 2/2] Update DependencyScanningFilesystem.h

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

Fixed Formatting
---
 .../DependencyScanningFilesystem.h              | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git 
a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h 
b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
index a24ba86dae0ef..a41d34a15f9da 100644
--- 
a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
+++ 
b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
@@ -247,16 +247,13 @@ class DependencyScanningFilesystemLocalCache {
   insertEntryForFilename(StringRef Filename,
                          const CachedFileSystemEntry &Entry) {
     assert(llvm::sys::path::is_absolute_gnu(Filename));
-
-      auto &[CachedEntry, CachedRealPath] = Cache.try_emplace(
+    auto &[CachedEntry, CachedRealPath] = Cache.try_emplace(
           Filename, &Entry, nullptr).first->getValue();
-    
-      if (!CachedEntry) {
-        assert((!CachedEntry && CachedRealPath) && "entry already present");
-        CachedEntry = &Entry;
-      }
-    
-      return *CachedEntry;
+    if (!CachedEntry) {
+      assert((!CachedEntry && CachedRealPath) && "entry already present");
+      CachedEntry = &Entry;
+    }
+    return *CachedEntry;
   }
 
   /// Returns real path associated with the filename or nullptr if none is
@@ -275,12 +272,10 @@ class DependencyScanningFilesystemLocalCache {
     assert(llvm::sys::path::is_absolute_gnu(Filename));
     auto &[CachedEntry, CachedRealPath] = Cache.try_emplace(
           Filename, nullptr, &RealPath).first->getValue();
-    
     if (!CachedRealPath) {
       assert((!CachedRealPath && CachedEntry) && "real path already present");
       CachedRealPath = &RealPath;
     }
-    
     return *CachedRealPath;
   }
 };

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to