Skip to content

Commit 2074cc2

Browse files
committed
Auto merge of rust-lang#16722 - mo8it:allocations, r=Veykril
Avoid some allocations I went on a small `.clone()` hunting tour :D
2 parents d8feb90 + 00a049b commit 2074cc2

File tree

6 files changed

+30
-27
lines changed

6 files changed

+30
-27
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl flags::AnalysisStats {
371371

372372
let parse = sema.parse(file_id);
373373
let file_txt = db.file_text(file_id);
374-
let path = vfs.file_path(file_id).as_path().unwrap().to_owned();
374+
let path = vfs.file_path(file_id).as_path().unwrap();
375375

376376
for node in parse.syntax().descendants() {
377377
let expr = match syntax::ast::Expr::cast(node.clone()) {
@@ -446,7 +446,7 @@ impl flags::AnalysisStats {
446446
edit.apply(&mut txt);
447447

448448
if self.validate_term_search {
449-
std::fs::write(&path, txt).unwrap();
449+
std::fs::write(path, txt).unwrap();
450450

451451
let res = ws.run_build_scripts(&cargo_config, &|_| ()).unwrap();
452452
if let Some(err) = res.error() {
@@ -495,7 +495,7 @@ impl flags::AnalysisStats {
495495
}
496496
// Revert file back to original state
497497
if self.validate_term_search {
498-
std::fs::write(&path, file_txt.to_string()).unwrap();
498+
std::fs::write(path, file_txt.to_string()).unwrap();
499499
}
500500

501501
bar.inc(1);
@@ -1077,12 +1077,12 @@ fn location_csv_pat(db: &RootDatabase, vfs: &Vfs, sm: &BodySourceMap, pat_id: Pa
10771077
format!("{path},{}:{},{}:{}", start.line + 1, start.col, end.line + 1, end.col)
10781078
}
10791079

1080-
fn expr_syntax_range(
1080+
fn expr_syntax_range<'a>(
10811081
db: &RootDatabase,
1082-
vfs: &Vfs,
1082+
vfs: &'a Vfs,
10831083
sm: &BodySourceMap,
10841084
expr_id: ExprId,
1085-
) -> Option<(VfsPath, LineCol, LineCol)> {
1085+
) -> Option<(&'a VfsPath, LineCol, LineCol)> {
10861086
let src = sm.expr_syntax(expr_id);
10871087
if let Ok(src) = src {
10881088
let root = db.parse_or_expand(src.file_id);
@@ -1098,12 +1098,12 @@ fn expr_syntax_range(
10981098
None
10991099
}
11001100
}
1101-
fn pat_syntax_range(
1101+
fn pat_syntax_range<'a>(
11021102
db: &RootDatabase,
1103-
vfs: &Vfs,
1103+
vfs: &'a Vfs,
11041104
sm: &BodySourceMap,
11051105
pat_id: PatId,
1106-
) -> Option<(VfsPath, LineCol, LineCol)> {
1106+
) -> Option<(&'a VfsPath, LineCol, LineCol)> {
11071107
let src = sm.pat_syntax(pat_id);
11081108
if let Ok(src) = src {
11091109
let root = db.parse_or_expand(src.file_id);

crates/rust-analyzer/src/global_state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl GlobalState {
297297
let mut bytes = vec![];
298298
let mut modified_rust_files = vec![];
299299
for file in changed_files {
300-
let vfs_path = &vfs.file_path(file.file_id);
300+
let vfs_path = vfs.file_path(file.file_id);
301301
if let Some(path) = vfs_path.as_path() {
302302
let path = path.to_path_buf();
303303
if reload::should_refresh_for_change(&path, file.kind()) {
@@ -481,23 +481,23 @@ impl GlobalStateSnapshot {
481481
}
482482

483483
pub(crate) fn anchored_path(&self, path: &AnchoredPathBuf) -> Url {
484-
let mut base = self.vfs_read().file_path(path.anchor);
484+
let mut base = self.vfs_read().file_path(path.anchor).clone();
485485
base.pop();
486486
let path = base.join(&path.path).unwrap();
487487
let path = path.as_path().unwrap();
488488
url_from_abs_path(path)
489489
}
490490

491491
pub(crate) fn file_id_to_file_path(&self, file_id: FileId) -> vfs::VfsPath {
492-
self.vfs_read().file_path(file_id)
492+
self.vfs_read().file_path(file_id).clone()
493493
}
494494

495495
pub(crate) fn cargo_target_for_crate_root(
496496
&self,
497497
crate_id: CrateId,
498498
) -> Option<(&CargoWorkspace, Target)> {
499499
let file_id = self.analysis.crate_root(crate_id).ok()?;
500-
let path = self.vfs_read().file_path(file_id);
500+
let path = self.vfs_read().file_path(file_id).clone();
501501
let path = path.as_path()?;
502502
self.workspaces.iter().find_map(|ws| match ws {
503503
ProjectWorkspace::Cargo { cargo, .. } => {

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ pub(crate) fn fetch_dependency_list(
20972097
.into_iter()
20982098
.filter_map(|it| {
20992099
let root_file_path = state.file_id_to_file_path(it.root_file_id);
2100-
crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult {
2100+
crate_path(&root_file_path).and_then(to_url).map(|path| CrateInfoResult {
21012101
name: it.name,
21022102
version: it.version,
21032103
path,
@@ -2118,7 +2118,7 @@ pub(crate) fn fetch_dependency_list(
21182118
/// An `Option` value representing the path to the directory of the crate with the given
21192119
/// name, if such a crate is found. If no crate with the given name is found, this function
21202120
/// returns `None`.
2121-
fn crate_path(root_file_path: VfsPath) -> Option<VfsPath> {
2121+
fn crate_path(root_file_path: &VfsPath) -> Option<VfsPath> {
21222122
let mut current_dir = root_file_path.parent();
21232123
while let Some(path) = current_dir {
21242124
let cargo_toml_path = path.join("../Cargo.toml")?;

crates/test-fixture/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ impl ChangeFixture {
149149
for entry in fixture {
150150
let text = if entry.text.contains(CURSOR_MARKER) {
151151
if entry.text.contains(ESCAPED_CURSOR_MARKER) {
152-
entry.text.replace(ESCAPED_CURSOR_MARKER, CURSOR_MARKER)
152+
entry.text.replace(ESCAPED_CURSOR_MARKER, CURSOR_MARKER).into()
153153
} else {
154154
let (range_or_offset, text) = extract_range_or_offset(&entry.text);
155155
assert!(file_position.is_none());
156156
file_position = Some((file_id, range_or_offset));
157-
text
157+
text.into()
158158
}
159159
} else {
160-
entry.text.clone()
160+
entry.text.as_str().into()
161161
};
162162

163163
let meta = FileMeta::from_fixture(entry, current_source_root_kind);
@@ -195,7 +195,10 @@ impl ChangeFixture {
195195
let prev = crates.insert(crate_name.clone(), crate_id);
196196
assert!(prev.is_none(), "multiple crates with same name: {}", crate_name);
197197
for dep in meta.deps {
198-
let prelude = meta.extern_prelude.contains(&dep);
198+
let prelude = match &meta.extern_prelude {
199+
Some(v) => v.contains(&dep),
200+
None => true,
201+
};
199202
let dep = CrateName::normalize_dashes(&dep);
200203
crate_deps.push((crate_name.clone(), dep, prelude))
201204
}
@@ -206,7 +209,7 @@ impl ChangeFixture {
206209
default_env.extend(meta.env.iter().map(|(x, y)| (x.to_owned(), y.to_owned())));
207210
}
208211

209-
source_change.change_file(file_id, Some(text.into()));
212+
source_change.change_file(file_id, Some(text));
210213
let path = VfsPath::new_virtual_path(meta.path);
211214
file_set.insert(file_id, path);
212215
files.push(file_id);
@@ -443,7 +446,7 @@ struct FileMeta {
443446
path: String,
444447
krate: Option<(String, CrateOrigin, Option<String>)>,
445448
deps: Vec<String>,
446-
extern_prelude: Vec<String>,
449+
extern_prelude: Option<Vec<String>>,
447450
cfg: CfgOptions,
448451
edition: Edition,
449452
env: Env,
@@ -473,7 +476,7 @@ impl FileMeta {
473476
Self {
474477
path: f.path,
475478
krate: f.krate.map(|it| parse_crate(it, current_source_root_kind, f.library)),
476-
extern_prelude: f.extern_prelude.unwrap_or_else(|| deps.clone()),
479+
extern_prelude: f.extern_prelude,
477480
deps,
478481
cfg,
479482
edition: f.edition.map_or(Edition::CURRENT, |v| Edition::from_str(&v).unwrap()),

crates/vfs/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ impl Vfs {
163163
/// # Panics
164164
///
165165
/// Panics if the id is not present in the `Vfs`.
166-
pub fn file_path(&self, file_id: FileId) -> VfsPath {
167-
self.interner.lookup(file_id).clone()
166+
pub fn file_path(&self, file_id: FileId) -> &VfsPath {
167+
self.interner.lookup(file_id)
168168
}
169169

170170
/// Returns an iterator over the stored ids and their corresponding paths.

lib/lsp-server/src/req_queue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<I> Incoming<I> {
3737
}
3838

3939
pub fn cancel(&mut self, id: RequestId) -> Option<Response> {
40-
let _data = self.complete(id.clone())?;
40+
let _data = self.complete(&id)?;
4141
let error = ResponseError {
4242
code: ErrorCode::RequestCanceled as i32,
4343
message: "canceled by client".to_owned(),
@@ -46,8 +46,8 @@ impl<I> Incoming<I> {
4646
Some(Response { id, result: None, error: Some(error) })
4747
}
4848

49-
pub fn complete(&mut self, id: RequestId) -> Option<I> {
50-
self.pending.remove(&id)
49+
pub fn complete(&mut self, id: &RequestId) -> Option<I> {
50+
self.pending.remove(id)
5151
}
5252

5353
pub fn is_completed(&self, id: &RequestId) -> bool {

0 commit comments

Comments
 (0)