1
Fork 0

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.
This commit is contained in:
Nicholas Nethercote 2018-11-01 05:35:20 +11:00
parent d586d5d2f5
commit c60ed5d22c

View file

@ -168,7 +168,7 @@ struct MatcherPos<'a> {
/// all bound matches from the submatcher into the shared top-level `matches` vector. If `sep` /// all bound matches from the submatcher into the shared top-level `matches` vector. If `sep`
/// and `up` are `Some`, then `matches` is _not_ the shared top-level list. Instead, if one /// and `up` are `Some`, then `matches` is _not_ the shared top-level list. Instead, if one
/// wants the shared `matches`, one should use `up.matches`. /// wants the shared `matches`, one should use `up.matches`.
matches: Vec<Rc<Vec<NamedMatch>>>, matches: Box<[Rc<Vec<NamedMatch>>]>,
/// The position in `matches` corresponding to the first metavar in this matcher's sequence of /// The position in `matches` corresponding to the first metavar in this matcher's sequence of
/// token trees. In other words, the first metavar in the first token of `top_elts` corresponds /// token trees. In other words, the first metavar in the first token of `top_elts` corresponds
/// to `matches[match_lo]`. /// to `matches[match_lo]`.
@ -278,9 +278,14 @@ pub fn count_names(ms: &[TokenTree]) -> usize {
}) })
} }
/// Initialize `len` empty shared `Vec`s to be used to store matches of metavars. /// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
fn create_matches(len: usize) -> Vec<Rc<Vec<NamedMatch>>> { fn create_matches(len: usize) -> Box<[Rc<Vec<NamedMatch>>]> {
(0..len).into_iter().map(|_| Rc::new(Vec::new())).collect() if len == 0 {
vec![]
} else {
let empty_matches = Rc::new(Vec::new());
vec![empty_matches.clone(); len]
}.into_boxed_slice()
} }
/// Generate the top-level matcher position in which the "dot" is before the first token of the /// Generate the top-level matcher position in which the "dot" is before the first token of the