Skip to content

Commit 23f3174

Browse files
committed
Auto merge of #67340 - nnethercote:shrink-Nonterminal, r=<try>
Shrink `Nonterminal` These commits shrink `Nonterminal` from 240 bytes to 40 bytes. When building `serde_derive` they reduce the number of `memcpy` calls from 9.6M to 7.4M, and it's a tiny win on a few other benchmarks. r? @petrochenkov
2 parents a605441 + ed9ba9b commit 23f3174

File tree

14 files changed

+137
-116
lines changed

14 files changed

+137
-116
lines changed

src/librustc_interface/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
707707
self.run(is_const, |s| noop_visit_item_kind(i, s))
708708
}
709709

710-
fn flat_map_trait_item(&mut self, i: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
710+
fn flat_map_trait_item(&mut self, i: P<ast::TraitItem>) -> SmallVec<[P<ast::TraitItem>; 1]> {
711711
let is_const = match i.kind {
712712
ast::TraitItemKind::Const(..) => true,
713713
ast::TraitItemKind::Method(ref sig, _) => Self::is_sig_const(sig),
@@ -716,7 +716,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
716716
self.run(is_const, |s| noop_flat_map_trait_item(i, s))
717717
}
718718

719-
fn flat_map_impl_item(&mut self, i: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
719+
fn flat_map_impl_item(&mut self, i: P<ast::ImplItem>) -> SmallVec<[P<ast::ImplItem>; 1]> {
720720
let is_const = match i.kind {
721721
ast::ImplItemKind::Const(..) => true,
722722
ast::ImplItemKind::Method(ref sig, _) => Self::is_sig_const(sig),

src/librustc_parse/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,11 @@ impl<'a> MutVisitor for StripUnconfigured<'a> {
344344
noop_flat_map_item(configure!(self, item), self)
345345
}
346346

347-
fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
347+
fn flat_map_impl_item(&mut self, item: P<ast::ImplItem>) -> SmallVec<[P<ast::ImplItem>; 1]> {
348348
noop_flat_map_impl_item(configure!(self, item), self)
349349
}
350350

351-
fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
351+
fn flat_map_trait_item(&mut self, item: P<ast::TraitItem>) -> SmallVec<[P<ast::TraitItem>; 1]> {
352352
noop_flat_map_trait_item(configure!(self, item), self)
353353
}
354354

src/librustc_parse/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'a> Parser<'a> {
170170
pub fn parse_attr_item(&mut self) -> PResult<'a, ast::AttrItem> {
171171
let item = match self.token.kind {
172172
token::Interpolated(ref nt) => match **nt {
173-
Nonterminal::NtMeta(ref item) => Some(item.clone()),
173+
Nonterminal::NtMeta(ref item) => Some(item.clone().into_inner()),
174174
_ => None,
175175
},
176176
_ => None,

src/librustc_parse/parser/item.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ impl<'a> Parser<'a> {
648648
Ok((Ident::invalid(), item_kind, Some(attrs)))
649649
}
650650

651-
fn parse_impl_body(&mut self) -> PResult<'a, (Vec<ImplItem>, Vec<Attribute>)> {
651+
fn parse_impl_body(&mut self) -> PResult<'a, (Vec<P<ImplItem>>, Vec<Attribute>)> {
652652
self.expect(&token::OpenDelim(token::Brace))?;
653653
let attrs = self.parse_inner_attributes()?;
654654

@@ -670,7 +670,7 @@ impl<'a> Parser<'a> {
670670
}
671671

672672
/// Parses an impl item.
673-
pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, ImplItem> {
673+
pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, P<ImplItem>> {
674674
maybe_whole!(self, NtImplItem, |x| x);
675675
let attrs = self.parse_outer_attributes()?;
676676
let mut unclosed_delims = vec![];
@@ -685,7 +685,7 @@ impl<'a> Parser<'a> {
685685
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
686686
item.tokens = Some(tokens);
687687
}
688-
Ok(item)
688+
Ok(P(item))
689689
}
690690

691691
fn parse_impl_item_(
@@ -858,7 +858,7 @@ impl<'a> Parser<'a> {
858858
}
859859

860860
/// Parses the items in a trait declaration.
861-
pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, TraitItem> {
861+
pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, P<TraitItem>> {
862862
maybe_whole!(self, NtTraitItem, |x| x);
863863
let attrs = self.parse_outer_attributes()?;
864864
let mut unclosed_delims = vec![];
@@ -872,7 +872,7 @@ impl<'a> Parser<'a> {
872872
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
873873
item.tokens = Some(tokens);
874874
}
875-
Ok(item)
875+
Ok(P(item))
876876
}
877877

878878
fn parse_trait_item_(
@@ -1123,7 +1123,7 @@ impl<'a> Parser<'a> {
11231123
}
11241124

11251125
/// Parses a foreign item.
1126-
pub fn parse_foreign_item(&mut self, extern_sp: Span) -> PResult<'a, ForeignItem> {
1126+
pub fn parse_foreign_item(&mut self, extern_sp: Span) -> PResult<'a, P<ForeignItem>> {
11271127
maybe_whole!(self, NtForeignItem, |ni| ni);
11281128

11291129
let attrs = self.parse_outer_attributes()?;
@@ -1173,7 +1173,7 @@ impl<'a> Parser<'a> {
11731173

11741174
match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? {
11751175
Some(mac) => {
1176-
Ok(
1176+
Ok(P(
11771177
ForeignItem {
11781178
ident: Ident::invalid(),
11791179
span: lo.to(self.prev_span),
@@ -1183,7 +1183,7 @@ impl<'a> Parser<'a> {
11831183
kind: ForeignItemKind::Macro(mac),
11841184
tokens: None,
11851185
}
1186-
)
1186+
))
11871187
}
11881188
None => {
11891189
if !attrs.is_empty() {
@@ -1198,41 +1198,41 @@ impl<'a> Parser<'a> {
11981198
/// Parses a static item from a foreign module.
11991199
/// Assumes that the `static` keyword is already parsed.
12001200
fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
1201-
-> PResult<'a, ForeignItem> {
1201+
-> PResult<'a, P<ForeignItem>> {
12021202
let mutbl = self.parse_mutability();
12031203
let ident = self.parse_ident()?;
12041204
self.expect(&token::Colon)?;
12051205
let ty = self.parse_ty()?;
12061206
let hi = self.token.span;
12071207
self.expect_semi()?;
1208-
Ok(ForeignItem {
1208+
Ok(P(ForeignItem {
12091209
ident,
12101210
attrs,
12111211
kind: ForeignItemKind::Static(ty, mutbl),
12121212
id: DUMMY_NODE_ID,
12131213
span: lo.to(hi),
12141214
vis,
12151215
tokens: None,
1216-
})
1216+
}))
12171217
}
12181218

12191219
/// Parses a type from a foreign module.
12201220
fn parse_item_foreign_type(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
1221-
-> PResult<'a, ForeignItem> {
1221+
-> PResult<'a, P<ForeignItem>> {
12221222
self.expect_keyword(kw::Type)?;
12231223

12241224
let ident = self.parse_ident()?;
12251225
let hi = self.token.span;
12261226
self.expect_semi()?;
1227-
Ok(ast::ForeignItem {
1227+
Ok(P(ast::ForeignItem {
12281228
ident,
12291229
attrs,
12301230
kind: ForeignItemKind::Ty,
12311231
id: DUMMY_NODE_ID,
12321232
span: lo.to(hi),
12331233
vis,
12341234
tokens: None,
1235-
})
1235+
}))
12361236
}
12371237

12381238
fn is_static_global(&mut self) -> bool {
@@ -1813,7 +1813,7 @@ impl<'a> Parser<'a> {
18131813
lo: Span,
18141814
attrs: Vec<Attribute>,
18151815
extern_sp: Span,
1816-
) -> PResult<'a, ForeignItem> {
1816+
) -> PResult<'a, P<ForeignItem>> {
18171817
self.expect_keyword(kw::Fn)?;
18181818
let (ident, decl, generics) = self.parse_fn_sig(ParamCfg {
18191819
is_self_allowed: false,
@@ -1822,15 +1822,15 @@ impl<'a> Parser<'a> {
18221822
})?;
18231823
let span = lo.to(self.token.span);
18241824
self.parse_semi_or_incorrect_foreign_fn_body(&ident, extern_sp)?;
1825-
Ok(ast::ForeignItem {
1825+
Ok(P(ast::ForeignItem {
18261826
ident,
18271827
attrs,
18281828
kind: ForeignItemKind::Fn(decl, generics),
18291829
id: DUMMY_NODE_ID,
18301830
span,
18311831
vis,
18321832
tokens: None,
1833-
})
1833+
}))
18341834
}
18351835

18361836
/// Parses a method or a macro invocation in a trait impl.

src/librustc_resolve/late.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
989989
/// When evaluating a `trait` use its associated types' idents for suggestionsa in E0412.
990990
fn with_trait_items<T>(
991991
&mut self,
992-
trait_items: &Vec<TraitItem>,
992+
trait_items: &Vec<P<TraitItem>>,
993993
f: impl FnOnce(&mut Self) -> T,
994994
) -> T {
995995
let trait_assoc_types = replace(
@@ -1063,7 +1063,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
10631063
opt_trait_reference: &Option<TraitRef>,
10641064
self_type: &Ty,
10651065
item_id: NodeId,
1066-
impl_items: &[ImplItem]) {
1066+
impl_items: &[P<ImplItem>]) {
10671067
debug!("resolve_implementation");
10681068
// If applicable, create a rib for the type parameters.
10691069
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {

src/librustc_save_analysis/dump_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
676676
generics: &'l ast::Generics,
677677
trait_ref: &'l Option<ast::TraitRef>,
678678
typ: &'l ast::Ty,
679-
impl_items: &'l [ast::ImplItem],
679+
impl_items: &'l [P<ast::ImplItem>],
680680
) {
681681
if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
682682
if !self.span.filter_generated(item.span) {
@@ -707,7 +707,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
707707
item: &'l ast::Item,
708708
generics: &'l ast::Generics,
709709
trait_refs: &'l ast::GenericBounds,
710-
methods: &'l [ast::TraitItem],
710+
methods: &'l [P<ast::TraitItem>],
711711
) {
712712
let name = item.ident.to_string();
713713
let qualname = format!("::{}",

src/libsyntax/ast.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ pub struct Mod {
22282228
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
22292229
pub struct ForeignMod {
22302230
pub abi: Option<StrLit>,
2231-
pub items: Vec<ForeignItem>,
2231+
pub items: Vec<P<ForeignItem>>,
22322232
}
22332233

22342234
/// Global inline assembly.
@@ -2602,7 +2602,7 @@ pub enum ItemKind {
26022602
/// A trait declaration (`trait`).
26032603
///
26042604
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
2605-
Trait(IsAuto, Unsafety, Generics, GenericBounds, Vec<TraitItem>),
2605+
Trait(IsAuto, Unsafety, Generics, GenericBounds, Vec<P<TraitItem>>),
26062606
/// Trait alias
26072607
///
26082608
/// E.g., `trait Foo = Bar + Quux;`.
@@ -2617,7 +2617,7 @@ pub enum ItemKind {
26172617
Generics,
26182618
Option<TraitRef>, // (optional) trait this impl implements
26192619
P<Ty>, // self
2620-
Vec<ImplItem>,
2620+
Vec<P<ImplItem>>,
26212621
),
26222622
/// A macro invocation.
26232623
///

src/libsyntax/mut_visit.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub trait MutVisitor: Sized {
8383
noop_visit_use_tree(use_tree, self);
8484
}
8585

86-
fn flat_map_foreign_item(&mut self, ni: ForeignItem) -> SmallVec<[ForeignItem; 1]> {
86+
fn flat_map_foreign_item(&mut self, ni: P<ForeignItem>) -> SmallVec<[P<ForeignItem>; 1]> {
8787
noop_flat_map_foreign_item(ni, self)
8888
}
8989

@@ -103,11 +103,11 @@ pub trait MutVisitor: Sized {
103103
noop_visit_item_kind(i, self);
104104
}
105105

106-
fn flat_map_trait_item(&mut self, i: TraitItem) -> SmallVec<[TraitItem; 1]> {
106+
fn flat_map_trait_item(&mut self, i: P<TraitItem>) -> SmallVec<[P<TraitItem>; 1]> {
107107
noop_flat_map_trait_item(i, self)
108108
}
109109

110-
fn flat_map_impl_item(&mut self, i: ImplItem) -> SmallVec<[ImplItem; 1]> {
110+
fn flat_map_impl_item(&mut self, i: P<ImplItem>) -> SmallVec<[P<ImplItem>; 1]> {
111111
noop_flat_map_impl_item(i, self)
112112
}
113113

@@ -701,7 +701,8 @@ pub fn noop_visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis:
701701
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
702702
token::NtLifetime(ident) => vis.visit_ident(ident),
703703
token::NtLiteral(expr) => vis.visit_expr(expr),
704-
token::NtMeta(AttrItem { path, args }) => {
704+
token::NtMeta(item) => {
705+
let AttrItem { path, args } = item.deref_mut();
705706
vis.visit_path(path);
706707
visit_mac_args(args, vis);
707708
}
@@ -936,10 +937,10 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
936937
}
937938
}
938939

939-
pub fn noop_flat_map_trait_item<T: MutVisitor>(mut item: TraitItem, visitor: &mut T)
940-
-> SmallVec<[TraitItem; 1]>
940+
pub fn noop_flat_map_trait_item<T: MutVisitor>(mut item: P<TraitItem>, visitor: &mut T)
941+
-> SmallVec<[P<TraitItem>; 1]>
941942
{
942-
let TraitItem { id, ident, vis, attrs, generics, kind, span, tokens: _ } = &mut item;
943+
let TraitItem { id, ident, vis, attrs, generics, kind, span, tokens: _ } = item.deref_mut();
943944
visitor.visit_id(id);
944945
visitor.visit_ident(ident);
945946
visitor.visit_vis(vis);
@@ -967,11 +968,11 @@ pub fn noop_flat_map_trait_item<T: MutVisitor>(mut item: TraitItem, visitor: &mu
967968
smallvec![item]
968969
}
969970

970-
pub fn noop_flat_map_impl_item<T: MutVisitor>(mut item: ImplItem, visitor: &mut T)
971-
-> SmallVec<[ImplItem; 1]>
971+
pub fn noop_flat_map_impl_item<T: MutVisitor>(mut item: P<ImplItem>, visitor: &mut T)
972+
-> SmallVec<[P<ImplItem>; 1]>
972973
{
973974
let ImplItem { id, ident, vis, defaultness: _, attrs, generics, kind, span, tokens: _ } =
974-
&mut item;
975+
item.deref_mut();
975976
visitor.visit_id(id);
976977
visitor.visit_ident(ident);
977978
visitor.visit_vis(vis);
@@ -1050,10 +1051,10 @@ pub fn noop_flat_map_item<T: MutVisitor>(mut item: P<Item>, visitor: &mut T)
10501051
smallvec![item]
10511052
}
10521053

1053-
pub fn noop_flat_map_foreign_item<T: MutVisitor>(mut item: ForeignItem, visitor: &mut T)
1054-
-> SmallVec<[ForeignItem; 1]>
1054+
pub fn noop_flat_map_foreign_item<T: MutVisitor>(mut item: P<ForeignItem>, visitor: &mut T)
1055+
-> SmallVec<[P<ForeignItem>; 1]>
10551056
{
1056-
let ForeignItem { ident, attrs, id, kind, vis, span, tokens: _ } = &mut item;
1057+
let ForeignItem { ident, attrs, id, kind, vis, span, tokens: _ } = item.deref_mut();
10571058
visitor.visit_ident(ident);
10581059
visit_attrs(attrs, visitor);
10591060
match kind {

src/libsyntax/token.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -678,18 +678,22 @@ pub enum Nonterminal {
678678
NtLifetime(ast::Ident),
679679
NtLiteral(P<ast::Expr>),
680680
/// Stuff inside brackets for attributes
681-
NtMeta(ast::AttrItem),
681+
NtMeta(P<ast::AttrItem>),
682682
NtPath(ast::Path),
683683
NtVis(ast::Visibility),
684684
NtTT(TokenTree),
685685
// Used only for passing items to proc macro attributes (they are not
686686
// strictly necessary for that, `Annotatable` can be converted into
687687
// tokens directly, but doing that naively regresses pretty-printing).
688-
NtTraitItem(ast::TraitItem),
689-
NtImplItem(ast::ImplItem),
690-
NtForeignItem(ast::ForeignItem),
688+
NtTraitItem(P<ast::TraitItem>),
689+
NtImplItem(P<ast::ImplItem>),
690+
NtForeignItem(P<ast::ForeignItem>),
691691
}
692692

693+
// `Nonterminal` is used a lot. Make sure it doesn't unintentionally get bigger.
694+
#[cfg(target_arch = "x86_64")]
695+
rustc_data_structures::static_assert_size!(Nonterminal, 40);
696+
693697
impl PartialEq for Nonterminal {
694698
fn eq(&self, rhs: &Self) -> bool {
695699
match (self, rhs) {

0 commit comments

Comments
 (0)