Skip to content

Commit 8720281

Browse files
bors[bot]Veykril
andauthored
Merge #10696
10696: internal: Replace more Name::to_string usages with Name::to_smol_str r=Veykril a=Veykril Gets rid of some more unnecessary string allocs bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 962be38 + 439a819 commit 8720281

File tree

24 files changed

+49
-41
lines changed

24 files changed

+49
-41
lines changed

crates/hir/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl Module {
580580
});
581581
for token in tokens {
582582
if token.kind() == SyntaxKind::IDENT
583-
&& token.text() == derive_name.as_str()
583+
&& token.text() == &**derive_name
584584
{
585585
precise_location = Some(token.text_range());
586586
break 'outer;
@@ -606,7 +606,12 @@ impl Module {
606606
}
607607
};
608608
acc.push(
609-
UnresolvedProcMacro { node, precise_location, macro_name: name }.into(),
609+
UnresolvedProcMacro {
610+
node,
611+
precise_location,
612+
macro_name: name.map(Into::into),
613+
}
614+
.into(),
610615
);
611616
}
612617

@@ -2219,7 +2224,7 @@ impl Impl {
22192224
.attrs()
22202225
.filter_map(|it| {
22212226
let path = ModPath::from_src(db.upcast(), it.path()?, &hygenic)?;
2222-
if path.as_ident()?.to_string() == "derive" {
2227+
if path.as_ident()?.to_smol_str() == "derive" {
22232228
Some(it)
22242229
} else {
22252230
None

crates/hir_def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl<'a> AttrQuery<'a> {
796796
let key = self.key;
797797
self.attrs
798798
.iter()
799-
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_string() == key))
799+
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_smol_str() == key))
800800
}
801801
}
802802

crates/hir_def/src/body/scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ mod tests {
282282
let actual = scopes
283283
.scope_chain(scope)
284284
.flat_map(|scope| scopes.entries(scope))
285-
.map(|it| it.name().to_string())
285+
.map(|it| it.name().to_smol_str())
286286
.collect::<Vec<_>>()
287287
.join("\n");
288288
let expected = expected.join("\n");

crates/hir_def/src/item_tree/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl<'a> Ctx<'a> {
449449

450450
fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
451451
let mut name = konst.name().map(|it| it.as_name());
452-
if name.as_ref().map_or(false, |n| n.to_string().starts_with("_DERIVE_")) {
452+
if name.as_ref().map_or(false, |n| n.to_smol_str().starts_with("_DERIVE_")) {
453453
// FIXME: this is a hack to treat consts generated by synstructure as unnamed
454454
// remove this some time in the future
455455
name = None;

crates/hir_def/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ fn derive_macro_as_call_id(
764764
krate,
765765
MacroCallKind::Derive {
766766
ast_id: item_attr.ast_id,
767-
derive_name: last_segment.to_string(),
767+
derive_name: last_segment.to_string().into_boxed_str(),
768768
derive_attr_index: derive_attr.ast_index,
769769
},
770770
);
@@ -801,7 +801,7 @@ fn attr_macro_as_call_id(
801801
krate,
802802
MacroCallKind::Attr {
803803
ast_id: item_attr.ast_id,
804-
attr_name: last_segment.to_string(),
804+
attr_name: last_segment.to_string().into_boxed_str(),
805805
attr_args: arg,
806806
invoc_attr_index: macro_attr.id.ast_index,
807807
},

crates/hir_def/src/nameres/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,7 @@ impl ModCollector<'_, '_> {
17591759
fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
17601760
if path.kind == PathKind::Plain {
17611761
if let Some(tool_module) = path.segments().first() {
1762-
let tool_module = tool_module.to_string();
1762+
let tool_module = tool_module.to_smol_str();
17631763
let is_tool = builtin_attr::TOOL_MODULES
17641764
.iter()
17651765
.copied()
@@ -1771,7 +1771,7 @@ impl ModCollector<'_, '_> {
17711771
}
17721772

17731773
if let Some(name) = path.as_ident() {
1774-
let name = name.to_string();
1774+
let name = name.to_smol_str();
17751775
let is_inert = builtin_attr::INERT_ATTRIBUTES
17761776
.iter()
17771777
.chain(builtin_attr::EXTRA_ATTRIBUTES)

crates/hir_def/src/nameres/mod_resolution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl ModDir {
4242
let path = match attr_path.map(|it| it.as_str()) {
4343
None => {
4444
let mut path = self.dir_path.clone();
45-
path.push(&name.to_string());
45+
path.push(&name.to_smol_str());
4646
path
4747
}
4848
Some(attr_path) => {

crates/hir_def/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Display for ImportAlias {
4646
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4747
match self {
4848
ImportAlias::Underscore => f.write_str("_"),
49-
ImportAlias::Alias(name) => f.write_str(&name.to_string()),
49+
ImportAlias::Alias(name) => f.write_str(&name.to_smol_str()),
5050
}
5151
}
5252
}

crates/hir_expand/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub enum MacroCallKind {
122122
},
123123
Derive {
124124
ast_id: AstId<ast::Item>,
125-
derive_name: String,
125+
derive_name: Box<str>,
126126
/// Syntactical index of the invoking `#[derive]` attribute.
127127
///
128128
/// Outer attributes are counted first, then inner attributes. This does not support
@@ -131,7 +131,7 @@ pub enum MacroCallKind {
131131
},
132132
Attr {
133133
ast_id: AstId<ast::Item>,
134-
attr_name: String,
134+
attr_name: Box<str>,
135135
attr_args: (tt::Subtree, mbe::TokenMap),
136136
/// Syntactical index of the invoking `#[attribute]`.
137137
///

crates/hir_expand/src/name.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ impl Name {
8484
}
8585

8686
/// Returns the textual representation of this name as a [`SmolStr`].
87-
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper.
87+
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
88+
/// the general case.
8889
pub fn to_smol_str(&self) -> SmolStr {
8990
match &self.0 {
9091
Repr::Text(it) => it.clone(),

0 commit comments

Comments
 (0)