From 7149851ea2b0f3e7af09999cdd060f2f7f182eb2 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 9 Sep 2018 23:46:13 -0700 Subject: [PATCH] Misc --- src/indexer.cc | 2 +- src/message_handler.cc | 4 ++-- src/messages/workspace_didChangeWatchedFiles.cc | 4 ++-- src/pipeline.cc | 12 +++++++++--- src/project.cc | 16 ++++++++++++---- src/project.h | 2 +- src/query.cc | 17 +++++++++++------ 7 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/indexer.cc b/src/indexer.cc index 1acd59c9..b663cbe1 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1236,7 +1236,7 @@ Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs, CI->getLangOpts()->RetainCommentsFromSystemHeaders = true; std::string buf = wfiles->GetContent(file); std::vector> Bufs; - if (buf.size()) { + if (g_config->index.onChange && buf.size()) { // If there is a completion session, reuse its preamble if exists. bool done_remap = false; std::shared_ptr session = diff --git a/src/message_handler.cc b/src/message_handler.cc index ae413292..7a2b2bb2 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -148,8 +148,8 @@ bool FindFileOrFail(DB *db, Project *project, std::optional id, bool indexing; { std::lock_guard lock(project->mutex_); - indexing = project->absolute_path_to_entry_index_.find(absolute_path) != - project->absolute_path_to_entry_index_.end(); + indexing = project->path_to_entry_index.find(absolute_path) != + project->path_to_entry_index.end(); } if (indexing) LOG_S(INFO) << "\"" << absolute_path << "\" is being indexed."; diff --git a/src/messages/workspace_didChangeWatchedFiles.cc b/src/messages/workspace_didChangeWatchedFiles.cc index 8a41616f..301a11ef 100644 --- a/src/messages/workspace_didChangeWatchedFiles.cc +++ b/src/messages/workspace_didChangeWatchedFiles.cc @@ -45,8 +45,8 @@ struct Handler_WorkspaceDidChangeWatchedFiles Project::Entry entry; { std::lock_guard lock(project->mutex_); - auto it = project->absolute_path_to_entry_index_.find(path); - if (it == project->absolute_path_to_entry_index_.end()) + auto it = project->path_to_entry_index.find(path); + if (it == project->path_to_entry_index.end()) continue; entry = project->entries[it->second]; } diff --git a/src/pipeline.cc b/src/pipeline.cc index c477fb90..00b24a14 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -20,6 +20,8 @@ using namespace llvm; #include +#include +#include #include #ifndef _WIN32 #include @@ -87,6 +89,7 @@ struct InMemoryIndexFile { std::string content; IndexFile index; }; +std::shared_mutex g_index_mutex; std::unordered_map g_index; bool CacheInvalid(VFS *vfs, IndexFile *prev, const std::string &path, @@ -135,6 +138,7 @@ std::string GetCachePath(const std::string &source_file) { std::unique_ptr RawCacheLoad(const std::string &path) { if (g_config->cacheDirectory.empty()) { + std::shared_lock lock(g_index_mutex); auto it = g_index.find(path); if (it == g_index.end()) return nullptr; @@ -177,8 +181,8 @@ bool Indexer_Parse(CompletionManager *completion, WorkingFiles *wfiles, Project::Entry entry; { std::lock_guard lock(project->mutex_); - auto it = project->absolute_path_to_entry_index_.find(request.path); - if (it != project->absolute_path_to_entry_index_.end()) + auto it = project->path_to_entry_index.find(request.path); + if (it != project->path_to_entry_index.end()) entry = project->entries[it->second]; else { entry.filename = request.path; @@ -295,6 +299,7 @@ bool Indexer_Parse(CompletionManager *completion, WorkingFiles *wfiles, LOG_IF_S(INFO, loud) << "store index for " << path << " (delta: " << !!prev << ")"; if (g_config->cacheDirectory.empty()) { + std::lock_guard lock(g_index_mutex); auto it = g_index.insert_or_assign( path, InMemoryIndexFile{curr->file_contents, *curr}); std::string().swap(it.first->second.index.file_contents); @@ -309,7 +314,7 @@ bool Indexer_Parse(CompletionManager *completion, WorkingFiles *wfiles, if (entry.id >= 0) { std::lock_guard lock(project->mutex_); for (auto &dep : curr->dependencies) - project->absolute_path_to_entry_index_[dep.first()] = entry.id; + project->path_to_entry_index[dep.first()] = entry.id; } // Build delta update. @@ -536,6 +541,7 @@ std::optional LastWriteTime(const std::string &path) { std::optional LoadIndexedContent(const std::string &path) { if (g_config->cacheDirectory.empty()) { + std::shared_lock lock(g_index_mutex); auto it = g_index.find(path); if (it == g_index.end()) return {}; diff --git a/src/project.cc b/src/project.cc index f5f02bfd..db83659a 100644 --- a/src/project.cc +++ b/src/project.cc @@ -365,13 +365,21 @@ void Project::Load(const std::string &root_directory) { EnsureEndsInSlash(path); LOG_S(INFO) << "angle_include_dir: " << path; } + + // Setup project entries. + std::lock_guard lock(mutex_); + path_to_entry_index.reserve(entries.size()); + for (size_t i = 0; i < entries.size(); ++i) { + entries[i].id = i; + path_to_entry_index[entries[i].filename] = i; + } } void Project::SetFlagsForFile(const std::vector &flags, const std::string &path) { std::lock_guard lock(mutex_); - auto it = absolute_path_to_entry_index_.find(path); - if (it != absolute_path_to_entry_index_.end()) { + auto it = path_to_entry_index.find(path); + if (it != path_to_entry_index.end()) { // The entry already exists in the project, just set the flags. this->entries[it->second].args = flags; } else { @@ -388,8 +396,8 @@ Project::Entry Project::FindCompilationEntryForFile(const std::string &filename) { { std::lock_guard lock(mutex_); - auto it = absolute_path_to_entry_index_.find(filename); - if (it != absolute_path_to_entry_index_.end()) + auto it = path_to_entry_index.find(filename); + if (it != path_to_entry_index.end()) return entries[it->second]; } diff --git a/src/project.h b/src/project.h index 58b24ad5..6320a171 100644 --- a/src/project.h +++ b/src/project.h @@ -31,7 +31,7 @@ struct Project { std::vector entries; std::mutex mutex_; - std::unordered_map absolute_path_to_entry_index_; + std::unordered_map path_to_entry_index; // Loads a project for the given |directory|. // diff --git a/src/query.cc b/src/query.cc index 89df5cfc..3b114be4 100644 --- a/src/query.cc +++ b/src/query.cc @@ -215,16 +215,21 @@ void DB::ApplyIndexUpdate(IndexUpdate *u) { SymbolKind kind, Use &use, int delta, int k = 1) { use.file_id = use.file_id == -1 ? u->file_id : lid2fid.find(use.file_id)->second; + SymbolRef sym{{use.range, usr, kind, use.role}}; if (k & 1) { - int &v = - files[use.file_id] - .symbol2refcnt[SymbolRef{{use.range, usr, kind, use.role}}]; + int &v = files[use.file_id].symbol2refcnt[sym]; v += delta; assert(v >= 0); + if (!v) + files[use.file_id].symbol2refcnt.erase(sym); + } + if (k & 2) { + int &v = files[use.file_id].outline2refcnt[sym]; + v += delta; + assert(v >= 0); + if (!v) + files[use.file_id].outline2refcnt.erase(sym); } - if (k & 2) - files[use.file_id] - .outline2refcnt[SymbolRef{{use.range, usr, kind, use.role}}] += delta; }; auto RefDecl = [&](std::unordered_map &lid2fid, Usr usr, SymbolKind kind, DeclRef &dr, int delta) {