[clang] Optimize Cache Insertion with try_emplace for Reduced Lookups (PR #131402)
Ayush Pareek via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 15 12:05:20 PDT 2025
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 <AYUSHPAREEK1980 at GMAIL.COM>
Date: Sat, 15 Mar 2025 03:43:18 +0530
Subject: [PATCH] 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;
}
};
More information about the cfe-commits
mailing list