Skip to content

Commit d0cc986

Browse files
committed
check_doc_keyword: don't alloc string for emptiness check
check_doc_alias_value: get argument as Symbol to prevent needless string convertions check_doc_attrs: don't alloc vec, iterate over slice. Vec introduced in rust-lang#83149, but no perf run posted on merge replace as_str() check with symbol check get_single_str_from_tts: don't prealloc string trivial string to str replace LifetimeScopeForPath::NonElided use Vec<Symbol> instead of Vec<String> AssertModuleSource use BTreeSet<Symbol> instead of BTreeSet<String> CrateInfo.crate_name replace FxHashMap<CrateNum, String> with FxHashMap<CrateNum, Symbol>
1 parent f262ca1 commit d0cc986

File tree

15 files changed

+56
-65
lines changed

15 files changed

+56
-65
lines changed

Diff for: compiler/rustc_builtin_macros/src/compile_error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn expand_compile_error<'cx>(
1313
return DummyResult::any(sp);
1414
};
1515

16-
cx.span_err(sp, &var);
16+
cx.span_err(sp, var.as_str());
1717

1818
DummyResult::any(sp)
1919
}

Diff for: compiler/rustc_builtin_macros/src/env.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub fn expand_option_env<'cx>(
2121
};
2222

2323
let sp = cx.with_def_site_ctxt(sp);
24-
let value = env::var(&var.as_str()).ok().as_deref().map(Symbol::intern);
25-
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((Symbol::intern(&var), value));
24+
let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
25+
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
2626
let e = match value {
2727
None => {
2828
let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));

Diff for: compiler/rustc_builtin_macros/src/source_util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub fn expand_include<'cx>(
104104
return DummyResult::any(sp);
105105
};
106106
// The file will be added to the code map by the parser
107-
let file = match resolve_path(cx, file, sp) {
107+
let file = match resolve_path(cx, file.as_str(), sp) {
108108
Ok(f) => f,
109109
Err(mut err) => {
110110
err.emit();
@@ -173,7 +173,7 @@ pub fn expand_include_str(
173173
let Some(file) = get_single_str_from_tts(cx, sp, tts, "include_str!") else {
174174
return DummyResult::any(sp);
175175
};
176-
let file = match resolve_path(cx, file, sp) {
176+
let file = match resolve_path(cx, file.as_str(), sp) {
177177
Ok(f) => f,
178178
Err(mut err) => {
179179
err.emit();
@@ -207,7 +207,7 @@ pub fn expand_include_bytes(
207207
let Some(file) = get_single_str_from_tts(cx, sp, tts, "include_bytes!") else {
208208
return DummyResult::any(sp);
209209
};
210-
let file = match resolve_path(cx, file, sp) {
210+
let file = match resolve_path(cx, file.as_str(), sp) {
211211
Ok(f) => f,
212212
Err(mut err) => {
213213
err.emit();

Diff for: compiler/rustc_codegen_cranelift/src/driver/jit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ fn load_imported_symbols_for_jit(
288288
match data[cnum.as_usize() - 1] {
289289
Linkage::NotLinked | Linkage::IncludedFromDylib => {}
290290
Linkage::Static => {
291-
let name = &crate_info.crate_name[&cnum];
292-
let mut err = sess.struct_err(&format!("Can't load static lib {}", name.as_str()));
291+
let name = crate_info.crate_name[&cnum];
292+
let mut err = sess.struct_err(&format!("Can't load static lib {}", name));
293293
err.note("rustc_codegen_cranelift can only load dylibs in JIT mode.");
294294
err.emit();
295295
}

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub fn each_linked_rlib(
216216
Some(_) => {}
217217
None => return Err("could not find formats for rlibs".to_string()),
218218
}
219-
let name = &info.crate_name[&cnum];
219+
let name = info.crate_name[&cnum];
220220
let used_crate_source = &info.used_crate_source[&cnum];
221221
if let Some((path, _)) = &used_crate_source.rlib {
222222
f(cnum, &path);
@@ -467,7 +467,7 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
467467
let mut all_native_libs = vec![];
468468

469469
let res = each_linked_rlib(&codegen_results.crate_info, &mut |cnum, path| {
470-
let name = &codegen_results.crate_info.crate_name[&cnum];
470+
let name = codegen_results.crate_info.crate_name[&cnum];
471471
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
472472

473473
// Here when we include the rlib into our staticlib we need to make a

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ impl CrateInfo {
861861
for &cnum in crates.iter() {
862862
info.native_libraries
863863
.insert(cnum, tcx.native_libraries(cnum).iter().map(Into::into).collect());
864-
info.crate_name.insert(cnum, tcx.crate_name(cnum).to_string());
864+
info.crate_name.insert(cnum, tcx.crate_name(cnum));
865865
info.used_crate_source.insert(cnum, tcx.used_crate_source(cnum).clone());
866866
if tcx.is_compiler_builtins(cnum) {
867867
info.compiler_builtins = Some(cnum);

Diff for: compiler/rustc_codegen_ssa/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub struct CrateInfo {
146146
pub profiler_runtime: Option<CrateNum>,
147147
pub is_no_builtins: FxHashSet<CrateNum>,
148148
pub native_libraries: FxHashMap<CrateNum, Vec<NativeLib>>,
149-
pub crate_name: FxHashMap<CrateNum, String>,
149+
pub crate_name: FxHashMap<CrateNum, Symbol>,
150150
pub used_libraries: Vec<NativeLib>,
151151
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
152152
pub used_crates: Vec<CrateNum>,

Diff for: compiler/rustc_expand/src/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ pub fn get_single_str_from_tts(
12151215
sp: Span,
12161216
tts: TokenStream,
12171217
name: &str,
1218-
) -> Option<String> {
1218+
) -> Option<Symbol> {
12191219
let mut p = cx.new_parser_from_tts(tts);
12201220
if p.token == token::Eof {
12211221
cx.span_err(sp, &format!("{} takes 1 argument", name));
@@ -1227,7 +1227,7 @@ pub fn get_single_str_from_tts(
12271227
if p.token != token::Eof {
12281228
cx.span_err(sp, &format!("{} takes 1 argument", name));
12291229
}
1230-
expr_to_string(cx, ret, "argument must be a string literal").map(|(s, _)| s.to_string())
1230+
expr_to_string(cx, ret, "argument must be a string literal").map(|(s, _)| s)
12311231
}
12321232

12331233
/// Extracts comma-separated expressions from `tts`.

Diff for: compiler/rustc_incremental/src/assert_module_sources.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
//! was re-used.
2323
2424
use rustc_ast as ast;
25+
use rustc_data_structures::stable_set::FxHashSet;
2526
use rustc_hir::def_id::LOCAL_CRATE;
2627
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
2728
use rustc_middle::ty::TyCtxt;
2829
use rustc_session::cgu_reuse_tracker::*;
2930
use rustc_span::symbol::{sym, Symbol};
30-
use std::collections::BTreeSet;
3131

3232
#[allow(missing_docs)]
3333
pub fn assert_module_sources(tcx: TyCtxt<'_>) {
@@ -36,12 +36,8 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>) {
3636
return;
3737
}
3838

39-
let available_cgus = tcx
40-
.collect_and_partition_mono_items(())
41-
.1
42-
.iter()
43-
.map(|cgu| cgu.name().to_string())
44-
.collect::<BTreeSet<String>>();
39+
let available_cgus =
40+
tcx.collect_and_partition_mono_items(()).1.iter().map(|cgu| cgu.name()).collect();
4541

4642
let ams = AssertModuleSource { tcx, available_cgus };
4743

@@ -53,7 +49,7 @@ pub fn assert_module_sources(tcx: TyCtxt<'_>) {
5349

5450
struct AssertModuleSource<'tcx> {
5551
tcx: TyCtxt<'tcx>,
56-
available_cgus: BTreeSet<String>,
52+
available_cgus: FxHashSet<Symbol>,
5753
}
5854

5955
impl<'tcx> AssertModuleSource<'tcx> {
@@ -124,18 +120,17 @@ impl<'tcx> AssertModuleSource<'tcx> {
124120

125121
debug!("mapping '{}' to cgu name '{}'", self.field(attr, sym::module), cgu_name);
126122

127-
if !self.available_cgus.contains(cgu_name.as_str()) {
123+
if !self.available_cgus.contains(&cgu_name) {
124+
let mut cgu_names: Vec<&str> =
125+
self.available_cgus.iter().map(|cgu| cgu.as_str()).collect();
126+
cgu_names.sort();
128127
self.tcx.sess.span_err(
129128
attr.span,
130129
&format!(
131130
"no module named `{}` (mangled: {}). Available modules: {}",
132131
user_path,
133132
cgu_name,
134-
self.available_cgus
135-
.iter()
136-
.map(|cgu| cgu.to_string())
137-
.collect::<Vec<_>>()
138-
.join(", ")
133+
cgu_names.join(", ")
139134
),
140135
);
141136
}

Diff for: compiler/rustc_middle/src/middle/resolve_lifetime.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
use rustc_hir::def_id::{DefId, LocalDefId};
77
use rustc_hir::ItemLocalId;
88
use rustc_macros::HashStable;
9+
use rustc_span::symbol::Symbol;
910

1011
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
1112
pub enum Region {
@@ -23,7 +24,7 @@ pub enum Region {
2324
pub enum LifetimeScopeForPath {
2425
// Contains all lifetime names that are in scope and could possibly be used in generics
2526
// arguments of path.
26-
NonElided(Vec<String>),
27+
NonElided(Vec<Symbol>),
2728

2829
// Information that allows us to suggest args of the form `<'_>` in case
2930
// no generic arguments were provided for a path.

Diff for: compiler/rustc_middle/src/mir/pretty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,7 @@ fn write_scope_tree(
561561
}
562562
indented_decl.push(';');
563563

564-
let local_name =
565-
if local == RETURN_PLACE { " return place".to_string() } else { String::new() };
564+
let local_name = if local == RETURN_PLACE { " return place" } else { "" };
566565

567566
writeln!(
568567
w,

Diff for: compiler/rustc_passes/src/check_attr.rs

+27-31
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_session::lint::builtin::{
2020
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, UNUSED_ATTRIBUTES,
2121
};
2222
use rustc_session::parse::feature_err;
23-
use rustc_span::symbol::{sym, Symbol};
23+
use rustc_span::symbol::{kw, sym, Symbol};
2424
use rustc_span::{Span, DUMMY_SP};
2525
use std::collections::hash_map::Entry;
2626

@@ -536,7 +536,7 @@ impl CheckAttrVisitor<'_> {
536536
fn check_doc_alias_value(
537537
&self,
538538
meta: &NestedMetaItem,
539-
doc_alias: &str,
539+
doc_alias: Symbol,
540540
hir_id: HirId,
541541
target: Target,
542542
is_list: bool,
@@ -554,14 +554,17 @@ impl CheckAttrVisitor<'_> {
554554
);
555555
false
556556
};
557-
if doc_alias.is_empty() {
557+
if doc_alias == kw::Empty {
558558
return err_fn(
559559
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
560560
"attribute cannot have empty value",
561561
);
562562
}
563-
if let Some(c) =
564-
doc_alias.chars().find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' '))
563+
564+
let doc_alias_str = doc_alias.as_str();
565+
if let Some(c) = doc_alias_str
566+
.chars()
567+
.find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' '))
565568
{
566569
self.tcx.sess.span_err(
567570
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
@@ -573,7 +576,7 @@ impl CheckAttrVisitor<'_> {
573576
);
574577
return false;
575578
}
576-
if doc_alias.starts_with(' ') || doc_alias.ends_with(' ') {
579+
if doc_alias_str.starts_with(' ') || doc_alias_str.ends_with(' ') {
577580
return err_fn(
578581
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
579582
"cannot start or end with ' '",
@@ -608,11 +611,11 @@ impl CheckAttrVisitor<'_> {
608611
return err_fn(meta.span(), &format!("isn't allowed on {}", err));
609612
}
610613
let item_name = self.tcx.hir().name(hir_id);
611-
if item_name.as_str() == doc_alias {
614+
if item_name == doc_alias {
612615
return err_fn(meta.span(), "is the same as the item's name");
613616
}
614617
let span = meta.span();
615-
if let Err(entry) = aliases.try_insert(doc_alias.to_owned(), span) {
618+
if let Err(entry) = aliases.try_insert(doc_alias_str.to_owned(), span) {
616619
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, span, |lint| {
617620
lint.build("doc alias is duplicated")
618621
.span_label(*entry.entry.get(), "first defined here")
@@ -635,14 +638,7 @@ impl CheckAttrVisitor<'_> {
635638
match v.literal() {
636639
Some(l) => match l.kind {
637640
LitKind::Str(s, _) => {
638-
if !self.check_doc_alias_value(
639-
v,
640-
s.as_str(),
641-
hir_id,
642-
target,
643-
true,
644-
aliases,
645-
) {
641+
if !self.check_doc_alias_value(v, s, hir_id, target, true, aliases) {
646642
errors += 1;
647643
}
648644
}
@@ -670,8 +666,8 @@ impl CheckAttrVisitor<'_> {
670666
}
671667
}
672668
errors == 0
673-
} else if let Some(doc_alias) = meta.value_str().map(|s| s.to_string()) {
674-
self.check_doc_alias_value(meta, &doc_alias, hir_id, target, false, aliases)
669+
} else if let Some(doc_alias) = meta.value_str() {
670+
self.check_doc_alias_value(meta, doc_alias, hir_id, target, false, aliases)
675671
} else {
676672
self.tcx
677673
.sess
@@ -686,8 +682,8 @@ impl CheckAttrVisitor<'_> {
686682
}
687683

688684
fn check_doc_keyword(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
689-
let doc_keyword = meta.value_str().map(|s| s.to_string()).unwrap_or_else(String::new);
690-
if doc_keyword.is_empty() {
685+
let doc_keyword = meta.value_str().unwrap_or(kw::Empty);
686+
if doc_keyword == kw::Empty {
691687
self.doc_attr_str_error(meta, "keyword");
692688
return false;
693689
}
@@ -718,7 +714,7 @@ impl CheckAttrVisitor<'_> {
718714
return false;
719715
}
720716
}
721-
if !rustc_lexer::is_ident(&doc_keyword) {
717+
if !rustc_lexer::is_ident(doc_keyword.as_str()) {
722718
self.tcx
723719
.sess
724720
.struct_span_err(
@@ -911,20 +907,20 @@ impl CheckAttrVisitor<'_> {
911907
) -> bool {
912908
let mut is_valid = true;
913909

914-
if let Some(list) = attr.meta().and_then(|mi| mi.meta_item_list().map(|l| l.to_vec())) {
915-
for meta in &list {
910+
if let Some(mi) = attr.meta() && let Some(list) = mi.meta_item_list() {
911+
for meta in list {
916912
if let Some(i_meta) = meta.meta_item() {
917913
match i_meta.name_or_empty() {
918914
sym::alias
919-
if !self.check_attr_not_crate_level(&meta, hir_id, "alias")
920-
|| !self.check_doc_alias(&meta, hir_id, target, aliases) =>
915+
if !self.check_attr_not_crate_level(meta, hir_id, "alias")
916+
|| !self.check_doc_alias(meta, hir_id, target, aliases) =>
921917
{
922918
is_valid = false
923919
}
924920

925921
sym::keyword
926-
if !self.check_attr_not_crate_level(&meta, hir_id, "keyword")
927-
|| !self.check_doc_keyword(&meta, hir_id) =>
922+
if !self.check_attr_not_crate_level(meta, hir_id, "keyword")
923+
|| !self.check_doc_keyword(meta, hir_id) =>
928924
{
929925
is_valid = false
930926
}
@@ -936,15 +932,15 @@ impl CheckAttrVisitor<'_> {
936932
| sym::html_root_url
937933
| sym::html_no_source
938934
| sym::test
939-
if !self.check_attr_crate_level(&attr, &meta, hir_id) =>
935+
if !self.check_attr_crate_level(attr, meta, hir_id) =>
940936
{
941937
is_valid = false;
942938
}
943939

944940
sym::inline | sym::no_inline
945941
if !self.check_doc_inline(
946-
&attr,
947-
&meta,
942+
attr,
943+
meta,
948944
hir_id,
949945
target,
950946
specified_inline,
@@ -976,7 +972,7 @@ impl CheckAttrVisitor<'_> {
976972
| sym::plugins => {}
977973

978974
sym::test => {
979-
if !self.check_test_attr(&meta, hir_id) {
975+
if !self.check_test_attr(meta, hir_id) {
980976
is_valid = false;
981977
}
982978
}

Diff for: compiler/rustc_passes/src/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
696696
hir::ItemKind::ExternCrate(_) => {
697697
// compiler-generated `extern crate` items have a dummy span.
698698
// `std` is still checked for the `restricted-std` feature.
699-
if item.span.is_dummy() && item.ident.as_str() != "std" {
699+
if item.span.is_dummy() && item.ident.name != sym::std {
700700
return;
701701
}
702702

Diff for: compiler/rustc_resolve/src/late/lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ fn get_lifetime_scopes_for_path(mut scope: &Scope<'_>) -> LifetimeScopeForPath {
587587
match scope {
588588
Scope::Binder { lifetimes, s, .. } => {
589589
available_lifetimes.extend(lifetimes.keys().filter_map(|p| match p {
590-
hir::ParamName::Plain(ident) => Some(ident.name.to_string()),
590+
hir::ParamName::Plain(ident) => Some(ident.name),
591591
_ => None,
592592
}));
593593
scope = s;

Diff for: compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
497497
param_names
498498
.iter()
499499
.take(num_params_to_take)
500-
.map(|p| (*p).clone())
500+
.map(|p| p.as_str())
501501
.collect::<Vec<_>>()
502502
.join(", ")
503503
} else {

0 commit comments

Comments
 (0)