Skip to content

Commit fe07d44

Browse files
committed
[CodeCompletion] Pack cached instance information into single struct
To prevent them from out-of-sync.
1 parent 044477e commit fe07d44

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

include/swift/IDE/CompletionInstance.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ class CompletionInstance {
4040
unsigned MaxASTReuseCount = 100;
4141
bool EnableASTCaching = false;
4242

43-
std::shared_ptr<CompilerInstance> CachedCI = nullptr;
44-
llvm::hash_code CachedArgsHash;
45-
unsigned CurrentASTReuseCount = 0;
43+
struct CachedInstance {
44+
CompilerInstance CI;
45+
llvm::hash_code ArgHash;
46+
unsigned ReuseCound = 0;
47+
};
48+
std::shared_ptr<CachedInstance> CachedInst;
4649

4750
/// Calls \p Callback with cached \c CompilerInstance if it's usable for the
4851
/// specified completion request.

lib/IDE/CompletionInstance.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,23 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
139139
return false;
140140

141141
// Temporary move the CI so other threads don't use the same instance.
142-
std::shared_ptr<CompilerInstance> CI;
143-
CI.swap(CachedCI);
142+
std::shared_ptr<CachedInstance> CachedI(nullptr);
143+
CachedInst.swap(CachedI);
144144

145-
if (!CI)
145+
if (!CachedI)
146146
return false;
147-
if (CurrentASTReuseCount >= MaxASTReuseCount)
147+
if (CachedI->ReuseCound >= MaxASTReuseCount)
148148
return false;
149-
if (ArgsHash != CachedArgsHash)
149+
if (CachedI->ArgHash != ArgsHash)
150150
return false;
151151

152-
auto &oldState = CI->getPersistentParserState();
152+
auto &CI = CachedI->CI;
153+
154+
auto &oldState = CachedI->CI.getPersistentParserState();
153155
if (!oldState.hasCodeCompletionDelayedDeclState())
154156
return false;
155157

156-
auto &SM = CI->getSourceMgr();
158+
auto &SM = CI.getSourceMgr();
157159
if (SM.getIdentifierForBuffer(SM.getCodeCompletionBufferID()) !=
158160
completionBuffer->getBufferIdentifier())
159161
return false;
@@ -230,19 +232,18 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
230232
if (AFD->isBodySkipped())
231233
AFD->setBodyDelayed(AFD->getBodySourceRange());
232234
if (DiagC)
233-
CI->addDiagnosticConsumer(DiagC);
235+
CI.addDiagnosticConsumer(DiagC);
234236

235-
CI->getDiags().diagnose(SM.getLocForOffset(BufferID, newInfo.StartOffset),
237+
CI.getDiags().diagnose(SM.getLocForOffset(BufferID, newInfo.StartOffset),
236238
diag::completion_reusing_astcontext);
237239

238-
Callback(*CI);
240+
Callback(CI);
239241

240242
if (DiagC)
241-
CI->removeDiagnosticConsumer(DiagC);
243+
CI.removeDiagnosticConsumer(DiagC);
242244

243-
CachedCI.swap(CI);
244-
CachedArgsHash = ArgsHash;
245-
CurrentASTReuseCount += 1;
245+
CachedI->ReuseCound += 1;
246+
CachedInst.swap(CachedI);
246247

247248
return true;
248249
}
@@ -253,32 +254,32 @@ bool CompletionInstance::performNewOperation(
253254
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
254255
std::string &Error, DiagnosticConsumer *DiagC,
255256
llvm::function_ref<void(CompilerInstance &)> Callback) {
256-
CachedCI.reset();
257-
auto CI = std::make_shared<CompilerInstance>();
257+
CachedInst.reset();
258+
auto TheInstance = std::make_shared<CachedInstance>();
259+
auto &CI = TheInstance->CI;
258260
if (DiagC)
259-
CI->addDiagnosticConsumer(DiagC);
261+
CI.addDiagnosticConsumer(DiagC);
260262

261263
if (FileSystem != llvm::vfs::getRealFileSystem())
262-
CI->getSourceMgr().setFileSystem(FileSystem);
264+
CI.getSourceMgr().setFileSystem(FileSystem);
263265

264266
Invocation.setCodeCompletionPoint(completionBuffer, Offset);
265267

266-
if (CI->setup(Invocation)) {
268+
if (CI.setup(Invocation)) {
267269
Error = "failed to setup compiler instance";
268270
return false;
269271
}
270-
registerIDERequestFunctions(CI->getASTContext().evaluator);
272+
registerIDERequestFunctions(CI.getASTContext().evaluator);
271273

272-
CI->performParseAndResolveImportsOnly();
273-
Callback(*CI);
274+
CI.performParseAndResolveImportsOnly();
275+
Callback(CI);
274276

275277
if (DiagC)
276-
CI->removeDiagnosticConsumer(DiagC);
278+
CI.removeDiagnosticConsumer(DiagC);
277279

278280
if (EnableASTCaching) {
279-
CachedCI.swap(CI);
280-
CachedArgsHash = ArgsHash;
281-
CurrentASTReuseCount = 0;
281+
TheInstance->ArgHash = ArgsHash;
282+
CachedInst.swap(TheInstance);
282283
}
283284

284285
return true;

0 commit comments

Comments
 (0)