From 525b6da1accfb0e6a7d69fc8016f12ab85a4eebd Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 19 Sep 2018 00:51:15 -0700 Subject: [PATCH] intern strings in dependencies and IndexInclude::resolved_path --- src/indexer.cc | 5 +++-- src/indexer.h | 7 ++++--- src/pipeline.cc | 2 +- src/query.cc | 2 +- src/query.h | 2 +- src/serializer.cc | 30 +++++++++++++++++------------- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/indexer.cc b/src/indexer.cc index 592d35f0..1f28475c 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1104,7 +1104,7 @@ public: if (IndexFile *db = param.ConsumeFile(*FE)) { std::string file_name = FileName(*File); if (file_name.size()) - db->includes.push_back({spell.start.line, std::move(file_name)}); + db->includes.push_back({spell.start.line, Intern(file_name)}); } } void MacroDefined(const Token &Tok, const MacroDirective *MD) override { @@ -1368,7 +1368,8 @@ Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs, // dependency set. for (auto &[_, path] : param.SeenUniqueID) if (path != entry->path && path != entry->import_file) - entry->dependencies[path] = param.file2mtime[path]; + entry->dependencies[llvm::CachedHashStringRef(Intern(path))] = + param.file2mtime[path]; } return result; diff --git a/src/indexer.h b/src/indexer.h index ab20ba30..7235f373 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -27,7 +27,8 @@ limitations under the License. #include "utils.h" #include -#include +#include +#include #include #include @@ -232,7 +233,7 @@ struct IndexInclude { // information - a line is good enough for clicking. int line = 0; // Absolute path to the index. - std::string resolved_path; + const char *resolved_path; }; struct IndexFile { @@ -266,7 +267,7 @@ struct IndexFile { std::vector skipped_ranges; std::vector includes; - llvm::StringMap dependencies; + llvm::DenseMap dependencies; std::unordered_map usr2func; std::unordered_map usr2type; std::unordered_map usr2var; diff --git a/src/pipeline.cc b/src/pipeline.cc index 3b8fb3c5..f651f271 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -337,7 +337,7 @@ bool Indexer_Parse(CompletionManager *completion, WorkingFiles *wfiles, if (entry.id >= 0) { std::lock_guard lock(project->mutex_); for (auto &dep : curr->dependencies) - project->path_to_entry_index[dep.first()] = entry.id; + project->path_to_entry_index[dep.first.val().str()] = entry.id; } } } diff --git a/src/query.cc b/src/query.cc index d09f33b1..f052c42c 100644 --- a/src/query.cc +++ b/src/query.cc @@ -62,7 +62,7 @@ QueryFile::DefUpdate BuildFileDefUpdate(const IndexFile &indexed) { def.skipped_ranges = std::move(indexed.skipped_ranges); def.dependencies.reserve(indexed.dependencies.size()); for (auto &dep : indexed.dependencies) - def.dependencies.push_back(dep.first()); + def.dependencies.push_back(dep.first.val().data()); // llvm 8 -> data() def.language = indexed.language; return {std::move(def), std::move(indexed.file_contents)}; } diff --git a/src/query.h b/src/query.h index ea7d0bcf..4dfe62bb 100644 --- a/src/query.h +++ b/src/query.h @@ -46,7 +46,7 @@ struct QueryFile { // Parts of the file which are disabled. std::vector skipped_ranges; // Used by |$ccls/reload|. - std::vector dependencies; + std::vector dependencies; }; using DefUpdate = std::pair; diff --git a/src/serializer.cc b/src/serializer.cc index e0f59535..d523b118 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -27,6 +27,7 @@ limitations under the License. #include #include +using namespace ccls; using namespace llvm; bool gTestOutputMode = false; @@ -127,7 +128,7 @@ void Reflect(Writer &visitor, std::string_view &data) { void Reflect(Reader &vis, const char *&v) { const char *str = vis.GetString(); - v = ccls::Intern(str); + v = Intern(str); } void Reflect(Writer &vis, const char *&v) { vis.String(v); } @@ -160,33 +161,33 @@ void Reflect(Writer &visitor, std::unordered_map &map) { } // Used by IndexFile::dependencies. -void Reflect(Reader &vis, StringMap &v) { +void Reflect(Reader &vis, DenseMap &v) { std::string name; if (vis.Format() == SerializeFormat::Json) { auto &vis1 = static_cast(vis); for (auto it = vis1.m().MemberBegin(); it != vis1.m().MemberEnd(); ++it) - v[it->name.GetString()] = it->value.GetInt64(); + v[CachedHashStringRef(Intern(it->name.GetString()))] = + it->value.GetInt64(); } else { vis.IterArray([&](Reader &entry) { Reflect(entry, name); - Reflect(entry, v[name]); + Reflect(entry, v[CachedHashStringRef(Intern(name))]); }); } } -void Reflect(Writer &vis, StringMap &v) { +void Reflect(Writer &vis, DenseMap &v) { if (vis.Format() == SerializeFormat::Json) { auto &vis1 = static_cast(vis); vis.StartObject(); for (auto &it : v) { - std::string key = it.first(); - vis1.m().Key(key.c_str()); + vis1.m().Key(it.first.val().data()); // llvm 8 -> data() vis1.m().Int64(it.second); } vis.EndObject(); } else { vis.StartArray(v.size()); for (auto &it : v) { - std::string key = it.first(); + std::string key = it.first.val().str(); Reflect(vis, key); Reflect(vis, it.second); } @@ -467,13 +468,16 @@ Deserialize(SerializeFormat format, const std::string &path, DoPathMapping(arg); for (auto &[_, path] : file->lid2path) DoPathMapping(path); - for (auto &include : file->includes) - DoPathMapping(include.resolved_path); - StringMap dependencies; + for (auto &include : file->includes) { + std::string p(include.resolved_path); + DoPathMapping(p); + include.resolved_path = Intern(p); + } + decltype(file->dependencies) dependencies; for (auto &it : file->dependencies) { - std::string path = it.first().str(); + std::string path = it.first.val().str(); DoPathMapping(path); - dependencies[path] = it.second; + dependencies[CachedHashStringRef(Intern(path))] = it.second; } file->dependencies = std::move(dependencies); }