internal: more obviously correct code for cache priming progerss

It doesn't make sense for the prime_caches itself send begin/end events
-- the caller knows perfectly fine when they happen!
This commit is contained in:
Aleksey Kladov 2021-08-30 19:18:48 +03:00
parent bb1987b45e
commit 6317292cd5
2 changed files with 36 additions and 37 deletions

View File

@ -8,17 +8,12 @@ use ide_db::base_db::SourceDatabase;
use crate::RootDatabase;
/// We started indexing a crate.
#[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 struct PrimeCachesProgress {
pub on_crate: String,
pub n_done: usize,
pub n_total: usize,
}
pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress) + Sync)) {
@ -26,21 +21,13 @@ pub(crate) fn prime_caches(db: &RootDatabase, cb: &(dyn Fn(PrimeCachesProgress)
let graph = db.crate_graph();
let topo = &graph.crates_in_topological_order();
cb(PrimeCachesProgress::Started);
// Take care to emit the finish signal even when the computation is canceled.
let _d = stdx::defer(|| cb(PrimeCachesProgress::Finished));
// 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, &crate_id) in topo.iter().enumerate() {
let crate_name = graph[crate_id].display_name.as_deref().unwrap_or_default().to_string();
cb(PrimeCachesProgress::StartedOnCrate {
on_crate: crate_name,
n_done: i,
n_total: topo.len(),
});
cb(PrimeCachesProgress { on_crate: crate_name, n_done: i, n_total: topo.len() });
db.crate_def_map(crate_id);
db.import_map(crate_id);
}

View File

@ -8,11 +8,10 @@ use std::{
use always_assert::always;
use crossbeam_channel::{select, Receiver};
use ide::{FileId, PrimeCachesProgress};
use ide_db::base_db::{SourceDatabaseExt, VfsPath};
use lsp_server::{Connection, Notification, Request};
use lsp_types::notification::Notification as _;
use vfs::ChangeKind;
use vfs::{ChangeKind, FileId};
use crate::{
config::Config,
@ -67,6 +66,13 @@ pub(crate) enum Task {
FetchBuildData(BuildDataProgress),
}
#[derive(Debug)]
pub(crate) enum PrimeCachesProgress {
Begin,
Report(ide::PrimeCachesProgress),
End { cancelled: bool },
}
impl fmt::Debug for Event {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let debug_verbose_not = |not: &Notification, f: &mut fmt::Formatter| {
@ -209,17 +215,17 @@ impl GlobalState {
}
}
Task::PrimeCaches(progress) => match progress {
PrimeCachesProgress::Started => prime_caches_progress.push(progress),
PrimeCachesProgress::StartedOnCrate { .. } => {
PrimeCachesProgress::Begin => prime_caches_progress.push(progress),
PrimeCachesProgress::Report(_) => {
match prime_caches_progress.last_mut() {
Some(last @ PrimeCachesProgress::StartedOnCrate { .. }) => {
Some(last @ PrimeCachesProgress::Report(_)) => {
// Coalesce subsequent update events.
*last = progress;
}
_ => prime_caches_progress.push(progress),
}
}
PrimeCachesProgress::Finished => prime_caches_progress.push(progress),
PrimeCachesProgress::End { .. } => prime_caches_progress.push(progress),
},
Task::FetchWorkspace(progress) => {
let (state, msg) = match progress {
@ -275,17 +281,20 @@ impl GlobalState {
for progress in prime_caches_progress {
let (state, message, fraction);
match progress {
PrimeCachesProgress::Started => {
PrimeCachesProgress::Begin => {
state = Progress::Begin;
message = None;
fraction = 0.0;
}
PrimeCachesProgress::StartedOnCrate { on_crate, n_done, n_total } => {
PrimeCachesProgress::Report(report) => {
state = Progress::Report;
message = Some(format!("{}/{} ({})", n_done, n_total, on_crate));
fraction = Progress::fraction(n_done, n_total);
message = Some(format!(
"{}/{} ({})",
report.n_done, report.n_total, report.on_crate
));
fraction = Progress::fraction(report.n_done, report.n_total);
}
PrimeCachesProgress::Finished => {
PrimeCachesProgress::End { cancelled: _ } => {
state = Progress::End;
message = None;
fraction = 1.0;
@ -422,13 +431,16 @@ impl GlobalState {
self.task_pool.handle.spawn_with_sender({
let analysis = self.snapshot().analysis;
move |sender| {
let cb = |progress| {
sender.send(Task::PrimeCaches(progress)).unwrap();
};
match analysis.prime_caches(cb) {
Ok(()) => (),
Err(_canceled) => (),
}
sender.send(Task::PrimeCaches(PrimeCachesProgress::Begin)).unwrap();
let res = analysis.prime_caches(|progress| {
let report = PrimeCachesProgress::Report(progress);
sender.send(Task::PrimeCaches(report)).unwrap();
});
sender
.send(Task::PrimeCaches(PrimeCachesProgress::End {
cancelled: res.is_err(),
}))
.unwrap();
}
});
}