Skip to content

Commit 1898b63

Browse files
authored
Merge pull request #13147 from benlangmuir/sourcekitd-sema-delay-throttle
[sourcekitd] Be more aggresive with the semantic editor delay to avoid being throttled
2 parents e3be361 + f373ef3 commit 1898b63

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

tools/SourceKit/tools/sourcekitd/bin/XPC/Client/sourcekitd.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ static void updateSemanticEditorDelay() {
409409
using namespace std::chrono;
410410
using TimePoint = time_point<system_clock, nanoseconds>;
411411

412+
// This minimum is chosen to keep us from being throttled by XPC.
413+
static const size_t MinDelaySeconds = 10;
414+
static const size_t MaxDelaySeconds = 20;
415+
412416
// Clear any previous setting.
413417
SemanticEditorDelaySecondsNum = 0;
414418

@@ -417,16 +421,13 @@ static void updateSemanticEditorDelay() {
417421
TimePoint PrevTime = gPrevCrashTime;
418422
TimePoint CurrTime = system_clock::now();
419423
gPrevCrashTime = CurrTime;
420-
if (PrevTime == TimePoint()) {
421-
// First time that it crashed.
422-
return;
423-
}
424424

425425
auto Diff = duration_cast<seconds>(CurrTime - PrevTime);
426+
size_t Delay = Diff.count()*2 + 1;
426427
if (Diff.count() > 30)
427-
return; // treat this as more likely unrelated to the previous crash.
428+
Delay = 0; // Treat this as more likely unrelated to the previous crash.
429+
Delay = std::min(std::max(Delay, MinDelaySeconds), MaxDelaySeconds);
428430

429-
size_t Delay = std::min(size_t(20), size_t(Diff.count()*2 + 1));
430431
LOG_WARN_FUNC("disabling semantic editor for " << Delay << " seconds");
431432
SemanticEditorDelaySecondsNum = Delay;
432433

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,9 @@ handleSemanticRequest(RequestDict Req,
720720

721721
llvm::SmallString<64> ErrBuf;
722722

723+
if (isSemanticEditorDisabled())
724+
return Rec(createErrorRequestFailed("semantic editor is disabled"));
725+
723726
if (ReqUID == RequestCodeComplete) {
724727
std::unique_ptr<llvm::MemoryBuffer>
725728
InputBuf = getInputBufForRequest(SourceFile, SourceText, ErrBuf);
@@ -767,9 +770,6 @@ handleSemanticRequest(RequestDict Req,
767770
return Rec(indexSource(*SourceFile, Args, Hash));
768771
}
769772

770-
if (isSemanticEditorDisabled())
771-
return Rec(createErrorRequestFailed("semantic editor is disabled"));
772-
773773
if (ReqUID == RequestCursorInfo) {
774774
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
775775

0 commit comments

Comments
 (0)