From 66a4d5eb1817ca24d493c08d3024a40f02206200 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Sat, 23 Feb 2019 23:48:47 +0000 Subject: [PATCH] VFS: Avoid some unnecessary std::string copies Thread Twine a little deeper through the VFS to avoid unnecessarily constructing the same std::string twice in a parameter sequence: Twine -> std::string -> StringRef -> std::string Changing a few parameters from StringRef to Twine avoids the early call to `Twine::str()`. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354739 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/VirtualFileSystem.h | 6 +++--- lib/Support/VirtualFileSystem.cpp | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/llvm/Support/VirtualFileSystem.h b/include/llvm/Support/VirtualFileSystem.h index 7c57dd3a31e..0bb574e1b12 100644 --- a/include/llvm/Support/VirtualFileSystem.h +++ b/include/llvm/Support/VirtualFileSystem.h @@ -58,15 +58,15 @@ public: Status() = default; Status(const llvm::sys::fs::file_status &Status); - Status(StringRef Name, llvm::sys::fs::UniqueID UID, + Status(const Twine &Name, llvm::sys::fs::UniqueID UID, llvm::sys::TimePoint<> MTime, uint32_t User, uint32_t Group, uint64_t Size, llvm::sys::fs::file_type Type, llvm::sys::fs::perms Perms); /// Get a copy of a Status with a different name. - static Status copyWithNewName(const Status &In, StringRef NewName); + static Status copyWithNewName(const Status &In, const Twine &NewName); static Status copyWithNewName(const llvm::sys::fs::file_status &In, - StringRef NewName); + const Twine &NewName); /// Returns the name that should be used for this file or directory. StringRef getName() const { return Name; } diff --git a/lib/Support/VirtualFileSystem.cpp b/lib/Support/VirtualFileSystem.cpp index 1f5c622cfaa..f5a8a1e8a44 100644 --- a/lib/Support/VirtualFileSystem.cpp +++ b/lib/Support/VirtualFileSystem.cpp @@ -66,19 +66,19 @@ Status::Status(const file_status &Status) User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()), Type(Status.type()), Perms(Status.permissions()) {} -Status::Status(StringRef Name, UniqueID UID, sys::TimePoint<> MTime, +Status::Status(const Twine &Name, UniqueID UID, sys::TimePoint<> MTime, uint32_t User, uint32_t Group, uint64_t Size, file_type Type, perms Perms) - : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size), - Type(Type), Perms(Perms) {} + : Name(Name.str()), UID(UID), MTime(MTime), User(User), Group(Group), + Size(Size), Type(Type), Perms(Perms) {} -Status Status::copyWithNewName(const Status &In, StringRef NewName) { +Status Status::copyWithNewName(const Status &In, const Twine &NewName) { return Status(NewName, In.getUniqueID(), In.getLastModificationTime(), In.getUser(), In.getGroup(), In.getSize(), In.getType(), In.getPermissions()); } -Status Status::copyWithNewName(const file_status &In, StringRef NewName) { +Status Status::copyWithNewName(const file_status &In, const Twine &NewName) { return Status(NewName, In.getUniqueID(), In.getLastModificationTime(), In.getUser(), In.getGroup(), In.getSize(), In.type(), In.permissions()); @@ -288,7 +288,7 @@ ErrorOr RealFileSystem::status(const Twine &Path) { if (std::error_code EC = sys::fs::status(adjustPath(Path, Storage), RealStatus)) return EC; - return Status::copyWithNewName(RealStatus, Path.str()); + return Status::copyWithNewName(RealStatus, Path); } ErrorOr> @@ -553,7 +553,7 @@ public: /// Return the \p Status for this node. \p RequestedName should be the name /// through which the caller referred to this node. It will override /// \p Status::Name in the return value, to mimic the behavior of \p RealFile. - Status getStatus(StringRef RequestedName) const { + Status getStatus(const Twine &RequestedName) const { return Status::copyWithNewName(Stat, RequestedName); } llvm::MemoryBuffer *getBuffer() const { return Buffer.get(); } @@ -627,7 +627,7 @@ public: /// Return the \p Status for this node. \p RequestedName should be the name /// through which the caller referred to this node. It will override /// \p Status::Name in the return value, to mimic the behavior of \p RealFile. - Status getStatus(StringRef RequestedName) const { + Status getStatus(const Twine &RequestedName) const { return Status::copyWithNewName(Stat, RequestedName); } InMemoryNode *getChild(StringRef Name) { @@ -661,7 +661,7 @@ public: }; namespace { -Status getNodeStatus(const InMemoryNode *Node, StringRef RequestedName) { +Status getNodeStatus(const InMemoryNode *Node, const Twine &RequestedName) { if (auto Dir = dyn_cast(Node)) return Dir->getStatus(RequestedName); if (auto File = dyn_cast(Node)) @@ -859,7 +859,7 @@ bool InMemoryFileSystem::addHardLink(const Twine &FromPath, llvm::ErrorOr InMemoryFileSystem::status(const Twine &Path) { auto Node = lookupInMemoryNode(*this, Root.get(), Path); if (Node) - return detail::getNodeStatus(*Node, Path.str()); + return detail::getNodeStatus(*Node, Path); return Node.getError(); } @@ -1675,7 +1675,7 @@ static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames, Status ExternalStatus) { Status S = ExternalStatus; if (!UseExternalNames) - S = Status::copyWithNewName(S, Path.str()); + S = Status::copyWithNewName(S, Path); S.IsVFSMapped = true; return S; } @@ -1692,7 +1692,7 @@ ErrorOr RedirectingFileSystem::status(const Twine &Path, return S; } else { // directory auto *DE = cast(E); - return Status::copyWithNewName(DE->getStatus(), Path.str()); + return Status::copyWithNewName(DE->getStatus(), Path); } } -- 2.50.1