From cde7392ec809599e6337d91561971e08c8e06831 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 6 Oct 2020 17:58:03 +0200 Subject: Improve prime_caches and display its progress --- crates/ide/src/lib.rs | 8 ++++++-- crates/ide/src/prime_caches.rs | 43 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 6 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 686cee3a1..aaf9b3b4b 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -77,6 +77,7 @@ pub use crate::{ hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult}, inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, markup::Markup, + prime_caches::PrimeCachesProgress, references::{ Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult, RenameError, }, @@ -223,8 +224,11 @@ impl Analysis { self.with_db(|db| status::status(&*db, file_id)) } - pub fn prime_caches(&self, files: Vec) -> Cancelable<()> { - self.with_db(|db| prime_caches::prime_caches(db, files)) + pub fn prime_caches(&self, cb: F) -> Cancelable<()> + where + F: Fn(PrimeCachesProgress) + Sync + std::panic::UnwindSafe, + { + self.with_db(move |db| prime_caches::prime_caches(db, &cb)) } /// Gets the text of the source file. diff --git a/crates/ide/src/prime_caches.rs b/crates/ide/src/prime_caches.rs index c5ab5a1d8..9687c2734 100644 --- a/crates/ide/src/prime_caches.rs +++ b/crates/ide/src/prime_caches.rs @@ -3,10 +3,45 @@ //! request takes longer to compute. This modules implemented prepopulating of //! various caches, it's not really advanced at the moment. -use crate::{FileId, RootDatabase}; +use base_db::SourceDatabase; +use hir::db::DefDatabase; -pub(crate) fn prime_caches(db: &RootDatabase, files: Vec) { - for file in files { - let _ = crate::syntax_highlighting::highlight(db, file, None, false); +use crate::RootDatabase; + +#[derive(Debug)] +pub enum PrimeCachesProgress { + Started, + /// We started indexing a crate. + StartedOnCrate { + on_crate: String, + n_done: usize, + n_total: usize, + }, + /// We finished indexing all crates. + Finished, +} + +pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress) + Sync)) { + let _p = profile::span("prime_caches"); + let graph = db.crate_graph(); + let topo = &graph.crates_in_topological_order(); + + cb(PrimeCachesProgress::Started); + + // FIXME: This would be easy to parallelize, since it's in the ideal ordering for that. + // Unfortunately rayon prevents panics from propagation out of a `scope`, which breaks + // cancellation, so we cannot use rayon. + for (i, krate) in topo.iter().enumerate() { + let crate_name = + graph[*krate].declaration_name.as_ref().map(ToString::to_string).unwrap_or_default(); + + cb(PrimeCachesProgress::StartedOnCrate { + on_crate: crate_name, + n_done: i, + n_total: topo.len(), + }); + db.crate_def_map(*krate); } + + cb(PrimeCachesProgress::Finished); } -- cgit v1.2.3