intern strings in dependencies and IndexInclude::resolved_path

This commit is contained in:
Fangrui Song 2018-09-19 00:51:15 -07:00
parent 1249eb1eb0
commit 97e773081b
6 changed files with 27 additions and 21 deletions

View File

@ -1089,7 +1089,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 {
@ -1343,7 +1343,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;

View File

@ -15,7 +15,8 @@
#include "utils.h"
#include <clang/Basic/Specifiers.h>
#include <llvm/ADT/StringMap.h>
#include <llvm/ADT/CachedHashString.h>
#include <llvm/ADT/DenseMap.h>
#include <algorithm>
#include <optional>
@ -220,7 +221,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 {
@ -254,7 +255,7 @@ struct IndexFile {
std::vector<Range> skipped_ranges;
std::vector<IndexInclude> includes;
llvm::StringMap<int64_t> dependencies;
llvm::DenseMap<llvm::CachedHashStringRef, int64_t> dependencies;
std::unordered_map<Usr, IndexFunc> usr2func;
std::unordered_map<Usr, IndexType> usr2type;
std::unordered_map<Usr, IndexVar> usr2var;

View File

@ -325,7 +325,7 @@ bool Indexer_Parse(CompletionManager *completion, WorkingFiles *wfiles,
if (entry.id >= 0) {
std::lock_guard<std::mutex> 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;
}
}
}

View File

@ -50,7 +50,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)};
}

View File

@ -35,7 +35,7 @@ struct QueryFile {
// Parts of the file which are disabled.
std::vector<Range> skipped_ranges;
// Used by |$ccls/reload|.
std::vector<std::string> dependencies;
std::vector<const char *> dependencies;
};
using DefUpdate = std::pair<Def, std::string>;

View File

@ -15,6 +15,7 @@
#include <mutex>
#include <stdexcept>
using namespace ccls;
using namespace llvm;
bool gTestOutputMode = false;
@ -115,7 +116,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); }
@ -148,33 +149,33 @@ void Reflect(Writer &visitor, std::unordered_map<Usr, V> &map) {
}
// Used by IndexFile::dependencies.
void Reflect(Reader &vis, StringMap<int64_t> &v) {
void Reflect(Reader &vis, DenseMap<CachedHashStringRef, int64_t> &v) {
std::string name;
if (vis.Format() == SerializeFormat::Json) {
auto &vis1 = static_cast<JsonReader&>(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<int64_t> &v) {
void Reflect(Writer &vis, DenseMap<CachedHashStringRef, int64_t> &v) {
if (vis.Format() == SerializeFormat::Json) {
auto &vis1 = static_cast<JsonWriter&>(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);
}
@ -455,13 +456,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<int64_t> 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);
}