Skip to content

Commit fd9b4ef

Browse files
committed
[SourceKit] Remove some dupliate computation of realpaths
1 parent cbf42ee commit fd9b4ef

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ struct SwiftASTManager::Implementation {
605605

606606
BufferStamp
607607
getBufferStamp(StringRef FilePath,
608-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem) const;
608+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
609+
bool CheckEditorDocs = true) const;
609610

610611
std::unique_ptr<llvm::MemoryBuffer>
611612
getMemoryBuffer(StringRef Filename,
@@ -793,24 +794,29 @@ FileContentRef SwiftASTManager::Implementation::getFileContent(
793794
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
794795
std::string &Error) const {
795796
std::string FilePath = SwiftLangSupport::resolvePathSymlinks(UnresolvedPath);
796-
if (auto EditorDoc = EditorDocs->findByPath(FilePath))
797+
if (auto EditorDoc = EditorDocs->findByPath(FilePath, /*IsRealpath=*/true))
797798
return getFileContentFromSnap(EditorDoc->getLatestSnapshot(), IsPrimary,
798799
FilePath);
799800

800801
// FIXME: Is there a way to get timestamp and buffer for a file atomically ?
801-
auto Stamp = getBufferStamp(FilePath, FileSystem);
802+
// No need to check EditorDocs again. We did so above.
803+
auto Stamp = getBufferStamp(FilePath, FileSystem, /*CheckEditorDocs=*/false);
802804
auto Buffer = getMemoryBuffer(FilePath, FileSystem, Error);
803805
return new FileContent(nullptr, UnresolvedPath.str(), std::move(Buffer),
804806
IsPrimary, Stamp);
805807
}
806808

807809
BufferStamp SwiftASTManager::Implementation::getBufferStamp(
808810
StringRef FilePath,
809-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem) const {
811+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
812+
bool CheckEditorDocs) const {
810813
assert(FileSystem);
811814

812-
if (auto EditorDoc = EditorDocs->findByPath(FilePath))
813-
return EditorDoc->getLatestSnapshot()->getStamp();
815+
if (CheckEditorDocs) {
816+
if (auto EditorDoc = EditorDocs->findByPath(FilePath)) {
817+
return EditorDoc->getLatestSnapshot()->getStamp();
818+
}
819+
}
814820

815821
auto StatusOrErr = FileSystem->status(FilePath);
816822
if (std::error_code Err = StatusOrErr.getError()) {

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,18 @@ SwiftEditorDocumentFileMap::getByUnresolvedName(StringRef FilePath) {
245245
}
246246

247247
SwiftEditorDocumentRef
248-
SwiftEditorDocumentFileMap::findByPath(StringRef FilePath) {
248+
SwiftEditorDocumentFileMap::findByPath(StringRef FilePath, bool IsRealpath) {
249249
SwiftEditorDocumentRef EditorDoc;
250250

251-
std::string ResolvedPath = SwiftLangSupport::resolvePathSymlinks(FilePath);
251+
std::string Scratch;
252+
if (!IsRealpath) {
253+
Scratch = SwiftLangSupport::resolvePathSymlinks(FilePath);
254+
FilePath = Scratch;
255+
}
252256
Queue.dispatchSync([&]{
253257
for (auto &Entry : Docs) {
254258
if (Entry.getKey() == FilePath ||
255-
Entry.getValue().ResolvedPath == ResolvedPath) {
259+
Entry.getValue().ResolvedPath == FilePath) {
256260
EditorDoc = Entry.getValue().DocRef;
257261
break;
258262
}

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ class SwiftEditorDocumentFileMap {
156156
/// Looks up the document only by the path name that was given initially.
157157
SwiftEditorDocumentRef getByUnresolvedName(StringRef FilePath);
158158
/// Looks up the document by resolving symlinks in the paths.
159-
SwiftEditorDocumentRef findByPath(StringRef FilePath);
159+
/// If \p IsRealpath is \c true, then \p FilePath must already be
160+
/// canonicalized to a realpath.
161+
SwiftEditorDocumentRef findByPath(StringRef FilePath,
162+
bool IsRealpath = false);
160163
SwiftEditorDocumentRef remove(StringRef FilePath);
161164
};
162165

0 commit comments

Comments
 (0)