Skip to content

Commit f6c0909

Browse files
committed
Don't unnecessarily clone the input tt for decl macros
1 parent d5f64f8 commit f6c0909

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

crates/hir-expand/src/db.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct DeclarativeMacroExpander {
3535
}
3636

3737
impl DeclarativeMacroExpander {
38-
pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
38+
pub fn expand(&self, tt: tt::Subtree) -> ExpandResult<tt::Subtree> {
3939
match self.mac.err() {
4040
Some(e) => ExpandResult::new(
4141
tt::Subtree::empty(),
@@ -44,6 +44,14 @@ impl DeclarativeMacroExpander {
4444
None => self.mac.expand(tt).map_err(Into::into),
4545
}
4646
}
47+
48+
pub fn map_id_down(&self, token_id: tt::TokenId) -> tt::TokenId {
49+
self.mac.map_id_down(token_id)
50+
}
51+
52+
pub fn map_id_up(&self, token_id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
53+
self.mac.map_id_up(token_id)
54+
}
4755
}
4856

4957
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -61,10 +69,11 @@ pub enum TokenExpander {
6169
ProcMacro(ProcMacroExpander),
6270
}
6371

72+
// FIXME: Get rid of these methods
6473
impl TokenExpander {
6574
pub(crate) fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
6675
match self {
67-
TokenExpander::DeclarativeMacro(expander) => expander.mac.map_id_down(id),
76+
TokenExpander::DeclarativeMacro(expander) => expander.map_id_down(id),
6877
TokenExpander::BuiltIn(..)
6978
| TokenExpander::BuiltInEager(..)
7079
| TokenExpander::BuiltInAttr(..)
@@ -75,7 +84,7 @@ impl TokenExpander {
7584

7685
pub(crate) fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
7786
match self {
78-
TokenExpander::DeclarativeMacro(expander) => expander.mac.map_id_up(id),
87+
TokenExpander::DeclarativeMacro(expander) => expander.map_id_up(id),
7988
TokenExpander::BuiltIn(..)
8089
| TokenExpander::BuiltInEager(..)
8190
| TokenExpander::BuiltInAttr(..)
@@ -167,7 +176,6 @@ pub fn expand_speculative(
167176
token_to_map: SyntaxToken,
168177
) -> Option<(SyntaxNode, SyntaxToken)> {
169178
let loc = db.lookup_intern_macro_call(actual_macro_call);
170-
let macro_def = db.macro_def(loc.def);
171179
let token_range = token_to_map.text_range();
172180

173181
// Build the subtree and token mapping for the speculative args
@@ -225,7 +233,12 @@ pub fn expand_speculative(
225233
None => {
226234
let range = token_range.checked_sub(speculative_args.text_range().start())?;
227235
let token_id = spec_args_tmap.token_by_range(range)?;
228-
macro_def.map_id_down(token_id)
236+
match loc.def.kind {
237+
MacroDefKind::Declarative(it) => {
238+
db.decl_macro_expander(loc.krate, it).map_id_down(token_id)
239+
}
240+
_ => token_id,
241+
}
229242
}
230243
};
231244

@@ -244,7 +257,7 @@ pub fn expand_speculative(
244257
let adt = ast::Adt::cast(speculative_args.clone()).unwrap();
245258
expander.expand(db, actual_macro_call, &adt, &spec_args_tmap)
246259
}
247-
MacroDefKind::Declarative(it) => db.decl_macro_expander(loc.krate, it).expand(&tt),
260+
MacroDefKind::Declarative(it) => db.decl_macro_expander(loc.krate, it).expand(tt),
248261
MacroDefKind::BuiltIn(it, _) => it.expand(db, actual_macro_call, &tt).map_err(Into::into),
249262
MacroDefKind::BuiltInEager(it, _) => {
250263
it.expand(db, actual_macro_call, &tt).map_err(Into::into)
@@ -518,7 +531,7 @@ fn macro_expand(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<Arc<tt
518531
let (arg, arg_tm, undo_info) = &*macro_arg;
519532
let mut res = match loc.def.kind {
520533
MacroDefKind::Declarative(id) => {
521-
db.decl_macro_expander(loc.def.krate, id).expand(&arg)
534+
db.decl_macro_expander(loc.def.krate, id).expand(arg.clone())
522535
}
523536
MacroDefKind::BuiltIn(it, _) => it.expand(db, id, &arg).map_err(Into::into),
524537
MacroDefKind::BuiltInEager(it, _) => it.expand(db, id, &arg).map_err(Into::into),

crates/mbe/src/benchmark.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn benchmark_expand_macro_rules() {
3838
invocations
3939
.into_iter()
4040
.map(|(id, tt)| {
41-
let res = rules[&id].expand(&tt);
41+
let res = rules[&id].expand(tt);
4242
assert!(res.err.is_none());
4343
res.value.token_trees.len()
4444
})
@@ -102,7 +102,7 @@ fn invocation_fixtures(rules: &FxHashMap<String, DeclarativeMacro>) -> Vec<(Stri
102102
for op in rule.lhs.iter() {
103103
collect_from_op(op, &mut subtree, &mut seed);
104104
}
105-
if it.expand(&subtree).err.is_none() {
105+
if it.expand(subtree.clone()).err.is_none() {
106106
res.push((name.clone(), subtree));
107107
break;
108108
}

crates/mbe/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,7 @@ impl DeclarativeMacro {
311311
DeclarativeMacro { rules: rules.into_boxed_slice(), shift: Shift::new(tt), is_2021, err }
312312
}
313313

314-
pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
315-
// apply shift
316-
let mut tt = tt.clone();
314+
pub fn expand(&self, mut tt: tt::Subtree) -> ExpandResult<tt::Subtree> {
317315
self.shift.shift_all(&mut tt);
318316
expander::expand_rules(&self.rules, &tt, self.is_2021)
319317
}

0 commit comments

Comments
 (0)