Skip to content

Commit 920878e

Browse files
committed
[SourceKit] Make FileContent non ref-counted
We never need to have two copies of the same `FileContent` object, so we don’t need a copy constructor and can thus pass it on the stack again, instead of storing it on the heap.
1 parent fd9b4ef commit 920878e

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ namespace {
230230

231231
typedef uint64_t BufferStamp;
232232

233-
struct FileContent : llvm::RefCountedBase<FileContent> {
233+
struct FileContent {
234234
ImmutableTextSnapshotRef Snapshot;
235235
std::string Filename;
236236
std::unique_ptr<llvm::MemoryBuffer> Buffer;
@@ -248,8 +248,6 @@ struct FileContent : llvm::RefCountedBase<FileContent> {
248248
}
249249
};
250250

251-
using FileContentRef = llvm::IntrusiveRefCntPtr<FileContent>;
252-
253251
/// An \c ASTBuildOperations builds an AST. Once the AST is built, it informs
254252
/// a list of \c SwiftASTConsumers about the built AST.
255253
/// It also supports cancellation with the following paradigm: If an \c
@@ -304,7 +302,7 @@ class ASTBuildOperation
304302

305303
/// The contents of all explicit input files of the compiler invoation, which
306304
/// can be determined at construction time of the \c ASTBuildOperation.
307-
const std::vector<FileContentRef> FileContents;
305+
const std::vector<FileContent> FileContents;
308306

309307
/// \c DependencyStamps contains the stamps of all module depenecies needed
310308
/// for the AST build. These stamps are only known after the AST is built.
@@ -366,7 +364,7 @@ class ASTBuildOperation
366364

367365
/// Create a vector of \c FileContents containing all files explicitly
368366
/// referenced by the compiler invocation.
369-
std::vector<FileContentRef> fileContentsForFilesInCompilerInvocation();
367+
std::vector<FileContent> fileContentsForFilesInCompilerInvocation();
370368

371369
public:
372370
ASTBuildOperation(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
@@ -376,7 +374,7 @@ class ASTBuildOperation
376374
DidFinishCallback(DidFinishCallback) {
377375
// const_cast is fine here. We just want to guard against modifying these
378376
// fields later on. It's fine to set them in the constructor.
379-
const_cast<std::vector<FileContentRef> &>(this->FileContents) =
377+
const_cast<std::vector<FileContent> &>(this->FileContents) =
380378
fileContentsForFilesInCompilerInvocation();
381379
}
382380

@@ -387,7 +385,7 @@ class ASTBuildOperation
387385
"not receive their callback.");
388386
}
389387

390-
ArrayRef<FileContentRef> getFileContents() const { return FileContents; }
388+
ArrayRef<FileContent> getFileContents() const { return FileContents; }
391389

392390
/// Returns true if the build operation has finished.
393391
bool isFinished() {
@@ -598,7 +596,7 @@ struct SwiftASTManager::Implementation {
598596

599597
ASTProducerRef getASTProducer(SwiftInvocationRef InvokRef);
600598

601-
FileContentRef
599+
FileContent
602600
getFileContent(StringRef FilePath, bool IsPrimary,
603601
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
604602
std::string &Error) const;
@@ -634,10 +632,10 @@ SwiftASTManager::getMemoryBuffer(StringRef Filename, std::string &Error) {
634632
}
635633

636634
static FrontendInputsAndOutputs
637-
convertFileContentsToInputs(ArrayRef<FileContentRef> contents) {
635+
convertFileContentsToInputs(ArrayRef<FileContent> contents) {
638636
FrontendInputsAndOutputs inputsAndOutputs;
639-
for (FileContentRef content : contents)
640-
inputsAndOutputs.addInput(InputFile(*content));
637+
for (const FileContent &content : contents)
638+
inputsAndOutputs.addInput(InputFile(content));
641639
return inputsAndOutputs;
642640
}
643641

@@ -780,16 +778,15 @@ SwiftASTManager::Implementation::getASTProducer(SwiftInvocationRef InvokRef) {
780778
return Producer;
781779
}
782780

783-
static FileContentRef getFileContentFromSnap(ImmutableTextSnapshotRef Snap,
784-
bool IsPrimary,
785-
StringRef FilePath) {
781+
static FileContent getFileContentFromSnap(ImmutableTextSnapshotRef Snap,
782+
bool IsPrimary, StringRef FilePath) {
786783
auto Buf = llvm::MemoryBuffer::getMemBufferCopy(
787784
Snap->getBuffer()->getText(), FilePath);
788-
return new FileContent(Snap, FilePath.str(), std::move(Buf), IsPrimary,
789-
Snap->getStamp());
785+
return FileContent(Snap, FilePath.str(), std::move(Buf), IsPrimary,
786+
Snap->getStamp());
790787
}
791788

792-
FileContentRef SwiftASTManager::Implementation::getFileContent(
789+
FileContent SwiftASTManager::Implementation::getFileContent(
793790
StringRef UnresolvedPath, bool IsPrimary,
794791
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
795792
std::string &Error) const {
@@ -802,8 +799,8 @@ FileContentRef SwiftASTManager::Implementation::getFileContent(
802799
// No need to check EditorDocs again. We did so above.
803800
auto Stamp = getBufferStamp(FilePath, FileSystem, /*CheckEditorDocs=*/false);
804801
auto Buffer = getMemoryBuffer(FilePath, FileSystem, Error);
805-
return new FileContent(nullptr, UnresolvedPath.str(), std::move(Buffer),
806-
IsPrimary, Stamp);
802+
return FileContent(nullptr, UnresolvedPath.str(), std::move(Buffer),
803+
IsPrimary, Stamp);
807804
}
808805

809806
BufferStamp SwiftASTManager::Implementation::getBufferStamp(
@@ -845,12 +842,12 @@ SwiftASTManager::Implementation::getMemoryBuffer(
845842
return nullptr;
846843
}
847844

848-
std::vector<FileContentRef>
845+
std::vector<FileContent>
849846
ASTBuildOperation::fileContentsForFilesInCompilerInvocation() {
850847
const InvocationOptions &Opts = InvokRef->Impl.Opts;
851848
std::string Error; // is ignored
852849

853-
std::vector<FileContentRef> FileContents;
850+
std::vector<FileContent> FileContents;
854851
FileContents.reserve(
855852
Opts.Invok.getFrontendOptions().InputsAndOutputs.inputCount());
856853

@@ -862,14 +859,13 @@ ASTBuildOperation::fileContentsForFilesInCompilerInvocation() {
862859
bool IsPrimary = input.isPrimary();
863860
auto Content =
864861
ASTManager->Impl.getFileContent(Filename, IsPrimary, FileSystem, Error);
865-
if (!Content->Buffer) {
862+
if (!Content.Buffer) {
866863
LOG_WARN_FUNC("failed getting file contents for " << Filename << ": "
867864
<< Error);
868865
// File may not exist, continue and recover as if it was empty.
869-
Content->Buffer =
870-
llvm::WritableMemoryBuffer::getNewMemBuffer(0, Filename);
866+
Content.Buffer = llvm::WritableMemoryBuffer::getNewMemBuffer(0, Filename);
871867
}
872-
FileContents.push_back(Content);
868+
FileContents.push_back(std::move(Content));
873869
}
874870
assert(FileContents.size() ==
875871
Opts.Invok.getFrontendOptions().InputsAndOutputs.inputCount());
@@ -882,7 +878,7 @@ bool ASTBuildOperation::matchesSourceState(
882878

883879
auto Inputs = Opts.Invok.getFrontendOptions().InputsAndOutputs.getAllInputs();
884880
for (size_t I = 0; I < Inputs.size(); I++) {
885-
if (getFileContents()[I]->Stamp !=
881+
if (getFileContents()[I].Stamp !=
886882
ASTManager->Impl.getBufferStamp(Inputs[I].getFileName(),
887883
OtherFileSystem)) {
888884
return false;
@@ -1011,8 +1007,8 @@ ASTUnitRef ASTBuildOperation::buildASTUnit(std::string &Error) {
10111007

10121008
ASTUnitRef ASTRef = new ASTUnit(++ASTUnitGeneration, ASTManager->Impl.Stats);
10131009
for (auto &Content : getFileContents()) {
1014-
if (Content->Snapshot)
1015-
ASTRef->Impl.Snapshots.push_back(Content->Snapshot);
1010+
if (Content.Snapshot)
1011+
ASTRef->Impl.Snapshots.push_back(Content.Snapshot);
10161012
}
10171013
auto &CompIns = ASTRef->Impl.CompInst;
10181014
auto &Consumer = ASTRef->Impl.CollectDiagConsumer;
@@ -1180,7 +1176,7 @@ ASTBuildOperationRef ASTProducer::getBuildOperationForConsumer(
11801176
std::vector<ImmutableTextSnapshotRef> Snapshots;
11811177
Snapshots.reserve(BuildOp->getFileContents().size());
11821178
for (auto &FileContent : BuildOp->getFileContents()) {
1183-
Snapshots.push_back(FileContent->Snapshot);
1179+
Snapshots.push_back(FileContent.Snapshot);
11841180
}
11851181
if (BuildOp->matchesSourceState(FileSystem)) {
11861182
++Mgr->Impl.Stats->numASTCacheHits;

0 commit comments

Comments
 (0)