Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1379b5f

Browse files
committed
Auto merge of rust-lang#14630 - Veykril:arc, r=Veykril
internal: `Arc<String>` -> `Arc<str>`
2 parents 11c55b2 + f2295cd commit 1379b5f

File tree

18 files changed

+48
-38
lines changed

18 files changed

+48
-38
lines changed

crates/base-db/src/change.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{CrateGraph, ProcMacros, SourceDatabaseExt, SourceRoot, SourceRootId}
1212
#[derive(Default)]
1313
pub struct Change {
1414
pub roots: Option<Vec<SourceRoot>>,
15-
pub files_changed: Vec<(FileId, Option<Arc<String>>)>,
15+
pub files_changed: Vec<(FileId, Option<Arc<str>>)>,
1616
pub crate_graph: Option<CrateGraph>,
1717
pub proc_macros: Option<ProcMacros>,
1818
}
@@ -42,7 +42,7 @@ impl Change {
4242
self.roots = Some(roots);
4343
}
4444

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

@@ -72,7 +72,7 @@ impl Change {
7272
let source_root = db.source_root(source_root_id);
7373
let durability = durability(&source_root);
7474
// XXX: can't actually remove the file, just reset the text
75-
let text = text.unwrap_or_default();
75+
let text = text.unwrap_or_else(|| Arc::from(""));
7676
db.set_file_text_with_durability(file_id, text, durability)
7777
}
7878
if let Some(crate_graph) = self.crate_graph {

crates/base-db/src/fixture.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl ChangeFixture {
190190
default_target_data_layout = meta.target_data_layout;
191191
}
192192

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

243-
change.change_file(core_file, Some(Arc::new(mini_core.source_code())));
243+
change.change_file(core_file, Some(Arc::from(mini_core.source_code())));
244244

245245
let all_crates = crate_graph.crates_in_topological_order();
246246

@@ -279,7 +279,7 @@ impl ChangeFixture {
279279
);
280280
roots.push(SourceRoot::new_library(fs));
281281

282-
change.change_file(proc_lib_file, Some(Arc::new(source)));
282+
change.change_file(proc_lib_file, Some(Arc::from(source)));
283283

284284
let all_crates = crate_graph.crates_in_topological_order();
285285

crates/base-db/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub const DEFAULT_LRU_CAP: usize = 128;
5757

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

120120
impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
121-
fn file_text(&self, file_id: FileId) -> Arc<String> {
121+
fn file_text(&self, file_id: FileId) -> Arc<str> {
122122
SourceDatabaseExt::file_text(self.0, file_id)
123123
}
124124
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {

crates/hir-def/src/nameres/tests/incremental.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn check_def_map_is_not_recomputed(ra_fixture_initial: &str, ra_fixture_change:
1515
});
1616
assert!(format!("{events:?}").contains("crate_def_map"), "{events:#?}")
1717
}
18-
db.set_file_text(pos.file_id, Arc::new(ra_fixture_change.to_string()));
18+
db.set_file_text(pos.file_id, Arc::from(ra_fixture_change));
1919

2020
{
2121
let events = db.log_executed(|| {
@@ -96,7 +96,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
9696
});
9797
assert!(format!("{events:?}").contains("crate_def_map"), "{events:#?}")
9898
}
99-
db.set_file_text(pos.file_id, Arc::new("m!(Y);".to_string()));
99+
db.set_file_text(pos.file_id, Arc::from("m!(Y);"));
100100

101101
{
102102
let events = db.log_executed(|| {
@@ -150,7 +150,7 @@ fn quux() { 92 }
150150
m!(Y);
151151
m!(Z);
152152
"#;
153-
db.set_file_text(pos.file_id, Arc::new(new_text.to_string()));
153+
db.set_file_text(pos.file_id, Arc::from(new_text));
154154

155155
{
156156
let events = db.log_executed(|| {

crates/hir-def/src/test_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl fmt::Debug for TestDB {
7171
impl panic::RefUnwindSafe for TestDB {}
7272

7373
impl FileLoader for TestDB {
74-
fn file_text(&self, file_id: FileId) -> Arc<String> {
74+
fn file_text(&self, file_id: FileId) -> Arc<str> {
7575
FileLoaderDelegate(self).file_text(file_id)
7676
}
7777
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {

crates/hir-ty/src/test_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl salsa::ParallelDatabase for TestDB {
7676
impl panic::RefUnwindSafe for TestDB {}
7777

7878
impl FileLoader for TestDB {
79-
fn file_text(&self, file_id: FileId) -> Arc<String> {
79+
fn file_text(&self, file_id: FileId) -> Arc<str> {
8080
FileLoaderDelegate(self).file_text(file_id)
8181
}
8282
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {

crates/hir-ty/src/tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,9 @@ fn salsa_bug() {
572572
let x = 1;
573573
x.push(1);
574574
}
575-
"
576-
.to_string();
575+
";
577576

578-
db.set_file_text(pos.file_id, Arc::new(new_text));
577+
db.set_file_text(pos.file_id, Arc::from(new_text));
579578

580579
let module = db.module_for_file(pos.file_id);
581580
let crate_def_map = module.def_map(&db);

crates/hir-ty/src/tests/incremental.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
3333
+
3434
1
3535
}
36-
"
37-
.to_string();
36+
";
3837

39-
db.set_file_text(pos.file_id, Arc::new(new_text));
38+
db.set_file_text(pos.file_id, Arc::from(new_text));
4039

4140
{
4241
let events = db.log_executed(|| {

crates/ide-db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl Upcast<dyn HirDatabase> for RootDatabase {
113113
}
114114

115115
impl FileLoader for RootDatabase {
116-
fn file_text(&self, file_id: FileId) -> Arc<String> {
116+
fn file_text(&self, file_id: FileId) -> Arc<str> {
117117
FileLoaderDelegate(self).file_text(file_id)
118118
}
119119
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {

crates/ide-db/src/search.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,11 @@ impl<'a> FindUsages<'a> {
438438
fn scope_files<'a>(
439439
sema: &'a Semantics<'_, RootDatabase>,
440440
scope: &'a SearchScope,
441-
) -> impl Iterator<Item = (Arc<String>, FileId, TextRange)> + 'a {
441+
) -> impl Iterator<Item = (Arc<str>, FileId, TextRange)> + 'a {
442442
scope.entries.iter().map(|(&file_id, &search_range)| {
443443
let text = sema.db.file_text(file_id);
444444
let search_range =
445-
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str())));
445+
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&*text)));
446446

447447
(text, file_id, search_range)
448448
})
@@ -553,7 +553,7 @@ impl<'a> FindUsages<'a> {
553553

554554
let text = sema.db.file_text(file_id);
555555
let search_range =
556-
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str())));
556+
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&*text)));
557557

558558
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
559559
let finder = &Finder::new("self");

crates/ide-ssr/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl<'db> MatchFinder<'db> {
224224
let file = self.sema.parse(file_id);
225225
let mut res = Vec::new();
226226
let file_text = self.sema.db.file_text(file_id);
227-
let mut remaining_text = file_text.as_str();
227+
let mut remaining_text = &*file_text;
228228
let mut base = 0;
229229
let len = snippet.len() as u32;
230230
while let Some(offset) = remaining_text.find(snippet) {

crates/ide/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl Analysis {
244244
Err("Analysis::from_single_file has no target layout".into()),
245245
None,
246246
);
247-
change.change_file(file_id, Some(Arc::new(text)));
247+
change.change_file(file_id, Some(Arc::from(text)));
248248
change.set_crate_graph(crate_graph);
249249
host.apply_change(change);
250250
(host.analysis(), file_id)
@@ -263,7 +263,7 @@ impl Analysis {
263263
}
264264

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

crates/ide/src/status.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ impl fmt::Display for FilesStats {
164164
}
165165
}
166166

167-
impl StatCollect<FileId, Arc<String>> for FilesStats {
168-
fn collect_entry(&mut self, _: FileId, value: Option<Arc<String>>) {
167+
impl StatCollect<FileId, Arc<str>> for FilesStats {
168+
fn collect_entry(&mut self, _: FileId, value: Option<Arc<str>>) {
169169
self.total += 1;
170170
self.size += value.unwrap().len();
171171
}

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ fn load_crate_graph(
162162
let changes = vfs.take_changes();
163163
for file in changes {
164164
if file.exists() {
165-
let contents = vfs.file_contents(file.file_id).to_vec();
166-
if let Ok(text) = String::from_utf8(contents) {
167-
analysis_change.change_file(file.file_id, Some(Arc::new(text)))
165+
let contents = vfs.file_contents(file.file_id);
166+
if let Ok(text) = std::str::from_utf8(contents) {
167+
analysis_change.change_file(file.file_id, Some(Arc::from(text)))
168168
}
169169
}
170170
}

crates/rust-analyzer/src/global_state.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl GlobalState {
269269
String::from_utf8(bytes).ok().and_then(|text| {
270270
let (text, line_endings) = LineEndings::normalize(text);
271271
line_endings_map.insert(file.file_id, line_endings);
272-
Some(Arc::new(text))
272+
Some(Arc::from(text))
273273
})
274274
} else {
275275
None
@@ -440,6 +440,10 @@ impl GlobalStateSnapshot {
440440
ProjectWorkspace::DetachedFiles { .. } => None,
441441
})
442442
}
443+
444+
pub(crate) fn vfs_memory_usage(&self) -> usize {
445+
self.vfs.read().0.memory_usage()
446+
}
443447
}
444448

445449
pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {

crates/rust-analyzer/src/handlers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub(crate) fn handle_analyzer_status(
103103
.collect::<Vec<&AbsPath>>()
104104
);
105105
}
106+
format_to!(buf, "\nVfs memory usage: {}\n", snap.vfs_memory_usage());
106107
buf.push_str("\nAnalysis:\n");
107108
buf.push_str(
108109
&snap

crates/rust-analyzer/src/integrated_benchmarks.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn integrated_highlighting_benchmark() {
6565
let mut text = host.analysis().file_text(file_id).unwrap().to_string();
6666
text.push_str("\npub fn _dummy() {}\n");
6767
let mut change = Change::new();
68-
change.change_file(file_id, Some(Arc::new(text)));
68+
change.change_file(file_id, Some(Arc::from(text)));
6969
host.apply_change(change);
7070
}
7171

@@ -121,7 +121,7 @@ fn integrated_completion_benchmark() {
121121
patch(&mut text, "db.struct_data(self.id)", "sel;\ndb.struct_data(self.id)")
122122
+ "sel".len();
123123
let mut change = Change::new();
124-
change.change_file(file_id, Some(Arc::new(text)));
124+
change.change_file(file_id, Some(Arc::from(text)));
125125
host.apply_change(change);
126126
completion_offset
127127
};
@@ -160,7 +160,7 @@ fn integrated_completion_benchmark() {
160160
patch(&mut text, "sel;\ndb.struct_data(self.id)", "self.;\ndb.struct_data(self.id)")
161161
+ "self.".len();
162162
let mut change = Change::new();
163-
change.change_file(file_id, Some(Arc::new(text)));
163+
change.change_file(file_id, Some(Arc::from(text)));
164164
host.apply_change(change);
165165
completion_offset
166166
};

crates/vfs/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ impl Vfs {
139139
self.get(file_id).as_deref().unwrap()
140140
}
141141

142+
/// Returns the overall memory usage for the stored files.
143+
pub fn memory_usage(&self) -> usize {
144+
self.data.iter().flatten().map(|d| d.capacity()).sum()
145+
}
146+
142147
/// Returns an iterator over the stored ids and their corresponding paths.
143148
///
144149
/// This will skip deleted files.
@@ -158,7 +163,7 @@ impl Vfs {
158163
///
159164
/// If the path does not currently exists in the `Vfs`, allocates a new
160165
/// [`FileId`] for it.
161-
pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) -> bool {
166+
pub fn set_file_contents(&mut self, path: VfsPath, mut contents: Option<Vec<u8>>) -> bool {
162167
let file_id = self.alloc_file_id(path);
163168
let change_kind = match (self.get(file_id), &contents) {
164169
(None, None) => return false,
@@ -167,7 +172,9 @@ impl Vfs {
167172
(Some(_), None) => ChangeKind::Delete,
168173
(Some(_), Some(_)) => ChangeKind::Modify,
169174
};
170-
175+
if let Some(contents) = &mut contents {
176+
contents.shrink_to_fit();
177+
}
171178
*self.get_mut(file_id) = contents;
172179
self.changes.push(ChangedFile { file_id, change_kind });
173180
true

0 commit comments

Comments
 (0)