Skip to content

Commit 4ba6096

Browse files
committed
Tweak NamedMatch representation.
The `Lrc` isn't necessary, neither is the `SmallVec`. Performance is changed negligibly, but the new code is simpler.
1 parent 482b25b commit 4ba6096

File tree

1 file changed

+7
-27
lines changed

1 file changed

+7
-27
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,12 @@ use rustc_session::parse::ParseSess;
8181
use rustc_span::symbol::MacroRulesNormalizedIdent;
8282
use rustc_span::Span;
8383

84-
use smallvec::{smallvec, SmallVec};
85-
8684
use rustc_data_structures::fx::FxHashMap;
8785
use rustc_data_structures::sync::Lrc;
8886
use rustc_span::symbol::Ident;
8987
use std::borrow::Cow;
9088
use std::collections::hash_map::Entry::{Occupied, Vacant};
9189

92-
// One element is enough to cover 95-99% of vectors for most benchmarks. Also, vectors longer than
93-
// one frequently have many elements, not just two or three.
94-
type NamedMatchVec = SmallVec<[NamedMatch; 1]>;
95-
96-
// This type is used a lot. Make sure it doesn't unintentionally get bigger.
97-
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
98-
rustc_data_structures::static_assert_size!(NamedMatchVec, 48);
99-
10090
/// A unit within a matcher that a `MatcherPos` can refer to. Similar to (and derived from)
10191
/// `mbe::TokenTree`, but designed specifically for fast and easy traversal during matching.
10292
/// Notable differences to `mbe::TokenTree`:
@@ -221,7 +211,7 @@ struct MatcherPos {
221211
/// with one element per metavar decl in the matcher. Each element records token trees matched
222212
/// against the relevant metavar by the black box parser. An element will be a `MatchedSeq` if
223213
/// the corresponding metavar decl is within a sequence.
224-
matches: Lrc<NamedMatchVec>,
214+
matches: Lrc<Vec<NamedMatch>>,
225215
}
226216

227217
// This type is used a lot. Make sure it doesn't unintentionally get bigger.
@@ -246,18 +236,12 @@ impl MatcherPos {
246236
let mut curr = &mut matches[metavar_idx];
247237
for _ in 0..seq_depth - 1 {
248238
match curr {
249-
MatchedSeq(seq) => {
250-
let seq = Lrc::make_mut(seq);
251-
curr = seq.last_mut().unwrap();
252-
}
239+
MatchedSeq(seq) => curr = seq.last_mut().unwrap(),
253240
_ => unreachable!(),
254241
}
255242
}
256243
match curr {
257-
MatchedSeq(seq) => {
258-
let seq = Lrc::make_mut(seq);
259-
seq.push(m);
260-
}
244+
MatchedSeq(seq) => seq.push(m),
261245
_ => unreachable!(),
262246
}
263247
}
@@ -350,7 +334,7 @@ pub(super) fn count_metavar_decls(matcher: &[TokenTree]) -> usize {
350334
/// ```
351335
#[derive(Debug, Clone)]
352336
crate enum NamedMatch {
353-
MatchedSeq(Lrc<NamedMatchVec>),
337+
MatchedSeq(Vec<NamedMatch>),
354338

355339
// A metavar match of type `tt`.
356340
MatchedTokenTree(rustc_ast::tokenstream::TokenTree),
@@ -388,7 +372,7 @@ pub struct TtParser {
388372

389373
/// Pre-allocate an empty match array, so it can be cloned cheaply for macros with many rules
390374
/// that have no metavars.
391-
empty_matches: Lrc<NamedMatchVec>,
375+
empty_matches: Lrc<Vec<NamedMatch>>,
392376
}
393377

394378
impl TtParser {
@@ -398,7 +382,7 @@ impl TtParser {
398382
cur_mps: vec![],
399383
next_mps: vec![],
400384
bb_mps: vec![],
401-
empty_matches: Lrc::new(smallvec![]),
385+
empty_matches: Lrc::new(vec![]),
402386
}
403387
}
404388

@@ -452,11 +436,7 @@ impl TtParser {
452436
} => {
453437
// Install an empty vec for each metavar within the sequence.
454438
for metavar_idx in next_metavar..next_metavar + num_metavar_decls {
455-
mp.push_match(
456-
metavar_idx,
457-
seq_depth,
458-
MatchedSeq(self.empty_matches.clone()),
459-
);
439+
mp.push_match(metavar_idx, seq_depth, MatchedSeq(vec![]));
460440
}
461441

462442
if op == KleeneOp::ZeroOrMore || op == KleeneOp::ZeroOrOne {

0 commit comments

Comments
 (0)