2018-02-08 15:37:35 +08:00
|
|
|
//===--- TUScheduler.cpp -----------------------------------------*-C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// For each file, managed by TUScheduler, we create a single ASTWorker that
|
|
|
|
// manages an AST for that file. All operations that modify or read the AST are
|
|
|
|
// run on a separate dedicated thread asynchronously in FIFO order.
|
|
|
|
//
|
|
|
|
// We start processing each update immediately after we receive it. If two or
|
|
|
|
// more updates come subsequently without reads in-between, we attempt to drop
|
|
|
|
// an older one to not waste time building the ASTs we don't need.
|
|
|
|
//
|
|
|
|
// The processing thread of the ASTWorker is also responsible for building the
|
|
|
|
// preamble. However, unlike AST, the same preamble can be read concurrently, so
|
|
|
|
// we run each of async preamble reads on its own thread.
|
|
|
|
//
|
|
|
|
// To limit the concurrent load that clangd produces we mantain a semaphore that
|
|
|
|
// keeps more than a fixed number of threads from running concurrently.
|
|
|
|
//
|
|
|
|
// Rationale for cancelling updates.
|
|
|
|
// LSP clients can send updates to clangd on each keystroke. Some files take
|
|
|
|
// significant time to parse (e.g. a few seconds) and clangd can get starved by
|
|
|
|
// the updates to those files. Therefore we try to process only the last update,
|
|
|
|
// if possible.
|
|
|
|
// Our current strategy to do that is the following:
|
|
|
|
// - For each update we immediately schedule rebuild of the AST.
|
|
|
|
// - Rebuild of the AST checks if it was cancelled before doing any actual work.
|
|
|
|
// If it was, it does not do an actual rebuild, only reports llvm::None to the
|
|
|
|
// callback
|
|
|
|
// - When adding an update, we cancel the last update in the queue if it didn't
|
|
|
|
// have any reads.
|
|
|
|
// There is probably a optimal ways to do that. One approach we might take is
|
|
|
|
// the following:
|
|
|
|
// - For each update we remember the pending inputs, but delay rebuild of the
|
|
|
|
// AST for some timeout.
|
|
|
|
// - If subsequent updates come before rebuild was started, we replace the
|
|
|
|
// pending inputs and reset the timer.
|
|
|
|
// - If any reads of the AST are scheduled, we start building the AST
|
|
|
|
// immediately.
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "TUScheduler.h"
|
2018-02-08 15:37:35 +08:00
|
|
|
#include "Logger.h"
|
2018-02-19 17:56:28 +08:00
|
|
|
#include "Trace.h"
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "clang/Frontend/PCHContainerOperations.h"
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2018-02-19 17:56:28 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
#include <algorithm>
|
2018-02-08 15:37:35 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
2018-02-09 18:17:23 +08:00
|
|
|
#include <thread>
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2018-03-02 16:56:37 +08:00
|
|
|
using std::chrono::steady_clock;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ASTWorker;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:25:26 +08:00
|
|
|
static clang::clangd::Key<std::string> kFileBeingProcessed;
|
2018-08-09 17:05:45 +08:00
|
|
|
|
|
|
|
llvm::Optional<llvm::StringRef> TUScheduler::getFileBeingProcessedInContext() {
|
|
|
|
if (auto *File = Context::current().get(kFileBeingProcessed))
|
|
|
|
return StringRef(*File);
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// An LRU cache of idle ASTs.
|
|
|
|
/// Because we want to limit the overall number of these we retain, the cache
|
|
|
|
/// owns ASTs (and may evict them) while their workers are idle.
|
|
|
|
/// Workers borrow ASTs when active, and return them when done.
|
|
|
|
class TUScheduler::ASTCache {
|
|
|
|
public:
|
|
|
|
using Key = const ASTWorker *;
|
|
|
|
|
|
|
|
ASTCache(unsigned MaxRetainedASTs) : MaxRetainedASTs(MaxRetainedASTs) {}
|
|
|
|
|
|
|
|
/// Returns result of getUsedBytes() for the AST cached by \p K.
|
|
|
|
/// If no AST is cached, 0 is returned.
|
2018-06-01 22:44:57 +08:00
|
|
|
std::size_t getUsedBytes(Key K) {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mut);
|
|
|
|
auto It = findByKey(K);
|
|
|
|
if (It == LRU.end() || !It->second)
|
|
|
|
return 0;
|
|
|
|
return It->second->getUsedBytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Store the value in the pool, possibly removing the last used AST.
|
|
|
|
/// The value should not be in the pool when this function is called.
|
|
|
|
void put(Key K, std::unique_ptr<ParsedAST> V) {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mut);
|
|
|
|
assert(findByKey(K) == LRU.end());
|
|
|
|
|
|
|
|
LRU.insert(LRU.begin(), {K, std::move(V)});
|
|
|
|
if (LRU.size() <= MaxRetainedASTs)
|
|
|
|
return;
|
|
|
|
// We're past the limit, remove the last element.
|
|
|
|
std::unique_ptr<ParsedAST> ForCleanup = std::move(LRU.back().second);
|
|
|
|
LRU.pop_back();
|
|
|
|
// Run the expensive destructor outside the lock.
|
|
|
|
Lock.unlock();
|
|
|
|
ForCleanup.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the cached value for \p K, or llvm::None if the value is not in
|
|
|
|
/// the cache anymore. If nullptr was cached for \p K, this function will
|
|
|
|
/// return a null unique_ptr wrapped into an optional.
|
|
|
|
llvm::Optional<std::unique_ptr<ParsedAST>> take(Key K) {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mut);
|
|
|
|
auto Existing = findByKey(K);
|
|
|
|
if (Existing == LRU.end())
|
|
|
|
return llvm::None;
|
|
|
|
std::unique_ptr<ParsedAST> V = std::move(Existing->second);
|
|
|
|
LRU.erase(Existing);
|
2018-06-01 20:03:16 +08:00
|
|
|
// GCC 4.8 fails to compile `return V;`, as it tries to call the copy
|
|
|
|
// constructor of unique_ptr, so we call the move ctor explicitly to avoid
|
|
|
|
// this miscompile.
|
|
|
|
return llvm::Optional<std::unique_ptr<ParsedAST>>(std::move(V));
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using KVPair = std::pair<Key, std::unique_ptr<ParsedAST>>;
|
|
|
|
|
|
|
|
std::vector<KVPair>::iterator findByKey(Key K) {
|
|
|
|
return std::find_if(LRU.begin(), LRU.end(),
|
|
|
|
[K](const KVPair &P) { return P.first == K; });
|
|
|
|
}
|
|
|
|
|
|
|
|
std::mutex Mut;
|
|
|
|
unsigned MaxRetainedASTs;
|
|
|
|
/// Items sorted in LRU order, i.e. first item is the most recently accessed
|
|
|
|
/// one.
|
|
|
|
std::vector<KVPair> LRU; /* GUARDED_BY(Mut) */
|
|
|
|
};
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
namespace {
|
|
|
|
class ASTWorkerHandle;
|
|
|
|
|
|
|
|
/// Owns one instance of the AST, schedules updates and reads of it.
|
|
|
|
/// Also responsible for building and providing access to the preamble.
|
|
|
|
/// Each ASTWorker processes the async requests sent to it on a separate
|
|
|
|
/// dedicated thread.
|
|
|
|
/// The ASTWorker that manages the AST is shared by both the processing thread
|
|
|
|
/// and the TUScheduler. The TUScheduler should discard an ASTWorker when
|
|
|
|
/// remove() is called, but its thread may be busy and we don't want to block.
|
|
|
|
/// So the workers are accessed via an ASTWorkerHandle. Destroying the handle
|
|
|
|
/// signals the worker to exit its run loop and gives up shared ownership of the
|
|
|
|
/// worker.
|
|
|
|
class ASTWorker {
|
|
|
|
friend class ASTWorkerHandle;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
ASTWorker(PathRef FileName, TUScheduler::ASTCache &LRUCache,
|
|
|
|
Semaphore &Barrier, bool RunSync,
|
|
|
|
steady_clock::duration UpdateDebounce,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
bool StorePreamblesInMemory, ParsingCallbacks &Callbacks);
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// Create a new ASTWorker and return a handle to it.
|
|
|
|
/// The processing thread is spawned using \p Tasks. However, when \p Tasks
|
|
|
|
/// is null, all requests will be processed on the calling thread
|
|
|
|
/// synchronously instead. \p Barrier is acquired when processing each
|
|
|
|
/// request, it is be used to limit the number of actively running threads.
|
2018-07-26 20:05:31 +08:00
|
|
|
static ASTWorkerHandle create(PathRef FileName,
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
TUScheduler::ASTCache &IdleASTs,
|
|
|
|
AsyncTaskRunner *Tasks, Semaphore &Barrier,
|
|
|
|
steady_clock::duration UpdateDebounce,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
bool StorePreamblesInMemory,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
ParsingCallbacks &Callbacks);
|
2018-02-08 15:37:35 +08:00
|
|
|
~ASTWorker();
|
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
void update(ParseInputs Inputs, WantDiagnostics,
|
2018-07-04 04:59:33 +08:00
|
|
|
llvm::unique_function<void(std::vector<Diag>)> OnUpdated);
|
|
|
|
void
|
|
|
|
runWithAST(llvm::StringRef Name,
|
|
|
|
llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action);
|
2018-02-13 16:59:23 +08:00
|
|
|
bool blockUntilIdle(Deadline Timeout) const;
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
std::shared_ptr<const PreambleData> getPossiblyStalePreamble() const;
|
2018-07-09 18:45:33 +08:00
|
|
|
/// Wait for the first build of preamble to finish. Preamble itself can be
|
|
|
|
/// accessed via getPossibleStalePreamble(). Note that this function will
|
|
|
|
/// return after an unsuccessful build of the preamble too, i.e. result of
|
|
|
|
/// getPossiblyStalePreamble() can be null even after this function returns.
|
|
|
|
void waitForFirstPreamble() const;
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
std::size_t getUsedBytes() const;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
bool isASTCached() const;
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Must be called exactly once on processing thread. Will return after
|
|
|
|
// stop() is called on a separate thread and all pending requests are
|
|
|
|
// processed.
|
|
|
|
void run();
|
|
|
|
/// Signal that run() should finish processing pending requests and exit.
|
|
|
|
void stop();
|
|
|
|
/// Adds a new task to the end of the request queue.
|
2018-07-04 04:59:33 +08:00
|
|
|
void startTask(llvm::StringRef Name, llvm::unique_function<void()> Task,
|
2018-02-22 21:11:12 +08:00
|
|
|
llvm::Optional<WantDiagnostics> UpdateType);
|
2018-03-02 16:56:37 +08:00
|
|
|
/// Determines the next action to perform.
|
|
|
|
/// All actions that should never run are disarded.
|
|
|
|
/// Returns a deadline for the next action. If it's expired, run now.
|
|
|
|
/// scheduleLocked() is called again at the deadline, or if requests arrive.
|
|
|
|
Deadline scheduleLocked();
|
2018-02-22 21:11:12 +08:00
|
|
|
/// Should the first task in the queue be skipped instead of run?
|
|
|
|
bool shouldSkipHeadLocked() const;
|
2018-02-08 15:37:35 +08:00
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
struct Request {
|
2018-07-04 04:59:33 +08:00
|
|
|
llvm::unique_function<void()> Action;
|
2018-02-19 17:56:28 +08:00
|
|
|
std::string Name;
|
2018-03-02 16:56:37 +08:00
|
|
|
steady_clock::time_point AddTime;
|
2018-02-19 17:56:28 +08:00
|
|
|
Context Ctx;
|
2018-02-22 21:11:12 +08:00
|
|
|
llvm::Optional<WantDiagnostics> UpdateType;
|
2018-02-19 17:56:28 +08:00
|
|
|
};
|
2018-02-08 15:37:35 +08:00
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Handles retention of ASTs.
|
|
|
|
TUScheduler::ASTCache &IdleASTs;
|
2018-02-08 15:37:35 +08:00
|
|
|
const bool RunSync;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Time to wait after an update to see whether another update obsoletes it.
|
2018-03-02 16:56:37 +08:00
|
|
|
const steady_clock::duration UpdateDebounce;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// File that ASTWorker is reponsible for.
|
|
|
|
const Path FileName;
|
|
|
|
/// Whether to keep the built preambles in memory or on disk.
|
|
|
|
const bool StorePreambleInMemory;
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
/// Callback, invoked when preamble or main file AST is built.
|
|
|
|
ParsingCallbacks &Callbacks;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Helper class required to build the ASTs.
|
|
|
|
const std::shared_ptr<PCHContainerOperations> PCHs;
|
2018-03-02 16:56:37 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
Semaphore &Barrier;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Inputs, corresponding to the current state of AST.
|
2018-02-08 15:37:35 +08:00
|
|
|
ParseInputs FileInputs;
|
2018-07-31 19:47:52 +08:00
|
|
|
/// Whether the diagnostics for the current FileInputs were reported to the
|
|
|
|
/// users before.
|
|
|
|
bool DiagsWereReported = false;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Size of the last AST
|
|
|
|
/// Guards members used by both TUScheduler and the worker thread.
|
2018-02-08 15:37:35 +08:00
|
|
|
mutable std::mutex Mutex;
|
2018-02-09 18:17:23 +08:00
|
|
|
std::shared_ptr<const PreambleData> LastBuiltPreamble; /* GUARDED_BY(Mutex) */
|
2018-07-09 18:45:33 +08:00
|
|
|
/// Becomes ready when the first preamble build finishes.
|
|
|
|
Notification PreambleWasBuilt;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
/// Set to true to signal run() to finish processing.
|
2018-03-12 23:28:22 +08:00
|
|
|
bool Done; /* GUARDED_BY(Mutex) */
|
|
|
|
std::deque<Request> Requests; /* GUARDED_BY(Mutex) */
|
2018-02-13 16:59:23 +08:00
|
|
|
mutable std::condition_variable RequestsCV;
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// A smart-pointer-like class that points to an active ASTWorker.
|
|
|
|
/// In destructor, signals to the underlying ASTWorker that no new requests will
|
|
|
|
/// be sent and the processing loop may exit (after running all pending
|
|
|
|
/// requests).
|
|
|
|
class ASTWorkerHandle {
|
|
|
|
friend class ASTWorker;
|
|
|
|
ASTWorkerHandle(std::shared_ptr<ASTWorker> Worker)
|
|
|
|
: Worker(std::move(Worker)) {
|
|
|
|
assert(this->Worker);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ASTWorkerHandle(const ASTWorkerHandle &) = delete;
|
|
|
|
ASTWorkerHandle &operator=(const ASTWorkerHandle &) = delete;
|
|
|
|
ASTWorkerHandle(ASTWorkerHandle &&) = default;
|
|
|
|
ASTWorkerHandle &operator=(ASTWorkerHandle &&) = default;
|
|
|
|
|
|
|
|
~ASTWorkerHandle() {
|
|
|
|
if (Worker)
|
|
|
|
Worker->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTWorker &operator*() {
|
|
|
|
assert(Worker && "Handle was moved from");
|
|
|
|
return *Worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTWorker *operator->() {
|
|
|
|
assert(Worker && "Handle was moved from");
|
|
|
|
return Worker.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an owning reference to the underlying ASTWorker that can outlive
|
|
|
|
/// the ASTWorkerHandle. However, no new requests to an active ASTWorker can
|
|
|
|
/// be schedule via the returned reference, i.e. only reads of the preamble
|
|
|
|
/// are possible.
|
|
|
|
std::shared_ptr<const ASTWorker> lock() { return Worker; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<ASTWorker> Worker;
|
|
|
|
};
|
|
|
|
|
2018-07-26 20:05:31 +08:00
|
|
|
ASTWorkerHandle ASTWorker::create(PathRef FileName,
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
TUScheduler::ASTCache &IdleASTs,
|
|
|
|
AsyncTaskRunner *Tasks, Semaphore &Barrier,
|
|
|
|
steady_clock::duration UpdateDebounce,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
bool StorePreamblesInMemory,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
ParsingCallbacks &Callbacks) {
|
2018-03-02 16:56:37 +08:00
|
|
|
std::shared_ptr<ASTWorker> Worker(new ASTWorker(
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
FileName, IdleASTs, Barrier, /*RunSync=*/!Tasks, UpdateDebounce,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
std::move(PCHs), StorePreamblesInMemory, Callbacks));
|
2018-02-08 15:37:35 +08:00
|
|
|
if (Tasks)
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
Tasks->runAsync("worker:" + llvm::sys::path::filename(FileName),
|
2018-02-19 17:56:28 +08:00
|
|
|
[Worker]() { Worker->run(); });
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
return ASTWorkerHandle(std::move(Worker));
|
|
|
|
}
|
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
ASTWorker::ASTWorker(PathRef FileName, TUScheduler::ASTCache &LRUCache,
|
|
|
|
Semaphore &Barrier, bool RunSync,
|
|
|
|
steady_clock::duration UpdateDebounce,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
bool StorePreamblesInMemory, ParsingCallbacks &Callbacks)
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
: IdleASTs(LRUCache), RunSync(RunSync), UpdateDebounce(UpdateDebounce),
|
|
|
|
FileName(FileName), StorePreambleInMemory(StorePreamblesInMemory),
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
Callbacks(Callbacks), PCHs(std::move(PCHs)), Barrier(Barrier),
|
|
|
|
Done(false) {}
|
2018-02-08 15:37:35 +08:00
|
|
|
|
|
|
|
ASTWorker::~ASTWorker() {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Make sure we remove the cached AST, if any.
|
|
|
|
IdleASTs.take(this);
|
2018-02-08 15:37:35 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(Done && "handle was not destroyed");
|
|
|
|
assert(Requests.empty() && "unprocessed requests when destroying ASTWorker");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-04 04:59:33 +08:00
|
|
|
void ASTWorker::update(
|
|
|
|
ParseInputs Inputs, WantDiagnostics WantDiags,
|
|
|
|
llvm::unique_function<void(std::vector<Diag>)> OnUpdated) {
|
2018-02-22 21:11:12 +08:00
|
|
|
auto Task = [=](decltype(OnUpdated) OnUpdated) mutable {
|
2018-07-26 17:21:07 +08:00
|
|
|
// Will be used to check if we can avoid rebuilding the AST.
|
|
|
|
bool InputsAreTheSame =
|
|
|
|
std::tie(FileInputs.CompileCommand, FileInputs.Contents) ==
|
|
|
|
std::tie(Inputs.CompileCommand, Inputs.Contents);
|
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
tooling::CompileCommand OldCommand = std::move(FileInputs.CompileCommand);
|
2018-07-31 19:47:52 +08:00
|
|
|
bool PrevDiagsWereReported = DiagsWereReported;
|
2018-02-08 15:37:35 +08:00
|
|
|
FileInputs = Inputs;
|
2018-07-31 19:47:52 +08:00
|
|
|
DiagsWereReported = false;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
log("Updating file {0} with command [{1}] {2}", FileName,
|
|
|
|
Inputs.CompileCommand.Directory,
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
llvm::join(Inputs.CompileCommand.CommandLine, " "));
|
|
|
|
// Rebuild the preamble and the AST.
|
2018-06-28 19:04:45 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> Invocation =
|
|
|
|
buildCompilerInvocation(Inputs);
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
if (!Invocation) {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
elog("Could not build CompilerInvocation for file {0}", FileName);
|
2018-07-30 23:30:45 +08:00
|
|
|
// Remove the old AST if it's still in cache.
|
|
|
|
IdleASTs.take(this);
|
2018-07-09 18:45:33 +08:00
|
|
|
// Make sure anyone waiting for the preamble gets notified it could not
|
|
|
|
// be built.
|
|
|
|
PreambleWasBuilt.notify();
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-02-09 18:17:23 +08:00
|
|
|
|
2018-07-26 17:21:07 +08:00
|
|
|
std::shared_ptr<const PreambleData> OldPreamble =
|
|
|
|
getPossiblyStalePreamble();
|
|
|
|
std::shared_ptr<const PreambleData> NewPreamble =
|
|
|
|
buildPreamble(FileName, *Invocation, OldPreamble, OldCommand, Inputs,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
PCHs, StorePreambleInMemory,
|
|
|
|
[this](PathRef Path, ASTContext &Ctx,
|
|
|
|
std::shared_ptr<clang::Preprocessor> PP) {
|
|
|
|
Callbacks.onPreambleAST(FileName, Ctx, std::move(PP));
|
|
|
|
});
|
2018-07-26 17:21:07 +08:00
|
|
|
|
|
|
|
bool CanReuseAST = InputsAreTheSame && (OldPreamble == NewPreamble);
|
2018-02-09 18:17:23 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2018-08-17 16:15:22 +08:00
|
|
|
LastBuiltPreamble = NewPreamble;
|
2018-02-09 18:17:23 +08:00
|
|
|
}
|
2018-07-26 17:21:07 +08:00
|
|
|
// Before doing the expensive AST reparse, we want to release our reference
|
|
|
|
// to the old preamble, so it can be freed if there are no other references
|
|
|
|
// to it.
|
|
|
|
OldPreamble.reset();
|
2018-07-09 18:45:33 +08:00
|
|
|
PreambleWasBuilt.notify();
|
|
|
|
|
2018-07-31 19:47:52 +08:00
|
|
|
if (!CanReuseAST) {
|
|
|
|
IdleASTs.take(this); // Remove the old AST if it's still in cache.
|
|
|
|
} else {
|
|
|
|
// Since we don't need to rebuild the AST, we might've already reported
|
|
|
|
// the diagnostics for it.
|
|
|
|
if (PrevDiagsWereReported) {
|
|
|
|
DiagsWereReported = true;
|
|
|
|
// Take a shortcut and don't report the diagnostics, since they should
|
|
|
|
// not changed. All the clients should handle the lack of OnUpdated()
|
|
|
|
// call anyway to handle empty result from buildAST.
|
|
|
|
// FIXME(ibiryukov): the AST could actually change if non-preamble
|
|
|
|
// includes changed, but we choose to ignore it.
|
|
|
|
// FIXME(ibiryukov): should we refresh the cache in IdleASTs for the
|
|
|
|
// current file at this point?
|
|
|
|
log("Skipping rebuild of the AST for {0}, inputs are the same.",
|
|
|
|
FileName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 21:45:37 +08:00
|
|
|
// We only need to build the AST if diagnostics were requested.
|
|
|
|
if (WantDiags == WantDiagnostics::No)
|
|
|
|
return;
|
|
|
|
|
2018-07-31 19:47:52 +08:00
|
|
|
// Get the AST for diagnostics.
|
|
|
|
llvm::Optional<std::unique_ptr<ParsedAST>> AST = IdleASTs.take(this);
|
|
|
|
if (!AST) {
|
|
|
|
llvm::Optional<ParsedAST> NewAST =
|
|
|
|
buildAST(FileName, std::move(Invocation), Inputs, NewPreamble, PCHs);
|
|
|
|
AST = NewAST ? llvm::make_unique<ParsedAST>(std::move(*NewAST)) : nullptr;
|
2018-07-26 17:21:07 +08:00
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
// We want to report the diagnostics even if this update was cancelled.
|
|
|
|
// It seems more useful than making the clients wait indefinitely if they
|
|
|
|
// spam us with updates.
|
2018-07-31 19:47:52 +08:00
|
|
|
// Note *AST can be still be null if buildAST fails.
|
2018-07-31 21:45:37 +08:00
|
|
|
if (*AST) {
|
2018-07-31 19:47:52 +08:00
|
|
|
OnUpdated((*AST)->getDiagnostics());
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
Callbacks.onMainAST(FileName, **AST);
|
2018-07-31 19:47:52 +08:00
|
|
|
DiagsWereReported = true;
|
|
|
|
}
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Stash the AST in the cache for further use.
|
2018-07-31 19:47:52 +08:00
|
|
|
IdleASTs.put(this, std::move(*AST));
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
|
|
|
|
2018-02-23 15:54:17 +08:00
|
|
|
startTask("Update", Bind(Task, std::move(OnUpdated)), WantDiags);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTWorker::runWithAST(
|
2018-02-19 17:56:28 +08:00
|
|
|
llvm::StringRef Name,
|
2018-07-04 04:59:33 +08:00
|
|
|
llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action) {
|
2018-02-08 15:37:35 +08:00
|
|
|
auto Task = [=](decltype(Action) Action) {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
llvm::Optional<std::unique_ptr<ParsedAST>> AST = IdleASTs.take(this);
|
|
|
|
if (!AST) {
|
2018-06-28 19:04:45 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> Invocation =
|
|
|
|
buildCompilerInvocation(FileInputs);
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Try rebuilding the AST.
|
|
|
|
llvm::Optional<ParsedAST> NewAST =
|
|
|
|
Invocation
|
|
|
|
? buildAST(FileName,
|
|
|
|
llvm::make_unique<CompilerInvocation>(*Invocation),
|
|
|
|
FileInputs, getPossiblyStalePreamble(), PCHs)
|
|
|
|
: llvm::None;
|
|
|
|
AST = NewAST ? llvm::make_unique<ParsedAST>(std::move(*NewAST)) : nullptr;
|
2018-02-09 18:17:23 +08:00
|
|
|
}
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Make sure we put the AST back into the LRU cache.
|
|
|
|
auto _ = llvm::make_scope_exit(
|
|
|
|
[&AST, this]() { IdleASTs.put(this, std::move(*AST)); });
|
|
|
|
// Run the user-provided action.
|
|
|
|
if (!*AST)
|
|
|
|
return Action(llvm::make_error<llvm::StringError>(
|
|
|
|
"invalid AST", llvm::errc::invalid_argument));
|
|
|
|
Action(InputsAndAST{FileInputs, **AST});
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
2018-02-23 15:54:17 +08:00
|
|
|
startTask(Name, Bind(Task, std::move(Action)),
|
2018-02-22 21:11:12 +08:00
|
|
|
/*UpdateType=*/llvm::None);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const PreambleData>
|
|
|
|
ASTWorker::getPossiblyStalePreamble() const {
|
2018-02-09 18:17:23 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
return LastBuiltPreamble;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
2018-07-09 18:45:33 +08:00
|
|
|
void ASTWorker::waitForFirstPreamble() const {
|
|
|
|
PreambleWasBuilt.wait();
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
std::size_t ASTWorker::getUsedBytes() const {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Note that we don't report the size of ASTs currently used for processing
|
|
|
|
// the in-flight requests. We used this information for debugging purposes
|
|
|
|
// only, so this should be fine.
|
|
|
|
std::size_t Result = IdleASTs.getUsedBytes(this);
|
|
|
|
if (auto Preamble = getPossiblyStalePreamble())
|
|
|
|
Result += Preamble->Preamble.getSize();
|
|
|
|
return Result;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
bool ASTWorker::isASTCached() const { return IdleASTs.getUsedBytes(this) != 0; }
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
void ASTWorker::stop() {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(!Done && "stop() called twice");
|
|
|
|
Done = true;
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 04:59:33 +08:00
|
|
|
void ASTWorker::startTask(llvm::StringRef Name,
|
|
|
|
llvm::unique_function<void()> Task,
|
2018-02-22 21:11:12 +08:00
|
|
|
llvm::Optional<WantDiagnostics> UpdateType) {
|
2018-02-08 15:37:35 +08:00
|
|
|
if (RunSync) {
|
|
|
|
assert(!Done && "running a task after stop()");
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
trace::Span Tracer(Name + ":" + llvm::sys::path::filename(FileName));
|
2018-02-08 15:37:35 +08:00
|
|
|
Task();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
assert(!Done && "running a task after stop()");
|
2018-08-09 17:05:45 +08:00
|
|
|
Requests.push_back(
|
|
|
|
{std::move(Task), Name, steady_clock::now(),
|
|
|
|
Context::current().derive(kFileBeingProcessed, FileName), UpdateType});
|
2018-02-22 21:11:12 +08:00
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTWorker::run() {
|
|
|
|
while (true) {
|
2018-02-19 17:56:28 +08:00
|
|
|
Request Req;
|
2018-02-08 15:37:35 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
2018-03-02 16:56:37 +08:00
|
|
|
for (auto Wait = scheduleLocked(); !Wait.expired();
|
|
|
|
Wait = scheduleLocked()) {
|
|
|
|
if (Done) {
|
|
|
|
if (Requests.empty())
|
|
|
|
return;
|
|
|
|
else // Even though Done is set, finish pending requests.
|
|
|
|
break; // However, skip delays to shutdown fast.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracing: we have a next request, attribute this sleep to it.
|
|
|
|
Optional<WithContext> Ctx;
|
|
|
|
Optional<trace::Span> Tracer;
|
|
|
|
if (!Requests.empty()) {
|
|
|
|
Ctx.emplace(Requests.front().Ctx.clone());
|
|
|
|
Tracer.emplace("Debounce");
|
|
|
|
SPAN_ATTACH(*Tracer, "next_request", Requests.front().Name);
|
|
|
|
if (!(Wait == Deadline::infinity()))
|
|
|
|
SPAN_ATTACH(*Tracer, "sleep_ms",
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
Wait.time() - steady_clock::now())
|
|
|
|
.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(Lock, RequestsCV, Wait);
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
Req = std::move(Requests.front());
|
2018-02-13 16:59:23 +08:00
|
|
|
// Leave it on the queue for now, so waiters don't see an empty queue.
|
2018-02-08 15:37:35 +08:00
|
|
|
} // unlock Mutex
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<Semaphore> BarrierLock(Barrier);
|
|
|
|
WithContext Guard(std::move(Req.Ctx));
|
|
|
|
trace::Span Tracer(Req.Name);
|
|
|
|
Req.Action();
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2018-02-22 21:11:12 +08:00
|
|
|
Requests.pop_front();
|
2018-02-13 16:59:23 +08:00
|
|
|
}
|
|
|
|
RequestsCV.notify_all();
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
|
2018-03-02 16:56:37 +08:00
|
|
|
Deadline ASTWorker::scheduleLocked() {
|
|
|
|
if (Requests.empty())
|
|
|
|
return Deadline::infinity(); // Wait for new requests.
|
|
|
|
while (shouldSkipHeadLocked())
|
|
|
|
Requests.pop_front();
|
|
|
|
assert(!Requests.empty() && "skipped the whole queue");
|
|
|
|
// Some updates aren't dead yet, but never end up being used.
|
|
|
|
// e.g. the first keystroke is live until obsoleted by the second.
|
|
|
|
// We debounce "maybe-unused" writes, sleeping 500ms in case they become dead.
|
|
|
|
// But don't delay reads (including updates where diagnostics are needed).
|
|
|
|
for (const auto &R : Requests)
|
|
|
|
if (R.UpdateType == None || R.UpdateType == WantDiagnostics::Yes)
|
|
|
|
return Deadline::zero();
|
|
|
|
// Front request needs to be debounced, so determine when we're ready.
|
|
|
|
Deadline D(Requests.front().AddTime + UpdateDebounce);
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
// Returns true if Requests.front() is a dead update that can be skipped.
|
|
|
|
bool ASTWorker::shouldSkipHeadLocked() const {
|
|
|
|
assert(!Requests.empty());
|
|
|
|
auto Next = Requests.begin();
|
|
|
|
auto UpdateType = Next->UpdateType;
|
|
|
|
if (!UpdateType) // Only skip updates.
|
|
|
|
return false;
|
|
|
|
++Next;
|
|
|
|
// An update is live if its AST might still be read.
|
|
|
|
// That is, if it's not immediately followed by another update.
|
|
|
|
if (Next == Requests.end() || !Next->UpdateType)
|
|
|
|
return false;
|
|
|
|
// The other way an update can be live is if its diagnostics might be used.
|
|
|
|
switch (*UpdateType) {
|
|
|
|
case WantDiagnostics::Yes:
|
|
|
|
return false; // Always used.
|
|
|
|
case WantDiagnostics::No:
|
|
|
|
return true; // Always dead.
|
|
|
|
case WantDiagnostics::Auto:
|
|
|
|
// Used unless followed by an update that generates diagnostics.
|
|
|
|
for (; Next != Requests.end(); ++Next)
|
|
|
|
if (Next->UpdateType == WantDiagnostics::Yes ||
|
|
|
|
Next->UpdateType == WantDiagnostics::Auto)
|
|
|
|
return true; // Prefer later diagnostics.
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-23 00:12:27 +08:00
|
|
|
llvm_unreachable("Unknown WantDiagnostics");
|
2018-02-22 21:11:12 +08:00
|
|
|
}
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
bool ASTWorker::blockUntilIdle(Deadline Timeout) const {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
return wait(Lock, RequestsCV, Timeout, [&] { return Requests.empty(); });
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
} // namespace
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
unsigned getDefaultAsyncThreadsCount() {
|
|
|
|
unsigned HardwareConcurrency = std::thread::hardware_concurrency();
|
|
|
|
// C++ standard says that hardware_concurrency()
|
|
|
|
// may return 0, fallback to 1 worker thread in
|
|
|
|
// that case.
|
|
|
|
if (HardwareConcurrency == 0)
|
|
|
|
return 1;
|
|
|
|
return HardwareConcurrency;
|
|
|
|
}
|
|
|
|
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
ParsingCallbacks &noopParsingCallbacks() {
|
|
|
|
static ParsingCallbacks *Instance = new ParsingCallbacks;
|
|
|
|
return *Instance;
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
struct TUScheduler::FileData {
|
|
|
|
/// Latest inputs, passed to TUScheduler::update().
|
2018-03-15 01:46:52 +08:00
|
|
|
std::string Contents;
|
|
|
|
tooling::CompileCommand Command;
|
2018-02-08 15:37:35 +08:00
|
|
|
ASTWorkerHandle Worker;
|
|
|
|
};
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
TUScheduler::TUScheduler(unsigned AsyncThreadsCount,
|
|
|
|
bool StorePreamblesInMemory,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
ParsingCallbacks &Callbacks,
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
std::chrono::steady_clock::duration UpdateDebounce,
|
|
|
|
ASTRetentionPolicy RetentionPolicy)
|
2018-02-08 15:37:35 +08:00
|
|
|
: StorePreamblesInMemory(StorePreamblesInMemory),
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
PCHOps(std::make_shared<PCHContainerOperations>()), Callbacks(Callbacks),
|
|
|
|
Barrier(AsyncThreadsCount),
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
IdleASTs(llvm::make_unique<ASTCache>(RetentionPolicy.MaxRetainedASTs)),
|
2018-03-02 16:56:37 +08:00
|
|
|
UpdateDebounce(UpdateDebounce) {
|
2018-02-13 16:59:23 +08:00
|
|
|
if (0 < AsyncThreadsCount) {
|
|
|
|
PreambleTasks.emplace();
|
|
|
|
WorkerThreads.emplace();
|
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
TUScheduler::~TUScheduler() {
|
|
|
|
// Notify all workers that they need to stop.
|
|
|
|
Files.clear();
|
|
|
|
|
|
|
|
// Wait for all in-flight tasks to finish.
|
2018-02-13 16:59:23 +08:00
|
|
|
if (PreambleTasks)
|
|
|
|
PreambleTasks->wait();
|
|
|
|
if (WorkerThreads)
|
|
|
|
WorkerThreads->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TUScheduler::blockUntilIdle(Deadline D) const {
|
|
|
|
for (auto &File : Files)
|
|
|
|
if (!File.getValue()->Worker->blockUntilIdle(D))
|
|
|
|
return false;
|
|
|
|
if (PreambleTasks)
|
|
|
|
if (!PreambleTasks->wait(D))
|
|
|
|
return false;
|
|
|
|
return true;
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-07-04 04:59:33 +08:00
|
|
|
void TUScheduler::update(
|
|
|
|
PathRef File, ParseInputs Inputs, WantDiagnostics WantDiags,
|
|
|
|
llvm::unique_function<void(std::vector<Diag>)> OnUpdated) {
|
2018-02-08 15:37:35 +08:00
|
|
|
std::unique_ptr<FileData> &FD = Files[File];
|
|
|
|
if (!FD) {
|
|
|
|
// Create a new worker to process the AST-related tasks.
|
2018-07-26 20:05:31 +08:00
|
|
|
ASTWorkerHandle Worker = ASTWorker::create(
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
File, *IdleASTs, WorkerThreads ? WorkerThreads.getPointer() : nullptr,
|
[clangd] Add callbacks on parsed AST in addition to parsed preambles
Summary:
Will be used for updating the dynamic index on updates to the open files.
Currently we collect only information coming from the preamble
AST. This has a bunch of limitations:
- Dynamic index misses important information from the body of the
file, e.g. locations of definitions.
- XRefs cannot be collected at all, since we can only obtain full
information for the current file (preamble is parsed with skipped
function bodies, therefore not reliable).
This patch only adds the new callback, actually updates to the index
will be done in a follow-up patch.
Reviewers: hokein
Reviewed By: hokein
Subscribers: kadircet, javed.absar, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D50847
llvm-svn: 340401
2018-08-22 19:39:16 +08:00
|
|
|
Barrier, UpdateDebounce, PCHOps, StorePreamblesInMemory, Callbacks);
|
2018-03-15 01:46:52 +08:00
|
|
|
FD = std::unique_ptr<FileData>(new FileData{
|
|
|
|
Inputs.Contents, Inputs.CompileCommand, std::move(Worker)});
|
2018-02-08 15:37:35 +08:00
|
|
|
} else {
|
2018-03-15 01:46:52 +08:00
|
|
|
FD->Contents = Inputs.Contents;
|
|
|
|
FD->Command = Inputs.CompileCommand;
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
2018-02-22 21:11:12 +08:00
|
|
|
FD->Worker->update(std::move(Inputs), WantDiags, std::move(OnUpdated));
|
2018-02-08 15:37:35 +08:00
|
|
|
}
|
2018-02-07 03:22:40 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
void TUScheduler::remove(PathRef File) {
|
|
|
|
bool Removed = Files.erase(File);
|
|
|
|
if (!Removed)
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
elog("Trying to remove file from TUScheduler that is not tracked: {0}",
|
|
|
|
File);
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TUScheduler::runWithAST(
|
2018-02-19 17:56:28 +08:00
|
|
|
llvm::StringRef Name, PathRef File,
|
2018-07-04 04:59:33 +08:00
|
|
|
llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action) {
|
2018-02-08 15:37:35 +08:00
|
|
|
auto It = Files.find(File);
|
|
|
|
if (It == Files.end()) {
|
2018-01-31 16:51:16 +08:00
|
|
|
Action(llvm::make_error<llvm::StringError>(
|
|
|
|
"trying to get AST for non-added document",
|
|
|
|
llvm::errc::invalid_argument));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
It->second->Worker->runWithAST(Name, std::move(Action));
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TUScheduler::runWithPreamble(
|
2018-02-19 17:56:28 +08:00
|
|
|
llvm::StringRef Name, PathRef File,
|
2018-07-04 04:59:33 +08:00
|
|
|
llvm::unique_function<void(llvm::Expected<InputsAndPreamble>)> Action) {
|
2018-02-08 15:37:35 +08:00
|
|
|
auto It = Files.find(File);
|
|
|
|
if (It == Files.end()) {
|
2018-01-31 16:51:16 +08:00
|
|
|
Action(llvm::make_error<llvm::StringError>(
|
|
|
|
"trying to get preamble for non-added document",
|
|
|
|
llvm::errc::invalid_argument));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
if (!PreambleTasks) {
|
2018-02-19 17:56:28 +08:00
|
|
|
trace::Span Tracer(Name);
|
|
|
|
SPAN_ATTACH(Tracer, "file", File);
|
2018-02-08 15:37:35 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble =
|
|
|
|
It->second->Worker->getPossiblyStalePreamble();
|
2018-03-15 01:46:52 +08:00
|
|
|
Action(InputsAndPreamble{It->second->Contents, It->second->Command,
|
|
|
|
Preamble.get()});
|
2018-02-08 15:37:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
std::shared_ptr<const ASTWorker> Worker = It->second->Worker.lock();
|
2018-03-15 01:46:52 +08:00
|
|
|
auto Task = [Worker, this](std::string Name, std::string File,
|
|
|
|
std::string Contents,
|
|
|
|
tooling::CompileCommand Command, Context Ctx,
|
|
|
|
decltype(Action) Action) mutable {
|
2018-07-09 18:45:33 +08:00
|
|
|
// We don't want to be running preamble actions before the preamble was
|
|
|
|
// built for the first time. This avoids extra work of processing the
|
|
|
|
// preamble headers in parallel multiple times.
|
|
|
|
Worker->waitForFirstPreamble();
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
std::lock_guard<Semaphore> BarrierLock(Barrier);
|
|
|
|
WithContext Guard(std::move(Ctx));
|
2018-02-19 17:56:28 +08:00
|
|
|
trace::Span Tracer(Name);
|
|
|
|
SPAN_ATTACH(Tracer, "file", File);
|
2018-02-08 15:37:35 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble =
|
|
|
|
Worker->getPossiblyStalePreamble();
|
2018-03-15 01:46:52 +08:00
|
|
|
Action(InputsAndPreamble{Contents, Command, Preamble.get()});
|
2018-02-08 15:37:35 +08:00
|
|
|
};
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-08-09 17:05:45 +08:00
|
|
|
PreambleTasks->runAsync(
|
|
|
|
"task:" + llvm::sys::path::filename(File),
|
|
|
|
Bind(Task, std::string(Name), std::string(File), It->second->Contents,
|
|
|
|
It->second->Command,
|
|
|
|
Context::current().derive(kFileBeingProcessed, File),
|
|
|
|
std::move(Action)));
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<Path, std::size_t>>
|
|
|
|
TUScheduler::getUsedBytesPerFile() const {
|
2018-02-08 15:37:35 +08:00
|
|
|
std::vector<std::pair<Path, std::size_t>> Result;
|
|
|
|
Result.reserve(Files.size());
|
|
|
|
for (auto &&PathAndFile : Files)
|
|
|
|
Result.push_back(
|
|
|
|
{PathAndFile.first(), PathAndFile.second->Worker->getUsedBytes()});
|
|
|
|
return Result;
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
2018-02-08 15:37:35 +08:00
|
|
|
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
std::vector<Path> TUScheduler::getFilesWithCachedAST() const {
|
|
|
|
std::vector<Path> Result;
|
|
|
|
for (auto &&PathAndFile : Files) {
|
|
|
|
if (!PathAndFile.second->Worker->isASTCached())
|
|
|
|
continue;
|
|
|
|
Result.push_back(PathAndFile.first());
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|