Skip to content

Commit 082b649

Browse files
committed
Avoid a clone() in transcribe().
The current code (expensively) clones the value within an `Rc`. This commit changes things so that the `Rc` itself is (cheaply) cloned instead, avoid some allocations. This requires converting a few `Rc` instances to `Lrc`.
1 parent 9cc100f commit 082b649

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ use smallvec::{smallvec, SmallVec};
8888
use syntax_pos::Span;
8989

9090
use rustc_data_structures::fx::FxHashMap;
91+
use rustc_data_structures::sync::Lrc;
9192
use std::collections::hash_map::Entry::{Occupied, Vacant};
9293
use std::mem;
9394
use std::ops::{Deref, DerefMut};
@@ -179,7 +180,7 @@ struct MatcherPos<'root, 'tt: 'root> {
179180
/// all bound matches from the submatcher into the shared top-level `matches` vector. If `sep`
180181
/// and `up` are `Some`, then `matches` is _not_ the shared top-level list. Instead, if one
181182
/// wants the shared `matches`, one should use `up.matches`.
182-
matches: Box<[Rc<NamedMatchVec>]>,
183+
matches: Box<[Lrc<NamedMatchVec>]>,
183184
/// The position in `matches` corresponding to the first metavar in this matcher's sequence of
184185
/// token trees. In other words, the first metavar in the first token of `top_elts` corresponds
185186
/// to `matches[match_lo]`.
@@ -218,7 +219,7 @@ struct MatcherPos<'root, 'tt: 'root> {
218219
impl<'root, 'tt> MatcherPos<'root, 'tt> {
219220
/// Adds `m` as a named match for the `idx`-th metavar.
220221
fn push_match(&mut self, idx: usize, m: NamedMatch) {
221-
let matches = Rc::make_mut(&mut self.matches[idx]);
222+
let matches = Lrc::make_mut(&mut self.matches[idx]);
222223
matches.push(m);
223224
}
224225
}
@@ -295,11 +296,11 @@ pub fn count_names(ms: &[TokenTree]) -> usize {
295296
}
296297

297298
/// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
298-
fn create_matches(len: usize) -> Box<[Rc<NamedMatchVec>]> {
299+
fn create_matches(len: usize) -> Box<[Lrc<NamedMatchVec>]> {
299300
if len == 0 {
300301
vec![]
301302
} else {
302-
let empty_matches = Rc::new(SmallVec::new());
303+
let empty_matches = Lrc::new(SmallVec::new());
303304
vec![empty_matches; len]
304305
}.into_boxed_slice()
305306
}
@@ -353,8 +354,8 @@ fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree], open: Span) -> MatcherP
353354
/// token tree it was derived from.
354355
#[derive(Debug, Clone)]
355356
pub enum NamedMatch {
356-
MatchedSeq(Rc<NamedMatchVec>, DelimSpan),
357-
MatchedNonterminal(Rc<Nonterminal>),
357+
MatchedSeq(Lrc<NamedMatchVec>, DelimSpan),
358+
MatchedNonterminal(Lrc<Nonterminal>),
358359
}
359360

360361
/// Takes a sequence of token trees `ms` representing a matcher which successfully matched input
@@ -561,7 +562,7 @@ fn inner_parse_loop<'root, 'tt>(
561562
new_item.match_cur += seq.num_captures;
562563
new_item.idx += 1;
563564
for idx in item.match_cur..item.match_cur + seq.num_captures {
564-
new_item.push_match(idx, MatchedSeq(Rc::new(smallvec![]), sp));
565+
new_item.push_match(idx, MatchedSeq(Lrc::new(smallvec![]), sp));
565566
}
566567
cur_items.push(new_item);
567568
}
@@ -707,7 +708,7 @@ pub fn parse(
707708
let matches = eof_items[0]
708709
.matches
709710
.iter_mut()
710-
.map(|dv| Rc::make_mut(dv).pop().unwrap());
711+
.map(|dv| Lrc::make_mut(dv).pop().unwrap());
711712
return nameize(sess, ms, matches);
712713
} else if eof_items.len() > 1 {
713714
return Error(
@@ -780,7 +781,7 @@ pub fn parse(
780781
let match_cur = item.match_cur;
781782
item.push_match(
782783
match_cur,
783-
MatchedNonterminal(Rc::new(parse_nt(&mut parser, span, &ident.as_str()))),
784+
MatchedNonterminal(Lrc::new(parse_nt(&mut parser, span, &ident.as_str()))),
784785
);
785786
item.idx += 1;
786787
item.match_cur += 1;

src/libsyntax/ext/tt/transcribe.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ pub fn transcribe(cx: &ExtCtxt<'_>,
149149
result.push(tt.clone().into());
150150
} else {
151151
sp = sp.apply_mark(cx.current_expansion.mark);
152-
let token =
153-
TokenTree::Token(sp, Token::Interpolated(Lrc::new((**nt).clone())));
152+
let token = TokenTree::Token(sp, Token::Interpolated(nt.clone()));
154153
result.push(token.into());
155154
}
156155
} else {

0 commit comments

Comments
 (0)