Skip to content

Commit 1115ecc

Browse files
committed
[SourceKit] Pass 'EnableASTCaching' flag as an argument
so that it is associated with a specific completion. (cherry picked from commit eebcbf6)
1 parent e12d4de commit 1115ecc

File tree

8 files changed

+68
-68
lines changed

8 files changed

+68
-68
lines changed

include/swift/IDE/CompletionInstance.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,18 @@ makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf,
3838
/// Manages \c CompilerInstance for completion like operations.
3939
class CompletionInstance {
4040
unsigned MaxASTReuseCount = 100;
41-
bool EnableASTCaching = false;
4241

4342
std::mutex mtx;
4443

4544
std::unique_ptr<CompilerInstance> CachedCI;
4645
llvm::hash_code CachedArgHash;
47-
unsigned CachedReuseCound = 0;
46+
unsigned CachedReuseCount = 0;
4847

4948
/// Calls \p Callback with cached \c CompilerInstance if it's usable for the
5049
/// specified completion request.
51-
/// Returns \c if the callback was called. Returns \c false if the
52-
/// functionality is disabled, compiler argument has
53-
/// changed, primary file is not the same, the \c Offset is not in function
54-
/// bodies, or the interface hash of the file has changed.
50+
/// Returns \c if the callback was called. Returns \c false if the compiler
51+
/// argument has changed, primary file is not the same, the \c Offset is not
52+
/// in function bodies, or the interface hash of the file has changed.
5553
bool performCachedOperaitonIfPossible(
5654
const swift::CompilerInvocation &Invocation, llvm::hash_code ArgsHash,
5755
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
@@ -63,15 +61,14 @@ class CompletionInstance {
6361
/// the first pass.
6462
/// Returns \c false if it fails to setup the \c CompilerInstance.
6563
bool performNewOperation(
66-
swift::CompilerInvocation &Invocation, llvm::hash_code ArgsHash,
64+
llvm::Optional<llvm::hash_code> ArgsHash,
65+
swift::CompilerInvocation &Invocation,
6766
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
6867
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
6968
std::string &Error, DiagnosticConsumer *DiagC,
7069
llvm::function_ref<void(CompilerInstance &)> Callback);
7170

7271
public:
73-
void setEnableASTCaching(bool Flag) { EnableASTCaching = Flag; }
74-
7572
/// Calls \p Callback with a \c CompilerInstance which is prepared for the
7673
/// second pass. \p Callback is resposible to perform the second pass on it.
7774
/// The \c CompilerInstance may be reused from the previous completions,
@@ -82,13 +79,12 @@ class CompletionInstance {
8279
/// NOTE: \p Args is only used for checking the equaity of the invocation.
8380
/// Since this function assumes that it is already normalized, exact the same
8481
/// arguments including their order is considered as the same invocation.
85-
bool
86-
performOperation(swift::CompilerInvocation &Invocation,
87-
llvm::ArrayRef<const char *> Args,
88-
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
89-
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
90-
std::string &Error, DiagnosticConsumer *DiagC,
91-
llvm::function_ref<void(CompilerInstance &)> Callback);
82+
bool performOperation(
83+
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
84+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
85+
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
86+
bool EnableASTCaching, std::string &Error, DiagnosticConsumer *DiagC,
87+
llvm::function_ref<void(CompilerInstance &)> Callback);
9288
};
9389

9490
} // namespace ide

lib/IDE/CompletionInstance.cpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,9 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
135135
DiagnosticConsumer *DiagC,
136136
llvm::function_ref<void(CompilerInstance &)> Callback) {
137137

138-
if (!EnableASTCaching)
139-
return false;
140-
141138
if (!CachedCI)
142139
return false;
143-
144-
if (CachedReuseCound >= MaxASTReuseCount)
140+
if (CachedReuseCount >= MaxASTReuseCount)
145141
return false;
146142
if (CachedArgHash != ArgsHash)
147143
return false;
@@ -239,13 +235,13 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
239235
if (DiagC)
240236
CI.removeDiagnosticConsumer(DiagC);
241237

242-
CachedReuseCound += 1;
238+
CachedReuseCount += 1;
243239

244240
return true;
245241
}
246242

247243
bool CompletionInstance::performNewOperation(
248-
swift::CompilerInvocation &Invocation, llvm::hash_code ArgsHash,
244+
Optional<llvm::hash_code> ArgsHash, swift::CompilerInvocation &Invocation,
249245
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
250246
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
251247
std::string &Error, DiagnosticConsumer *DiagC,
@@ -273,10 +269,10 @@ bool CompletionInstance::performNewOperation(
273269
if (DiagC)
274270
CI.removeDiagnosticConsumer(DiagC);
275271

276-
if (EnableASTCaching) {
272+
if (ArgsHash.hasValue()) {
277273
CachedCI = std::move(TheInstance);
278-
CachedArgHash = ArgsHash;
279-
CachedReuseCound = 0;
274+
CachedArgHash = *ArgsHash;
275+
CachedReuseCount = 0;
280276
}
281277

282278
return true;
@@ -286,7 +282,7 @@ bool swift::ide::CompletionInstance::performOperation(
286282
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
287283
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
288284
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
289-
std::string &Error, DiagnosticConsumer *DiagC,
285+
bool EnableASTCaching, std::string &Error, DiagnosticConsumer *DiagC,
290286
llvm::function_ref<void(CompilerInstance &)> Callback) {
291287

292288
// Always disable source location resolutions from .swiftsourceinfo file
@@ -300,24 +296,29 @@ bool swift::ide::CompletionInstance::performOperation(
300296
// FIXME: ASTScopeLookup doesn't support code completion yet.
301297
Invocation.disableASTScopeLookup();
302298

303-
// Compute the signature of the invocation.
304-
llvm::hash_code ArgsHash(0);
305-
for (auto arg : Args)
306-
ArgsHash = llvm::hash_combine(ArgsHash, StringRef(arg));
307-
308-
// If AST caching is enabled, block completions in other threads. So they
309-
// have higher chance to use the cached completion instance.
310-
Optional<std::lock_guard<std::mutex>> lock;
311-
if (EnableASTCaching)
312-
lock.emplace(mtx);
313-
314-
if (performCachedOperaitonIfPossible(Invocation, ArgsHash, completionBuffer,
315-
Offset, DiagC, Callback))
316-
return true;
317-
318-
if (performNewOperation(Invocation, ArgsHash, FileSystem, completionBuffer,
319-
Offset, Error, DiagC, Callback))
320-
return true;
299+
if (EnableASTCaching) {
300+
// Compute the signature of the invocation.
301+
llvm::hash_code ArgsHash(0);
302+
for (auto arg : Args)
303+
ArgsHash = llvm::hash_combine(ArgsHash, StringRef(arg));
304+
305+
// Concurrent completions will block so that they have higher chance to use
306+
// the cached completion instance.
307+
std::lock_guard<std::mutex> lock(mtx);
308+
309+
if (performCachedOperaitonIfPossible(Invocation, ArgsHash, completionBuffer,
310+
Offset, DiagC, Callback))
311+
return true;
312+
313+
if (performNewOperation(ArgsHash, Invocation, FileSystem, completionBuffer,
314+
Offset, Error, DiagC, Callback))
315+
return true;
316+
} else {
317+
// Concurrent completions may happen in parallel when caching is disabled.
318+
if (performNewOperation(None, Invocation, FileSystem, completionBuffer,
319+
Offset, Error, DiagC, Callback))
320+
return true;
321+
}
321322

322323
assert(!Error.empty());
323324
return false;

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ static bool swiftCodeCompleteImpl(
123123
unsigned Offset, SwiftCodeCompletionConsumer &SwiftConsumer,
124124
ArrayRef<const char *> Args,
125125
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
126-
std::string &Error) {
126+
bool EnableASTCaching, std::string &Error) {
127127
return Lang.performCompletionLikeOperation(
128-
UnresolvedInputFile, Offset, Args, FileSystem, Error,
128+
UnresolvedInputFile, Offset, Args, FileSystem, EnableASTCaching, Error,
129129
[&](CompilerInstance &CI) {
130130
// Create a factory for code completion callbacks that will feed the
131131
// Consumer.
@@ -155,7 +155,6 @@ void SwiftLangSupport::codeComplete(
155155
SourceKit::CodeCompletionConsumer &SKConsumer, ArrayRef<const char *> Args,
156156
Optional<VFSOptions> vfsOptions) {
157157

158-
159158
CodeCompletion::Options CCOpts;
160159
if (options) {
161160
StringRef filterText;
@@ -164,7 +163,6 @@ void SwiftLangSupport::codeComplete(
164163
translateCodeCompletionOptions(*options, CCOpts, filterText, resultOffset,
165164
maxResults);
166165
}
167-
CompletionInst->setEnableASTCaching(CCOpts.reuseASTContextIfPossible);
168166

169167
std::string error;
170168
// FIXME: the use of None as primary file is to match the fact we do not read
@@ -210,7 +208,8 @@ void SwiftLangSupport::codeComplete(
210208

211209
std::string Error;
212210
if (!swiftCodeCompleteImpl(*this, UnresolvedInputFile, Offset, SwiftConsumer,
213-
Args, fileSystem, Error)) {
211+
Args, fileSystem,
212+
CCOpts.reuseASTContextIfPossible, Error)) {
214213
SKConsumer.failed(Error);
215214
}
216215
}
@@ -1080,7 +1079,8 @@ static void transformAndForwardResults(
10801079
cargs.push_back(arg.c_str());
10811080
std::string error;
10821081
if (!swiftCodeCompleteImpl(lang, buffer.get(), str.size(), swiftConsumer,
1083-
cargs, session->getFileSystem(), error)) {
1082+
cargs, session->getFileSystem(),
1083+
options.reuseASTContextIfPossible, error)) {
10841084
consumer.failed(error);
10851085
return;
10861086
}
@@ -1128,7 +1128,6 @@ void SwiftLangSupport::codeCompleteOpen(
11281128
if (options)
11291129
translateCodeCompletionOptions(*options, CCOpts, filterText, resultOffset,
11301130
maxResults);
1131-
CompletionInst->setEnableASTCaching(CCOpts.reuseASTContextIfPossible);
11321131

11331132
std::string error;
11341133
// FIXME: the use of None as primary file is to match the fact we do not read
@@ -1179,7 +1178,8 @@ void SwiftLangSupport::codeCompleteOpen(
11791178

11801179
// Invoke completion.
11811180
if (!swiftCodeCompleteImpl(*this, inputBuf, offset, swiftConsumer,
1182-
extendedArgs, fileSystem, error)) {
1181+
extendedArgs, fileSystem,
1182+
CCOpts.reuseASTContextIfPossible, error)) {
11831183
consumer.failed(error);
11841184
return;
11851185
}
@@ -1249,7 +1249,6 @@ void SwiftLangSupport::codeCompleteUpdate(
12491249
if (options)
12501250
translateCodeCompletionOptions(*options, CCOpts, filterText, resultOffset,
12511251
maxResults);
1252-
CompletionInst->setEnableASTCaching(CCOpts.reuseASTContextIfPossible);
12531252

12541253
NameToPopularityMap *nameToPopularity = nullptr;
12551254
// This reference must outlive the uses of nameToPopularity.

tools/SourceKit/lib/SwiftLang/SwiftConformingMethodList.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ static bool swiftConformingMethodListImpl(
3232
ArrayRef<const char *> ExpectedTypeNames,
3333
ide::ConformingMethodListConsumer &Consumer,
3434
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
35-
std::string &Error) {
35+
bool EnableASTCaching, std::string &Error) {
3636
return Lang.performCompletionLikeOperation(
37-
UnresolvedInputFile, Offset, Args, FileSystem, Error,
37+
UnresolvedInputFile, Offset, Args, FileSystem, EnableASTCaching, Error,
3838
[&](CompilerInstance &CI) {
3939
// Create a factory for code completion callbacks that will feed the
4040
// Consumer.
@@ -176,7 +176,7 @@ void SwiftLangSupport::getConformingMethodList(
176176

177177
if (!swiftConformingMethodListImpl(*this, UnresolvedInputFile, Offset, Args,
178178
ExpectedTypeNames, Consumer, fileSystem,
179-
error)) {
179+
/*EnableASTCaching=*/false, error)) {
180180
SKConsumer.failed(error);
181181
}
182182
}

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,8 @@ bool SwiftLangSupport::performCompletionLikeOperation(
956956
llvm::MemoryBuffer *UnresolvedInputFile, unsigned Offset,
957957
ArrayRef<const char *> Args,
958958
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
959-
std::string &Error, llvm::function_ref<void(CompilerInstance &)> Callback) {
959+
bool EnableASTCaching, std::string &Error,
960+
llvm::function_ref<void(CompilerInstance &)> Callback) {
960961
assert(FileSystem);
961962

962963
// Resolve symlinks for the input file; we resolve them for the input files
@@ -1010,7 +1011,8 @@ bool SwiftLangSupport::performCompletionLikeOperation(
10101011
auto CompletionInst = getCompletionInstance();
10111012

10121013
return CompletionInst->performOperation(Invocation, Args, FileSystem,
1013-
newBuffer.get(), Offset, Error,
1014+
newBuffer.get(), Offset,
1015+
EnableASTCaching, Error,
10141016
&CIDiags, Callback);
10151017
}
10161018

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class SwiftLangSupport : public LangSupport {
443443
llvm::MemoryBuffer *UnresolvedInputFile, unsigned Offset,
444444
ArrayRef<const char *> Args,
445445
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
446-
std::string &Error,
446+
bool EnableASTCaching, std::string &Error,
447447
llvm::function_ref<void(swift::CompilerInstance &)> Callback);
448448

449449
//==========================================================================//

tools/SourceKit/lib/SwiftLang/SwiftTypeContextInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ static bool swiftTypeContextInfoImpl(
3030
unsigned Offset, ide::TypeContextInfoConsumer &Consumer,
3131
ArrayRef<const char *> Args,
3232
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
33-
std::string &Error) {
33+
bool EnableASTCaching, std::string &Error) {
3434
return Lang.performCompletionLikeOperation(
35-
UnresolvedInputFile, Offset, Args, FileSystem, Error,
35+
UnresolvedInputFile, Offset, Args, FileSystem, EnableASTCaching, Error,
3636
[&](CompilerInstance &CI) {
3737
// Create a factory for code completion callbacks that will feed the
3838
// Consumer.
@@ -151,7 +151,8 @@ void SwiftLangSupport::getExpressionContextInfo(
151151
} Consumer(SKConsumer);
152152

153153
if (!swiftTypeContextInfoImpl(*this, UnresolvedInputFile, Offset, Consumer,
154-
Args, fileSystem, error)) {
154+
Args, fileSystem, /*EnableASTCaching=*/false,
155+
error)) {
155156
SKConsumer.failed(error);
156157
}
157158
}

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,12 @@ static bool doCodeCompletionImpl(
759759
CompletionInstance CompletionInst;
760760
auto isSuccess = CompletionInst.performOperation(
761761
Invocation, /*Args=*/{}, llvm::vfs::getRealFileSystem(), CleanFile.get(),
762-
Offset, Error, CodeCompletionDiagnostics ? &PrintDiags : nullptr,
763-
[&](CompilerInstance &CI) {
764-
performCodeCompletionSecondPass(CI.getPersistentParserState(),
765-
*callbacksFactory);
766-
});
762+
Offset, /*EnableASTCaching=*/false, Error,
763+
CodeCompletionDiagnostics ? &PrintDiags : nullptr,
764+
[&](CompilerInstance &CI) {
765+
performCodeCompletionSecondPass(CI.getPersistentParserState(),
766+
*callbacksFactory);
767+
});
767768
return isSuccess ? 0 : 1;
768769
}
769770

0 commit comments

Comments
 (0)