diff --git a/src/cache.cc b/src/cache.cc index 2d0dc41e..8e8cab5b 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -4,6 +4,8 @@ #include "platform.h" #include "language_server_api.h" +#include + #include namespace { @@ -30,7 +32,9 @@ std::unique_ptr LoadCachedIndex(Config* config, if (!file_content) return nullptr; - return Deserialize(filename, *file_content, IndexFile::kCurrentVersion); + auto result = Deserialize(filename, *file_content, IndexFile::kCurrentVersion); + result->is_loaded_from_cache_ = true; + return result; } optional LoadCachedFileContents(Config* config, @@ -42,25 +46,21 @@ optional LoadCachedFileContents(Config* config, } void WriteToCache(Config* config, - const std::string& filename, - IndexFile& file, - const optional& indexed_file_content) { + IndexFile& file) { if (!config->enableCacheWrite) return; std::string cache_basename = - GetCachedBaseFileName(config->cacheDirectory, filename); + GetCachedBaseFileName(config->cacheDirectory, file.path); - if (indexed_file_content) { - std::ofstream cache_content; - cache_content.open(cache_basename); - assert(cache_content.good()); - cache_content << *indexed_file_content; - cache_content.close(); - } - else { - CopyFileTo(cache_basename, filename); - } + LOG_IF_S(ERROR, file.file_contents_.empty()) << "Writing " << file.path << " to cache but it has no contents"; + + assert(!file.file_contents_.empty()); + std::ofstream cache_content; + cache_content.open(cache_basename); + assert(cache_content.good()); + cache_content << file.file_contents_; + cache_content.close(); std::string indexed_content = Serialize(file); std::ofstream cache; diff --git a/src/cache.h b/src/cache.h index 95aeb4ed..34f2da54 100644 --- a/src/cache.h +++ b/src/cache.h @@ -17,6 +17,4 @@ optional LoadCachedFileContents(Config* config, const std::string& filename); void WriteToCache(Config* config, - const std::string& filename, - IndexFile& file, - const optional& indexed_file_contents); \ No newline at end of file + IndexFile& file); \ No newline at end of file diff --git a/src/command_line.cc b/src/command_line.cc index 1aafba66..76c190ff 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -583,6 +583,7 @@ struct Index_OnIdMapped { std::unique_ptr previous; std::unique_ptr current; + PerformanceImportFile perf; bool is_interactive; @@ -638,8 +639,6 @@ struct Index_OnIndexed { - - @@ -896,11 +895,6 @@ std::vector DoParseFile( // Note: we are reusing the parent perf. perf.index_load_cached = time.ElapsedMicrosecondsAndReset(); - // Write new cache to disk. Add file to list of paths that need to be reimported. - time.Reset(); - WriteToCache(config, new_index->path, *new_index, new_index->file_contents_); - perf.index_save_to_disk = time.ElapsedMicrosecondsAndReset(); - // TODO/FIXME: real is_interactive bool is_interactive = false; LOG_S(INFO) << "Emitting index result for " << new_index->path; @@ -952,6 +946,7 @@ bool IndexMain_DoParse( } bool IndexMain_DoCreateIndexUpdate( + Config* config, QueueManager* queue) { // TODO: Index_OnIdMapped dtor is failing because it seems that its contents have already been destroyed. optional response = queue->on_id_mapped.TryDequeue(); @@ -968,11 +963,19 @@ bool IndexMain_DoCreateIndexUpdate( previous_index = response->previous->file.get(); } + // Build delta update. IndexUpdate update = IndexUpdate::CreateDelta( previous_id_map, response->current->ids.get(), previous_index, response->current->file.get()); response->perf.index_make_delta = time.ElapsedMicrosecondsAndReset(); + // Write new cache to disk if it is a fresh index. + if (!response->current->file->is_loaded_from_cache_) { + time.Reset(); + WriteToCache(config, *response->current->file); + response->perf.index_save_to_disk = time.ElapsedMicrosecondsAndReset(); + } + #if false #define PRINT_SECTION(name) \ if (response->perf.name) {\ @@ -1061,8 +1064,8 @@ void IndexMain(Config* config, bool did_parse = IndexMain_DoParse(config, queue, file_consumer_shared, &index); bool did_create_update = - IndexMain_DoCreateIndexUpdate(queue); - + IndexMain_DoCreateIndexUpdate(config, queue); + bool did_load_previous = IndexMain_LoadPreviousIndex(config, queue); // Nothing to index and no index updates to create, so join some already diff --git a/src/indexer.cc b/src/indexer.cc index bb2237c1..7ffabf2c 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -356,10 +356,6 @@ std::string IndexFile::ToString() { return Serialize(*this); } -void IndexFile::ClearLargeState() { - file_contents_ = ""; -} - IndexType::IndexType(IndexTypeId id, const std::string& usr) : def(usr), id(id) { assert(usr.size() > 0); diff --git a/src/indexer.h b/src/indexer.h index c47c3ca8..01dbcdc6 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -480,6 +480,8 @@ struct IndexFile { std::vector funcs; std::vector vars; + // True if this file was loaded from cache. Not serialized. + bool is_loaded_from_cache_ = false; // Diagnostics found when indexing this file. Not serialized. NonElidedVector diagnostics_; // File contents at the time of index. Not serialized. @@ -498,7 +500,6 @@ struct IndexFile { IndexVar* Resolve(IndexVarId id); std::string ToString(); - void ClearLargeState(); }; struct FileContents {