61
61
#include " llvm/Support/Threading.h"
62
62
#include < algorithm>
63
63
#include < memory>
64
- #include < mutex>
65
64
#include < queue>
66
65
#include < thread>
67
66
@@ -165,7 +164,7 @@ class ASTWorker {
165
164
friend class ASTWorkerHandle ;
166
165
ASTWorker (PathRef FileName, const GlobalCompilationDatabase &CDB,
167
166
TUScheduler::ASTCache &LRUCache, Semaphore &Barrier, bool RunSync,
168
- DebouncePolicy UpdateDebounce, bool StorePreamblesInMemory,
167
+ steady_clock::duration UpdateDebounce, bool StorePreamblesInMemory,
169
168
ParsingCallbacks &Callbacks);
170
169
171
170
public:
@@ -177,7 +176,7 @@ class ASTWorker {
177
176
static ASTWorkerHandle
178
177
create (PathRef FileName, const GlobalCompilationDatabase &CDB,
179
178
TUScheduler::ASTCache &IdleASTs, AsyncTaskRunner *Tasks,
180
- Semaphore &Barrier, DebouncePolicy UpdateDebounce,
179
+ Semaphore &Barrier, steady_clock::duration UpdateDebounce,
181
180
bool StorePreamblesInMemory, ParsingCallbacks &Callbacks);
182
181
~ASTWorker ();
183
182
@@ -243,7 +242,7 @@ class ASTWorker {
243
242
TUScheduler::ASTCache &IdleASTs;
244
243
const bool RunSync;
245
244
// / Time to wait after an update to see whether another update obsoletes it.
246
- const DebouncePolicy UpdateDebounce;
245
+ const steady_clock::duration UpdateDebounce;
247
246
// / File that ASTWorker is responsible for.
248
247
const Path FileName;
249
248
const GlobalCompilationDatabase &CDB;
@@ -264,9 +263,6 @@ class ASTWorker {
264
263
// / be consumed by clients of ASTWorker.
265
264
std::shared_ptr<const ParseInputs> FileInputs; /* GUARDED_BY(Mutex) */
266
265
std::shared_ptr<const PreambleData> LastBuiltPreamble; /* GUARDED_BY(Mutex) */
267
- // / Times of recent AST rebuilds, used for UpdateDebounce computation.
268
- llvm::SmallVector<DebouncePolicy::clock::duration, 8 >
269
- RebuildTimes; /* GUARDED_BY(Mutex) */
270
266
// / Becomes ready when the first preamble build finishes.
271
267
Notification PreambleWasBuilt;
272
268
// / Set to true to signal run() to finish processing.
@@ -330,7 +326,7 @@ class ASTWorkerHandle {
330
326
ASTWorkerHandle
331
327
ASTWorker::create (PathRef FileName, const GlobalCompilationDatabase &CDB,
332
328
TUScheduler::ASTCache &IdleASTs, AsyncTaskRunner *Tasks,
333
- Semaphore &Barrier, DebouncePolicy UpdateDebounce,
329
+ Semaphore &Barrier, steady_clock::duration UpdateDebounce,
334
330
bool StorePreamblesInMemory, ParsingCallbacks &Callbacks) {
335
331
std::shared_ptr<ASTWorker> Worker (
336
332
new ASTWorker (FileName, CDB, IdleASTs, Barrier, /* RunSync=*/ !Tasks,
@@ -344,7 +340,7 @@ ASTWorker::create(PathRef FileName, const GlobalCompilationDatabase &CDB,
344
340
345
341
ASTWorker::ASTWorker (PathRef FileName, const GlobalCompilationDatabase &CDB,
346
342
TUScheduler::ASTCache &LRUCache, Semaphore &Barrier,
347
- bool RunSync, DebouncePolicy UpdateDebounce,
343
+ bool RunSync, steady_clock::duration UpdateDebounce,
348
344
bool StorePreamblesInMemory, ParsingCallbacks &Callbacks)
349
345
: IdleASTs(LRUCache), RunSync(RunSync), UpdateDebounce(UpdateDebounce),
350
346
FileName (FileName), CDB(CDB),
@@ -492,7 +488,6 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags) {
492
488
493
489
// Get the AST for diagnostics.
494
490
llvm::Optional<std::unique_ptr<ParsedAST>> AST = IdleASTs.take (this );
495
- auto RebuildStartTime = DebouncePolicy::clock::now ();
496
491
if (!AST) {
497
492
llvm::Optional<ParsedAST> NewAST =
498
493
buildAST (FileName, std::move (Invocation), CompilerInvocationDiags,
@@ -515,19 +510,6 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags) {
515
510
// spam us with updates.
516
511
// Note *AST can still be null if buildAST fails.
517
512
if (*AST) {
518
- {
519
- // Try to record the AST-build time, to inform future update debouncing.
520
- // This is best-effort only: if the lock is held, don't bother.
521
- auto RebuildDuration = DebouncePolicy::clock::now () - RebuildStartTime;
522
- std::unique_lock<std::mutex> Lock (Mutex, std::try_to_lock);
523
- if (Lock.owns_lock ()) {
524
- // Do not let RebuildTimes grow beyond its small-size (i.e. capacity).
525
- if (RebuildTimes.size () == RebuildTimes.capacity ())
526
- RebuildTimes.erase (RebuildTimes.begin ());
527
- RebuildTimes.push_back (RebuildDuration);
528
- Mutex.unlock ();
529
- }
530
- }
531
513
trace::Span Span (" Running main AST callback" );
532
514
533
515
Callbacks.onMainAST (FileName, **AST, RunPublish);
@@ -768,13 +750,13 @@ Deadline ASTWorker::scheduleLocked() {
768
750
assert (!Requests.empty () && " skipped the whole queue" );
769
751
// Some updates aren't dead yet, but never end up being used.
770
752
// e.g. the first keystroke is live until obsoleted by the second.
771
- // We debounce "maybe-unused" writes, sleeping in case they become dead.
753
+ // We debounce "maybe-unused" writes, sleeping 500ms in case they become dead.
772
754
// But don't delay reads (including updates where diagnostics are needed).
773
755
for (const auto &R : Requests)
774
756
if (R.UpdateType == None || R.UpdateType == WantDiagnostics::Yes)
775
757
return Deadline::zero ();
776
758
// Front request needs to be debounced, so determine when we're ready.
777
- Deadline D (Requests.front ().AddTime + UpdateDebounce. compute (RebuildTimes) );
759
+ Deadline D (Requests.front ().AddTime + UpdateDebounce);
778
760
return D;
779
761
}
780
762
@@ -1054,33 +1036,5 @@ std::vector<Path> TUScheduler::getFilesWithCachedAST() const {
1054
1036
return Result;
1055
1037
}
1056
1038
1057
- DebouncePolicy::clock::duration
1058
- DebouncePolicy::compute (llvm::ArrayRef<clock::duration> History) const {
1059
- assert (Min <= Max && " Invalid policy" );
1060
- if (History.empty ())
1061
- return Max; // Arbitrary.
1062
-
1063
- // Base the result on the median rebuild.
1064
- // nth_element needs a mutable array, take the chance to bound the data size.
1065
- History = History.take_back (15 );
1066
- llvm::SmallVector<clock::duration, 15 > Recent (History.begin (), History.end ());
1067
- auto Median = Recent.begin () + Recent.size () / 2 ;
1068
- std::nth_element (Recent.begin (), Median, Recent.end ());
1069
-
1070
- clock::duration Target =
1071
- std::chrono::duration_cast<clock::duration>(RebuildRatio * *Median);
1072
- if (Target > Max)
1073
- return Max;
1074
- if (Target < Min)
1075
- return Min;
1076
- return Target;
1077
- }
1078
-
1079
- DebouncePolicy DebouncePolicy::fixed (clock::duration T) {
1080
- DebouncePolicy P;
1081
- P.Min = P.Max = T;
1082
- return P;
1083
- }
1084
-
1085
1039
} // namespace clangd
1086
1040
} // namespace clang
0 commit comments