Shuffle code around to avoid an allocation

This commit is contained in:
Laurențiu Nicola 2021-09-13 20:58:09 +03:00
parent 8875f2c8aa
commit 4d7a3bb5c7
3 changed files with 50 additions and 60 deletions

View file

@ -25,7 +25,7 @@ use crate::{
main_loop::Task, main_loop::Task,
mem_docs::MemDocs, mem_docs::MemDocs,
op_queue::OpQueue, op_queue::OpQueue,
reload::SourceRootConfig, reload::{self, SourceRootConfig},
thread_pool::TaskPool, thread_pool::TaskPool,
to_proto::url_from_abs_path, to_proto::url_from_abs_path,
Result, Result,
@ -175,7 +175,8 @@ impl GlobalState {
pub(crate) fn process_changes(&mut self) -> bool { pub(crate) fn process_changes(&mut self) -> bool {
let _p = profile::span("GlobalState::process_changes"); let _p = profile::span("GlobalState::process_changes");
let mut fs_changes = Vec::new(); let mut fs_changes = Vec::new();
let mut has_fs_changes = false; // A file was added or deleted
let mut has_structure_changes = false;
let change = { let change = {
let mut change = Change::new(); let mut change = Change::new();
@ -187,9 +188,13 @@ impl GlobalState {
for file in changed_files { for file in changed_files {
if let Some(path) = vfs.file_path(file.file_id).as_path() { if let Some(path) = vfs.file_path(file.file_id).as_path() {
fs_changes.push((path.to_path_buf(), file.change_kind)); let path = path.to_path_buf();
if reload::should_refresh_for_change(&path, file.change_kind) {
self.fetch_workspaces_queue.request_op();
}
fs_changes.push((path, file.change_kind));
if file.is_created_or_deleted() { if file.is_created_or_deleted() {
has_fs_changes = true; has_structure_changes = true;
} }
} }
@ -208,7 +213,7 @@ impl GlobalState {
}; };
change.change_file(file.file_id, text); change.change_file(file.file_id, text);
} }
if has_fs_changes { if has_structure_changes {
let roots = self.source_root_config.partition(vfs); let roots = self.source_root_config.partition(vfs);
change.set_roots(roots); change.set_roots(roots);
} }
@ -216,7 +221,6 @@ impl GlobalState {
}; };
self.analysis_host.apply_change(change); self.analysis_host.apply_change(change);
self.maybe_refresh(&fs_changes);
true true
} }

View file

@ -21,7 +21,7 @@ use crate::{
handlers, lsp_ext, handlers, lsp_ext,
lsp_utils::{apply_document_changes, is_cancelled, notification_is, Progress}, lsp_utils::{apply_document_changes, is_cancelled, notification_is, Progress},
mem_docs::DocumentData, mem_docs::DocumentData,
reload::{BuildDataProgress, ProjectWorkspaceProgress}, reload::{self, BuildDataProgress, ProjectWorkspaceProgress},
Result, Result,
}; };
@ -693,7 +693,9 @@ impl GlobalState {
flycheck.update(); flycheck.update();
} }
if let Ok(abs_path) = from_proto::abs_path(&params.text_document.uri) { if let Ok(abs_path) = from_proto::abs_path(&params.text_document.uri) {
this.maybe_refresh(&[(abs_path, ChangeKind::Modify)]); if reload::should_refresh_for_change(&abs_path, ChangeKind::Modify) {
this.fetch_workspaces_queue.request_op();
}
} }
Ok(()) Ok(())
})? })?

View file

@ -58,58 +58,6 @@ impl GlobalState {
.raw_database_mut() .raw_database_mut()
.set_enable_proc_attr_macros(self.config.expand_proc_attr_macros()); .set_enable_proc_attr_macros(self.config.expand_proc_attr_macros());
} }
pub(crate) fn maybe_refresh(&mut self, changes: &[(AbsPathBuf, ChangeKind)]) {
if !changes.iter().any(|(path, kind)| is_interesting(path, *kind)) {
return;
}
tracing::info!(
"Requesting workspace reload because of the following changes: {}",
itertools::join(
changes
.iter()
.filter(|(path, kind)| is_interesting(path, *kind))
.map(|(path, kind)| format!("{}: {:?}", path.display(), kind)),
", "
)
);
self.fetch_workspaces_queue.request_op();
fn is_interesting(path: &AbsPath, change_kind: ChangeKind) -> bool {
const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
let file_name = path.file_name().unwrap_or_default();
if file_name == "Cargo.toml" || file_name == "Cargo.lock" {
return true;
}
if change_kind == ChangeKind::Modify {
return false;
}
if path.extension().unwrap_or_default() != "rs" {
return false;
}
if IMPLICIT_TARGET_FILES.iter().any(|it| path.as_ref().ends_with(it)) {
return true;
}
let parent = match path.parent() {
Some(it) => it,
None => return false,
};
if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.as_ref().ends_with(it)) {
return true;
}
if file_name == "main.rs" {
let grand_parent = match parent.parent() {
Some(it) => it,
None => return false,
};
if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.as_ref().ends_with(it)) {
return true;
}
}
false
}
}
pub(crate) fn current_status(&self) -> lsp_ext::ServerStatusParams { pub(crate) fn current_status(&self) -> lsp_ext::ServerStatusParams {
let mut status = lsp_ext::ServerStatusParams { let mut status = lsp_ext::ServerStatusParams {
@ -617,3 +565,39 @@ pub(crate) fn load_proc_macro(client: Option<&ProcMacroServer>, path: &AbsPath)
} }
} }
} }
pub(crate) fn should_refresh_for_change(path: &AbsPath, change_kind: ChangeKind) -> bool {
const IMPLICIT_TARGET_FILES: &[&str] = &["build.rs", "src/main.rs", "src/lib.rs"];
const IMPLICIT_TARGET_DIRS: &[&str] = &["src/bin", "examples", "tests", "benches"];
let file_name = path.file_name().unwrap_or_default();
if file_name == "Cargo.toml" || file_name == "Cargo.lock" {
return true;
}
if change_kind == ChangeKind::Modify {
return false;
}
if path.extension().unwrap_or_default() != "rs" {
return false;
}
if IMPLICIT_TARGET_FILES.iter().any(|it| path.as_ref().ends_with(it)) {
return true;
}
let parent = match path.parent() {
Some(it) => it,
None => return false,
};
if IMPLICIT_TARGET_DIRS.iter().any(|it| parent.as_ref().ends_with(it)) {
return true;
}
if file_name == "main.rs" {
let grand_parent = match parent.parent() {
Some(it) => it,
None => return false,
};
if IMPLICIT_TARGET_DIRS.iter().any(|it| grand_parent.as_ref().ends_with(it)) {
return true;
}
}
false
}