Stop using an Arc when setting the file text

This commit is contained in:
Laurențiu Nicola 2024-01-10 12:17:10 +02:00
parent 02b6c181dd
commit 0f43b55e83
11 changed files with 21 additions and 24 deletions

View file

@ -13,7 +13,7 @@ use crate::{CrateGraph, SourceDatabaseExt, SourceDatabaseExt2, SourceRoot, Sourc
#[derive(Default)] #[derive(Default)]
pub struct FileChange { pub struct FileChange {
pub roots: Option<Vec<SourceRoot>>, pub roots: Option<Vec<SourceRoot>>,
pub files_changed: Vec<(FileId, Option<Arc<str>>)>, pub files_changed: Vec<(FileId, Option<String>)>,
pub crate_graph: Option<CrateGraph>, pub crate_graph: Option<CrateGraph>,
} }
@ -42,7 +42,7 @@ impl FileChange {
self.roots = Some(roots); self.roots = Some(roots);
} }
pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<str>>) { pub fn change_file(&mut self, file_id: FileId, new_text: Option<String>) {
self.files_changed.push((file_id, new_text)) self.files_changed.push((file_id, new_text))
} }
@ -68,7 +68,7 @@ impl FileChange {
let source_root = db.source_root(source_root_id); let source_root = db.source_root(source_root_id);
let durability = durability(&source_root); let durability = durability(&source_root);
// XXX: can't actually remove the file, just reset the text // XXX: can't actually remove the file, just reset the text
let text = text.unwrap_or_else(|| Arc::from("")); let text = text.as_ref().map(String::as_str).unwrap_or_else(|| "");
db.set_file_text_with_durability(file_id, text, durability) db.set_file_text_with_durability(file_id, text, durability)
} }
if let Some(crate_graph) = self.crate_graph { if let Some(crate_graph) = self.crate_graph {

View file

@ -115,14 +115,14 @@ fn file_text(db: &dyn SourceDatabaseExt, file_id: FileId) -> Arc<str> {
} }
pub trait SourceDatabaseExt2 { pub trait SourceDatabaseExt2 {
fn set_file_text(&mut self, file_id: FileId, text: Arc<str>) { fn set_file_text(&mut self, file_id: FileId, text: &str) {
self.set_file_text_with_durability(file_id, text, Durability::LOW); self.set_file_text_with_durability(file_id, text, Durability::LOW);
} }
fn set_file_text_with_durability( fn set_file_text_with_durability(
&mut self, &mut self,
file_id: FileId, file_id: FileId,
text: Arc<str>, text: &str,
durability: Durability, durability: Durability,
); );
} }
@ -131,7 +131,7 @@ impl<Db: ?Sized + SourceDatabaseExt> SourceDatabaseExt2 for Db {
fn set_file_text_with_durability( fn set_file_text_with_durability(
&mut self, &mut self,
file_id: FileId, file_id: FileId,
text: Arc<str>, text: &str,
durability: Durability, durability: Durability,
) { ) {
let bytes = text.as_bytes(); let bytes = text.as_bytes();

View file

@ -1,6 +1,5 @@
use base_db::{SourceDatabase, SourceDatabaseExt2 as _}; use base_db::{SourceDatabase, SourceDatabaseExt2 as _};
use test_fixture::WithFixture; use test_fixture::WithFixture;
use triomphe::Arc;
use crate::{db::DefDatabase, nameres::tests::TestDB, AdtId, ModuleDefId}; use crate::{db::DefDatabase, nameres::tests::TestDB, AdtId, ModuleDefId};
@ -17,7 +16,7 @@ fn check_def_map_is_not_recomputed(ra_fixture_initial: &str, ra_fixture_change:
}); });
assert!(format!("{events:?}").contains("crate_def_map"), "{events:#?}") assert!(format!("{events:?}").contains("crate_def_map"), "{events:#?}")
} }
db.set_file_text(pos.file_id, Arc::from(ra_fixture_change)); db.set_file_text(pos.file_id, ra_fixture_change);
{ {
let events = db.log_executed(|| { let events = db.log_executed(|| {
@ -267,7 +266,7 @@ fn quux() { 92 }
m!(Y); m!(Y);
m!(Z); m!(Z);
"#; "#;
db.set_file_text(pos.file_id, Arc::from(new_text)); db.set_file_text(pos.file_id, new_text);
{ {
let events = db.log_executed(|| { let events = db.log_executed(|| {

View file

@ -48,7 +48,7 @@ impl ChangeWithProcMacros {
} }
} }
pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<str>>) { pub fn change_file(&mut self, file_id: FileId, new_text: Option<String>) {
self.source_change.change_file(file_id, new_text) self.source_change.change_file(file_id, new_text)
} }

View file

@ -575,7 +575,7 @@ fn salsa_bug() {
} }
"; ";
db.set_file_text(pos.file_id, Arc::from(new_text)); db.set_file_text(pos.file_id, new_text);
let module = db.module_for_file(pos.file_id); let module = db.module_for_file(pos.file_id);
let crate_def_map = module.def_map(&db); let crate_def_map = module.def_map(&db);

View file

@ -1,6 +1,5 @@
use base_db::SourceDatabaseExt2 as _; use base_db::SourceDatabaseExt2 as _;
use test_fixture::WithFixture; use test_fixture::WithFixture;
use triomphe::Arc;
use crate::{db::HirDatabase, test_db::TestDB}; use crate::{db::HirDatabase, test_db::TestDB};
@ -33,7 +32,7 @@ fn foo() -> i32 {
1 1
}"; }";
db.set_file_text(pos.file_id, Arc::from(new_text)); db.set_file_text(pos.file_id, new_text);
{ {
let events = db.log_executed(|| { let events = db.log_executed(|| {
@ -85,7 +84,7 @@ fn baz() -> i32 {
} }
"; ";
db.set_file_text(pos.file_id, Arc::from(new_text)); db.set_file_text(pos.file_id, new_text);
{ {
let events = db.log_executed(|| { let events = db.log_executed(|| {

View file

@ -259,7 +259,7 @@ impl Analysis {
false, false,
CrateOrigin::Local { repo: None, name: None }, CrateOrigin::Local { repo: None, name: None },
); );
change.change_file(file_id, Some(Arc::from(text))); change.change_file(file_id, Some(text));
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);
change.set_target_data_layouts(vec![Err("fixture has no layout".into())]); change.set_target_data_layouts(vec![Err("fixture has no layout".into())]);
change.set_toolchains(vec![None]); change.set_toolchains(vec![None]);

View file

@ -361,8 +361,8 @@ fn load_crate_graph(
let changes = vfs.take_changes(); let changes = vfs.take_changes();
for file in changes { for file in changes {
if let vfs::Change::Create(v) | vfs::Change::Modify(v) = file.change { if let vfs::Change::Create(v) | vfs::Change::Modify(v) = file.change {
if let Ok(text) = std::str::from_utf8(&v) { if let Ok(text) = String::from_utf8(v) {
analysis_change.change_file(file.file_id, Some(text.into())) analysis_change.change_file(file.file_id, Some(text))
} }
} }
} }

View file

@ -134,7 +134,7 @@ impl Tester {
let should_have_no_error = text.contains("// check-pass") let should_have_no_error = text.contains("// check-pass")
|| text.contains("// build-pass") || text.contains("// build-pass")
|| text.contains("// run-pass"); || text.contains("// run-pass");
change.change_file(self.root_file, Some(Arc::from(text))); change.change_file(self.root_file, Some(text));
self.host.apply_change(change); self.host.apply_change(change);
let diagnostic_config = DiagnosticsConfig::test_sample(); let diagnostic_config = DiagnosticsConfig::test_sample();

View file

@ -330,7 +330,7 @@ impl GlobalState {
// FIXME: Consider doing normalization in the `vfs` instead? That allows // FIXME: Consider doing normalization in the `vfs` instead? That allows
// getting rid of some locking // getting rid of some locking
let (text, line_endings) = LineEndings::normalize(text); let (text, line_endings) = LineEndings::normalize(text);
(Arc::from(text), line_endings) (text, line_endings)
}) })
} else { } else {
None None

View file

@ -20,7 +20,6 @@ use ide_db::{
}; };
use project_model::CargoConfig; use project_model::CargoConfig;
use test_utils::project_root; use test_utils::project_root;
use triomphe::Arc;
use vfs::{AbsPathBuf, VfsPath}; use vfs::{AbsPathBuf, VfsPath};
use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice}; use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
@ -70,7 +69,7 @@ fn integrated_highlighting_benchmark() {
let mut text = host.analysis().file_text(file_id).unwrap().to_string(); let mut text = host.analysis().file_text(file_id).unwrap().to_string();
text.push_str("\npub fn _dummy() {}\n"); text.push_str("\npub fn _dummy() {}\n");
let mut change = ChangeWithProcMacros::new(); let mut change = ChangeWithProcMacros::new();
change.change_file(file_id, Some(Arc::from(text))); change.change_file(file_id, Some(text));
host.apply_change(change); host.apply_change(change);
} }
@ -125,7 +124,7 @@ fn integrated_completion_benchmark() {
patch(&mut text, "db.struct_data(self.id)", "sel;\ndb.struct_data(self.id)") patch(&mut text, "db.struct_data(self.id)", "sel;\ndb.struct_data(self.id)")
+ "sel".len(); + "sel".len();
let mut change = ChangeWithProcMacros::new(); let mut change = ChangeWithProcMacros::new();
change.change_file(file_id, Some(Arc::from(text))); change.change_file(file_id, Some(text));
host.apply_change(change); host.apply_change(change);
completion_offset completion_offset
}; };
@ -168,7 +167,7 @@ fn integrated_completion_benchmark() {
patch(&mut text, "sel;\ndb.struct_data(self.id)", ";sel;\ndb.struct_data(self.id)") patch(&mut text, "sel;\ndb.struct_data(self.id)", ";sel;\ndb.struct_data(self.id)")
+ ";sel".len(); + ";sel".len();
let mut change = ChangeWithProcMacros::new(); let mut change = ChangeWithProcMacros::new();
change.change_file(file_id, Some(Arc::from(text))); change.change_file(file_id, Some(text));
host.apply_change(change); host.apply_change(change);
completion_offset completion_offset
}; };
@ -210,7 +209,7 @@ fn integrated_completion_benchmark() {
patch(&mut text, "sel;\ndb.struct_data(self.id)", "self.;\ndb.struct_data(self.id)") patch(&mut text, "sel;\ndb.struct_data(self.id)", "self.;\ndb.struct_data(self.id)")
+ "self.".len(); + "self.".len();
let mut change = ChangeWithProcMacros::new(); let mut change = ChangeWithProcMacros::new();
change.change_file(file_id, Some(Arc::from(text))); change.change_file(file_id, Some(text));
host.apply_change(change); host.apply_change(change);
completion_offset completion_offset
}; };