Skip to content

Commit c60ed5d

Browse files
committed
Share empty Vecs more within MatcherPos::matches.
`create_matches` creates a `Vec<Rc<Vec<NamedMatch>>>`. Even though all the inner `Vec`s are empty, each one is created separately. This commit changes `create_matches` so it instead creates one empty inner `Vec`, and shares it. The commit also changes `MatcherPos::matches` to a boxed slice, because its length doesn't change.
1 parent d586d5d commit c60ed5d

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/libsyntax/ext/tt/macro_parser.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ struct MatcherPos<'a> {
168168
/// all bound matches from the submatcher into the shared top-level `matches` vector. If `sep`
169169
/// and `up` are `Some`, then `matches` is _not_ the shared top-level list. Instead, if one
170170
/// wants the shared `matches`, one should use `up.matches`.
171-
matches: Vec<Rc<Vec<NamedMatch>>>,
171+
matches: Box<[Rc<Vec<NamedMatch>>]>,
172172
/// The position in `matches` corresponding to the first metavar in this matcher's sequence of
173173
/// token trees. In other words, the first metavar in the first token of `top_elts` corresponds
174174
/// to `matches[match_lo]`.
@@ -278,9 +278,14 @@ pub fn count_names(ms: &[TokenTree]) -> usize {
278278
})
279279
}
280280

281-
/// Initialize `len` empty shared `Vec`s to be used to store matches of metavars.
282-
fn create_matches(len: usize) -> Vec<Rc<Vec<NamedMatch>>> {
283-
(0..len).into_iter().map(|_| Rc::new(Vec::new())).collect()
281+
/// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
282+
fn create_matches(len: usize) -> Box<[Rc<Vec<NamedMatch>>]> {
283+
if len == 0 {
284+
vec![]
285+
} else {
286+
let empty_matches = Rc::new(Vec::new());
287+
vec![empty_matches.clone(); len]
288+
}.into_boxed_slice()
284289
}
285290

286291
/// Generate the top-level matcher position in which the "dot" is before the first token of the

0 commit comments

Comments
 (0)