Skip to content

Commit

Permalink
Auto merge of #14630 - Veykril:arc, r=Veykril
Browse files Browse the repository at this point in the history
internal: `Arc<String>` -> `Arc<str>`
  • Loading branch information
bors committed Apr 22, 2023
2 parents 11c55b2 + f2295cd commit 1379b5f
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 38 deletions.
6 changes: 3 additions & 3 deletions crates/base-db/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{CrateGraph, ProcMacros, SourceDatabaseExt, SourceRoot, SourceRootId}
#[derive(Default)]
pub struct Change {
pub roots: Option<Vec<SourceRoot>>,
pub files_changed: Vec<(FileId, Option<Arc<String>>)>,
pub files_changed: Vec<(FileId, Option<Arc<str>>)>,
pub crate_graph: Option<CrateGraph>,
pub proc_macros: Option<ProcMacros>,
}
Expand Down Expand Up @@ -42,7 +42,7 @@ impl Change {
self.roots = Some(roots);
}

pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<String>>) {
pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<str>>) {
self.files_changed.push((file_id, new_text))
}

Expand Down Expand Up @@ -72,7 +72,7 @@ impl Change {
let source_root = db.source_root(source_root_id);
let durability = durability(&source_root);
// XXX: can't actually remove the file, just reset the text
let text = text.unwrap_or_default();
let text = text.unwrap_or_else(|| Arc::from(""));
db.set_file_text_with_durability(file_id, text, durability)
}
if let Some(crate_graph) = self.crate_graph {
Expand Down
6 changes: 3 additions & 3 deletions crates/base-db/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl ChangeFixture {
default_target_data_layout = meta.target_data_layout;
}

change.change_file(file_id, Some(Arc::new(text)));
change.change_file(file_id, Some(Arc::from(text)));
let path = VfsPath::new_virtual_path(meta.path);
file_set.insert(file_id, path);
files.push(file_id);
Expand Down Expand Up @@ -240,7 +240,7 @@ impl ChangeFixture {
fs.insert(core_file, VfsPath::new_virtual_path("/sysroot/core/lib.rs".to_string()));
roots.push(SourceRoot::new_library(fs));

change.change_file(core_file, Some(Arc::new(mini_core.source_code())));
change.change_file(core_file, Some(Arc::from(mini_core.source_code())));

let all_crates = crate_graph.crates_in_topological_order();

Expand Down Expand Up @@ -279,7 +279,7 @@ impl ChangeFixture {
);
roots.push(SourceRoot::new_library(fs));

change.change_file(proc_lib_file, Some(Arc::new(source)));
change.change_file(proc_lib_file, Some(Arc::from(source)));

let all_crates = crate_graph.crates_in_topological_order();

Expand Down
6 changes: 3 additions & 3 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub const DEFAULT_LRU_CAP: usize = 128;

pub trait FileLoader {
/// Text of the file.
fn file_text(&self, file_id: FileId) -> Arc<String>;
fn file_text(&self, file_id: FileId) -> Arc<str>;
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>;
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ fn parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFil
#[salsa::query_group(SourceDatabaseExtStorage)]
pub trait SourceDatabaseExt: SourceDatabase {
#[salsa::input]
fn file_text(&self, file_id: FileId) -> Arc<String>;
fn file_text(&self, file_id: FileId) -> Arc<str>;
/// Path to a file, relative to the root of its source root.
/// Source root of the file.
#[salsa::input]
Expand Down Expand Up @@ -118,7 +118,7 @@ fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<FxHas
pub struct FileLoaderDelegate<T>(pub T);

impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
fn file_text(&self, file_id: FileId) -> Arc<String> {
fn file_text(&self, file_id: FileId) -> Arc<str> {
SourceDatabaseExt::file_text(self.0, file_id)
}
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/nameres/tests/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn check_def_map_is_not_recomputed(ra_fixture_initial: &str, ra_fixture_change:
});
assert!(format!("{events:?}").contains("crate_def_map"), "{events:#?}")
}
db.set_file_text(pos.file_id, Arc::new(ra_fixture_change.to_string()));
db.set_file_text(pos.file_id, Arc::from(ra_fixture_change));

{
let events = db.log_executed(|| {
Expand Down Expand Up @@ -96,7 +96,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
});
assert!(format!("{events:?}").contains("crate_def_map"), "{events:#?}")
}
db.set_file_text(pos.file_id, Arc::new("m!(Y);".to_string()));
db.set_file_text(pos.file_id, Arc::from("m!(Y);"));

{
let events = db.log_executed(|| {
Expand Down Expand Up @@ -150,7 +150,7 @@ fn quux() { 92 }
m!(Y);
m!(Z);
"#;
db.set_file_text(pos.file_id, Arc::new(new_text.to_string()));
db.set_file_text(pos.file_id, Arc::from(new_text));

{
let events = db.log_executed(|| {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl fmt::Debug for TestDB {
impl panic::RefUnwindSafe for TestDB {}

impl FileLoader for TestDB {
fn file_text(&self, file_id: FileId) -> Arc<String> {
fn file_text(&self, file_id: FileId) -> Arc<str> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl salsa::ParallelDatabase for TestDB {
impl panic::RefUnwindSafe for TestDB {}

impl FileLoader for TestDB {
fn file_text(&self, file_id: FileId) -> Arc<String> {
fn file_text(&self, file_id: FileId) -> Arc<str> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
Expand Down
5 changes: 2 additions & 3 deletions crates/hir-ty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,9 @@ fn salsa_bug() {
let x = 1;
x.push(1);
}
"
.to_string();
";

db.set_file_text(pos.file_id, Arc::new(new_text));
db.set_file_text(pos.file_id, Arc::from(new_text));

let module = db.module_for_file(pos.file_id);
let crate_def_map = module.def_map(&db);
Expand Down
5 changes: 2 additions & 3 deletions crates/hir-ty/src/tests/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
+
1
}
"
.to_string();
";

db.set_file_text(pos.file_id, Arc::new(new_text));
db.set_file_text(pos.file_id, Arc::from(new_text));

{
let events = db.log_executed(|| {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Upcast<dyn HirDatabase> for RootDatabase {
}

impl FileLoader for RootDatabase {
fn file_text(&self, file_id: FileId) -> Arc<String> {
fn file_text(&self, file_id: FileId) -> Arc<str> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
Expand Down
6 changes: 3 additions & 3 deletions crates/ide-db/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ impl<'a> FindUsages<'a> {
fn scope_files<'a>(
sema: &'a Semantics<'_, RootDatabase>,
scope: &'a SearchScope,
) -> impl Iterator<Item = (Arc<String>, FileId, TextRange)> + 'a {
) -> impl Iterator<Item = (Arc<str>, FileId, TextRange)> + 'a {
scope.entries.iter().map(|(&file_id, &search_range)| {
let text = sema.db.file_text(file_id);
let search_range =
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str())));
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&*text)));

(text, file_id, search_range)
})
Expand Down Expand Up @@ -553,7 +553,7 @@ impl<'a> FindUsages<'a> {

let text = sema.db.file_text(file_id);
let search_range =
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str())));
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&*text)));

let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
let finder = &Finder::new("self");
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-ssr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl<'db> MatchFinder<'db> {
let file = self.sema.parse(file_id);
let mut res = Vec::new();
let file_text = self.sema.db.file_text(file_id);
let mut remaining_text = file_text.as_str();
let mut remaining_text = &*file_text;
let mut base = 0;
let len = snippet.len() as u32;
while let Some(offset) = remaining_text.find(snippet) {
Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Analysis {
Err("Analysis::from_single_file has no target layout".into()),
None,
);
change.change_file(file_id, Some(Arc::new(text)));
change.change_file(file_id, Some(Arc::from(text)));
change.set_crate_graph(crate_graph);
host.apply_change(change);
(host.analysis(), file_id)
Expand All @@ -263,7 +263,7 @@ impl Analysis {
}

/// Gets the text of the source file.
pub fn file_text(&self, file_id: FileId) -> Cancellable<Arc<String>> {
pub fn file_text(&self, file_id: FileId) -> Cancellable<Arc<str>> {
self.with_db(|db| db.file_text(file_id))
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ impl fmt::Display for FilesStats {
}
}

impl StatCollect<FileId, Arc<String>> for FilesStats {
fn collect_entry(&mut self, _: FileId, value: Option<Arc<String>>) {
impl StatCollect<FileId, Arc<str>> for FilesStats {
fn collect_entry(&mut self, _: FileId, value: Option<Arc<str>>) {
self.total += 1;
self.size += value.unwrap().len();
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rust-analyzer/src/cli/load_cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ fn load_crate_graph(
let changes = vfs.take_changes();
for file in changes {
if file.exists() {
let contents = vfs.file_contents(file.file_id).to_vec();
if let Ok(text) = String::from_utf8(contents) {
analysis_change.change_file(file.file_id, Some(Arc::new(text)))
let contents = vfs.file_contents(file.file_id);
if let Ok(text) = std::str::from_utf8(contents) {
analysis_change.change_file(file.file_id, Some(Arc::from(text)))
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/rust-analyzer/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl GlobalState {
String::from_utf8(bytes).ok().and_then(|text| {
let (text, line_endings) = LineEndings::normalize(text);
line_endings_map.insert(file.file_id, line_endings);
Some(Arc::new(text))
Some(Arc::from(text))
})
} else {
None
Expand Down Expand Up @@ -440,6 +440,10 @@ impl GlobalStateSnapshot {
ProjectWorkspace::DetachedFiles { .. } => None,
})
}

pub(crate) fn vfs_memory_usage(&self) -> usize {
self.vfs.read().0.memory_usage()
}
}

pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub(crate) fn handle_analyzer_status(
.collect::<Vec<&AbsPath>>()
);
}
format_to!(buf, "\nVfs memory usage: {}\n", snap.vfs_memory_usage());
buf.push_str("\nAnalysis:\n");
buf.push_str(
&snap
Expand Down
6 changes: 3 additions & 3 deletions crates/rust-analyzer/src/integrated_benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn integrated_highlighting_benchmark() {
let mut text = host.analysis().file_text(file_id).unwrap().to_string();
text.push_str("\npub fn _dummy() {}\n");
let mut change = Change::new();
change.change_file(file_id, Some(Arc::new(text)));
change.change_file(file_id, Some(Arc::from(text)));
host.apply_change(change);
}

Expand Down Expand Up @@ -121,7 +121,7 @@ fn integrated_completion_benchmark() {
patch(&mut text, "db.struct_data(self.id)", "sel;\ndb.struct_data(self.id)")
+ "sel".len();
let mut change = Change::new();
change.change_file(file_id, Some(Arc::new(text)));
change.change_file(file_id, Some(Arc::from(text)));
host.apply_change(change);
completion_offset
};
Expand Down Expand Up @@ -160,7 +160,7 @@ fn integrated_completion_benchmark() {
patch(&mut text, "sel;\ndb.struct_data(self.id)", "self.;\ndb.struct_data(self.id)")
+ "self.".len();
let mut change = Change::new();
change.change_file(file_id, Some(Arc::new(text)));
change.change_file(file_id, Some(Arc::from(text)));
host.apply_change(change);
completion_offset
};
Expand Down
11 changes: 9 additions & 2 deletions crates/vfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ impl Vfs {
self.get(file_id).as_deref().unwrap()
}

/// Returns the overall memory usage for the stored files.
pub fn memory_usage(&self) -> usize {
self.data.iter().flatten().map(|d| d.capacity()).sum()
}

/// Returns an iterator over the stored ids and their corresponding paths.
///
/// This will skip deleted files.
Expand All @@ -158,7 +163,7 @@ impl Vfs {
///
/// If the path does not currently exists in the `Vfs`, allocates a new
/// [`FileId`] for it.
pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) -> bool {
pub fn set_file_contents(&mut self, path: VfsPath, mut contents: Option<Vec<u8>>) -> bool {
let file_id = self.alloc_file_id(path);
let change_kind = match (self.get(file_id), &contents) {
(None, None) => return false,
Expand All @@ -167,7 +172,9 @@ impl Vfs {
(Some(_), None) => ChangeKind::Delete,
(Some(_), Some(_)) => ChangeKind::Modify,
};

if let Some(contents) = &mut contents {
contents.shrink_to_fit();
}
*self.get_mut(file_id) = contents;
self.changes.push(ChangedFile { file_id, change_kind });
true
Expand Down

0 comments on commit 1379b5f

Please sign in to comment.