summaryrefslogtreecommitdiff
path: root/lldb/source/Utility/FileSpec.cpp
diff options
context:
space:
mode:
authorGreg Clayton <gclayton@fb.com>2022-07-22 13:18:40 -0700
committerGreg Clayton <gclayton@fb.com>2022-07-22 14:04:52 -0700
commitdabe877248b85b34878e75d5510339325ee087d0 (patch)
tree9fd55ec75af263db6a6762f3b0011bf87587a285 /lldb/source/Utility/FileSpec.cpp
parent0bbce7a4c2d2bff622bdadd4323f93f5d90e6d24 (diff)
Cache the value for absolute path in FileSpec.
Checking if a path is absolute can be expensive and currently the result is not cached in the FileSpec object. This patch adds caching and also code to clear the cache if the file is modified. Differential Revision: https://reviews.llvm.org/D130396
Diffstat (limited to 'lldb/source/Utility/FileSpec.cpp')
-rw-r--r--lldb/source/Utility/FileSpec.cpp33
1 files changed, 21 insertions, 12 deletions
diff --git a/lldb/source/Utility/FileSpec.cpp b/lldb/source/Utility/FileSpec.cpp
index 7646d3326422..48b922970d87 100644
--- a/lldb/source/Utility/FileSpec.cpp
+++ b/lldb/source/Utility/FileSpec.cpp
@@ -170,9 +170,7 @@ void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
// up into a directory and filename and stored as uniqued string values for
// quick comparison and efficient memory usage.
void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
- m_filename.Clear();
- m_directory.Clear();
- m_is_resolved = false;
+ Clear();
m_style = (style == Style::native) ? GetNativeStyle() : style;
if (pathname.empty())
@@ -259,6 +257,7 @@ Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
void FileSpec::Clear() {
m_directory.Clear();
m_filename.Clear();
+ PathWasModified();
}
// Compare two FileSpec objects. If "full" is true, then both the directory and
@@ -332,26 +331,32 @@ FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
void FileSpec::SetDirectory(ConstString directory) {
m_directory = directory;
+ PathWasModified();
}
void FileSpec::SetDirectory(llvm::StringRef directory) {
m_directory = ConstString(directory);
+ PathWasModified();
}
void FileSpec::SetFilename(ConstString filename) {
m_filename = filename;
+ PathWasModified();
}
void FileSpec::SetFilename(llvm::StringRef filename) {
m_filename = ConstString(filename);
+ PathWasModified();
}
void FileSpec::ClearFilename() {
m_filename.Clear();
+ PathWasModified();
}
void FileSpec::ClearDirectory() {
m_directory.Clear();
+ PathWasModified();
}
// Extract the directory and path into a fixed buffer. This is needed as the
@@ -488,18 +493,22 @@ bool FileSpec::IsRelative() const {
}
bool FileSpec::IsAbsolute() const {
-llvm::SmallString<64> current_path;
- GetPath(current_path, false);
+ // Check if we have cached if this path is absolute to avoid recalculating.
+ if (m_absolute != Absolute::Calculate)
+ return m_absolute == Absolute::Yes;
- // Early return if the path is empty.
- if (current_path.empty())
- return false;
+ m_absolute = Absolute::No;
- // We consider paths starting with ~ to be absolute.
- if (current_path[0] == '~')
- return true;
+ llvm::SmallString<64> path;
+ GetPath(path, false);
+
+ if (!path.empty()) {
+ // We consider paths starting with ~ to be absolute.
+ if (path[0] == '~' || llvm::sys::path::is_absolute(path, m_style))
+ m_absolute = Absolute::Yes;
+ }
- return llvm::sys::path::is_absolute(current_path, m_style);
+ return m_absolute == Absolute::Yes;
}
void FileSpec::MakeAbsolute(const FileSpec &dir) {