Skip to content

Commit 3182375

Browse files
committed
Auto merge of #81405 - bugadani:ast, r=cjgillot
Box the biggest ast::ItemKind variants This PR is a different approach on #81400, aiming to save memory in humongous ASTs. The three affected item kind enums are: - `ast::ItemKind` (208 -> 112 bytes) - `ast::AssocItemKind` (176 -> 72 bytes) - `ast::ForeignItemKind` (176 -> 72 bytes)
2 parents b81f581 + 003fba3 commit 3182375

File tree

34 files changed

+319
-204
lines changed

34 files changed

+319
-204
lines changed

compiler/rustc_ast/src/ast.rs

+64-35
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,36 @@ impl Default for FnHeader {
26552655
}
26562656
}
26572657

2658+
#[derive(Clone, Encodable, Decodable, Debug)]
2659+
pub struct TraitKind(
2660+
pub IsAuto,
2661+
pub Unsafe,
2662+
pub Generics,
2663+
pub GenericBounds,
2664+
pub Vec<P<AssocItem>>,
2665+
);
2666+
2667+
#[derive(Clone, Encodable, Decodable, Debug)]
2668+
pub struct TyAliasKind(pub Defaultness, pub Generics, pub GenericBounds, pub Option<P<Ty>>);
2669+
2670+
#[derive(Clone, Encodable, Decodable, Debug)]
2671+
pub struct ImplKind {
2672+
pub unsafety: Unsafe,
2673+
pub polarity: ImplPolarity,
2674+
pub defaultness: Defaultness,
2675+
pub constness: Const,
2676+
pub generics: Generics,
2677+
2678+
/// The trait being implemented, if any.
2679+
pub of_trait: Option<TraitRef>,
2680+
2681+
pub self_ty: P<Ty>,
2682+
pub items: Vec<P<AssocItem>>,
2683+
}
2684+
2685+
#[derive(Clone, Encodable, Decodable, Debug)]
2686+
pub struct FnKind(pub Defaultness, pub FnSig, pub Generics, pub Option<P<Block>>);
2687+
26582688
#[derive(Clone, Encodable, Decodable, Debug)]
26592689
pub enum ItemKind {
26602690
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
@@ -2676,7 +2706,7 @@ pub enum ItemKind {
26762706
/// A function declaration (`fn`).
26772707
///
26782708
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
2679-
Fn(Defaultness, FnSig, Generics, Option<P<Block>>),
2709+
Fn(Box<FnKind>),
26802710
/// A module declaration (`mod`).
26812711
///
26822712
/// E.g., `mod foo;` or `mod foo { .. }`.
@@ -2690,7 +2720,7 @@ pub enum ItemKind {
26902720
/// A type alias (`type`).
26912721
///
26922722
/// E.g., `type Foo = Bar<u8>;`.
2693-
TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>),
2723+
TyAlias(Box<TyAliasKind>),
26942724
/// An enum definition (`enum`).
26952725
///
26962726
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
@@ -2706,27 +2736,15 @@ pub enum ItemKind {
27062736
/// A trait declaration (`trait`).
27072737
///
27082738
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
2709-
Trait(IsAuto, Unsafe, Generics, GenericBounds, Vec<P<AssocItem>>),
2739+
Trait(Box<TraitKind>),
27102740
/// Trait alias
27112741
///
27122742
/// E.g., `trait Foo = Bar + Quux;`.
27132743
TraitAlias(Generics, GenericBounds),
27142744
/// An implementation.
27152745
///
27162746
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
2717-
Impl {
2718-
unsafety: Unsafe,
2719-
polarity: ImplPolarity,
2720-
defaultness: Defaultness,
2721-
constness: Const,
2722-
generics: Generics,
2723-
2724-
/// The trait being implemented, if any.
2725-
of_trait: Option<TraitRef>,
2726-
2727-
self_ty: P<Ty>,
2728-
items: Vec<P<AssocItem>>,
2729-
},
2747+
Impl(Box<ImplKind>),
27302748
/// A macro invocation.
27312749
///
27322750
/// E.g., `foo!(..)`.
@@ -2736,6 +2754,9 @@ pub enum ItemKind {
27362754
MacroDef(MacroDef),
27372755
}
27382756

2757+
#[cfg(target_arch = "x86_64")]
2758+
rustc_data_structures::static_assert_size!(ItemKind, 112);
2759+
27392760
impl ItemKind {
27402761
pub fn article(&self) -> &str {
27412762
use ItemKind::*;
@@ -2770,14 +2791,14 @@ impl ItemKind {
27702791

27712792
pub fn generics(&self) -> Option<&Generics> {
27722793
match self {
2773-
Self::Fn(_, _, generics, _)
2774-
| Self::TyAlias(_, generics, ..)
2794+
Self::Fn(box FnKind(_, _, generics, _))
2795+
| Self::TyAlias(box TyAliasKind(_, generics, ..))
27752796
| Self::Enum(_, generics)
27762797
| Self::Struct(_, generics)
27772798
| Self::Union(_, generics)
2778-
| Self::Trait(_, _, generics, ..)
2799+
| Self::Trait(box TraitKind(_, _, generics, ..))
27792800
| Self::TraitAlias(generics, _)
2780-
| Self::Impl { generics, .. } => Some(generics),
2801+
| Self::Impl(box ImplKind { generics, .. }) => Some(generics),
27812802
_ => None,
27822803
}
27832804
}
@@ -2800,17 +2821,22 @@ pub enum AssocItemKind {
28002821
/// If `def` is parsed, then the constant is provided, and otherwise required.
28012822
Const(Defaultness, P<Ty>, Option<P<Expr>>),
28022823
/// An associated function.
2803-
Fn(Defaultness, FnSig, Generics, Option<P<Block>>),
2824+
Fn(Box<FnKind>),
28042825
/// An associated type.
2805-
TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>),
2826+
TyAlias(Box<TyAliasKind>),
28062827
/// A macro expanding to associated items.
28072828
MacCall(MacCall),
28082829
}
28092830

2831+
#[cfg(target_arch = "x86_64")]
2832+
rustc_data_structures::static_assert_size!(AssocItemKind, 72);
2833+
28102834
impl AssocItemKind {
28112835
pub fn defaultness(&self) -> Defaultness {
28122836
match *self {
2813-
Self::Const(def, ..) | Self::Fn(def, ..) | Self::TyAlias(def, ..) => def,
2837+
Self::Const(def, ..)
2838+
| Self::Fn(box FnKind(def, ..))
2839+
| Self::TyAlias(box TyAliasKind(def, ..)) => def,
28142840
Self::MacCall(..) => Defaultness::Final,
28152841
}
28162842
}
@@ -2820,8 +2846,8 @@ impl From<AssocItemKind> for ItemKind {
28202846
fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
28212847
match assoc_item_kind {
28222848
AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
2823-
AssocItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d),
2824-
AssocItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d),
2849+
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
2850+
AssocItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
28252851
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
28262852
}
28272853
}
@@ -2833,8 +2859,8 @@ impl TryFrom<ItemKind> for AssocItemKind {
28332859
fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
28342860
Ok(match item_kind {
28352861
ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
2836-
ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d),
2837-
ItemKind::TyAlias(a, b, c, d) => AssocItemKind::TyAlias(a, b, c, d),
2862+
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
2863+
ItemKind::TyAlias(ty_alias_kind) => AssocItemKind::TyAlias(ty_alias_kind),
28382864
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
28392865
_ => return Err(item_kind),
28402866
})
@@ -2846,20 +2872,23 @@ impl TryFrom<ItemKind> for AssocItemKind {
28462872
pub enum ForeignItemKind {
28472873
/// A foreign static item (`static FOO: u8`).
28482874
Static(P<Ty>, Mutability, Option<P<Expr>>),
2849-
/// A foreign function.
2850-
Fn(Defaultness, FnSig, Generics, Option<P<Block>>),
2851-
/// A foreign type.
2852-
TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>),
2875+
/// An foreign function.
2876+
Fn(Box<FnKind>),
2877+
/// An foreign type.
2878+
TyAlias(Box<TyAliasKind>),
28532879
/// A macro expanding to foreign items.
28542880
MacCall(MacCall),
28552881
}
28562882

2883+
#[cfg(target_arch = "x86_64")]
2884+
rustc_data_structures::static_assert_size!(ForeignItemKind, 72);
2885+
28572886
impl From<ForeignItemKind> for ItemKind {
28582887
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
28592888
match foreign_item_kind {
28602889
ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
2861-
ForeignItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d),
2862-
ForeignItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d),
2890+
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
2891+
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
28632892
ForeignItemKind::MacCall(a) => ItemKind::MacCall(a),
28642893
}
28652894
}
@@ -2871,8 +2900,8 @@ impl TryFrom<ItemKind> for ForeignItemKind {
28712900
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
28722901
Ok(match item_kind {
28732902
ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
2874-
ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d),
2875-
ItemKind::TyAlias(a, b, c, d) => ForeignItemKind::TyAlias(a, b, c, d),
2903+
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
2904+
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
28762905
ItemKind::MacCall(a) => ForeignItemKind::MacCall(a),
28772906
_ => return Err(item_kind),
28782907
})

compiler/rustc_ast/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
test(attr(deny(warnings)))
1010
)]
1111
#![feature(box_syntax)]
12+
#![feature(box_patterns)]
1213
#![feature(const_fn)] // For the `transmute` in `P::new`
1314
#![feature(const_fn_transmute)]
1415
#![feature(const_panic)]

compiler/rustc_ast/src/mut_visit.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -912,15 +912,15 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
912912
vis.visit_ty(ty);
913913
visit_opt(expr, |expr| vis.visit_expr(expr));
914914
}
915-
ItemKind::Fn(_, sig, generics, body) => {
915+
ItemKind::Fn(box FnKind(_, sig, generics, body)) => {
916916
visit_fn_sig(sig, vis);
917917
vis.visit_generics(generics);
918918
visit_opt(body, |body| vis.visit_block(body));
919919
}
920920
ItemKind::Mod(m) => vis.visit_mod(m),
921921
ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm),
922922
ItemKind::GlobalAsm(_ga) => {}
923-
ItemKind::TyAlias(_, generics, bounds, ty) => {
923+
ItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
924924
vis.visit_generics(generics);
925925
visit_bounds(bounds, vis);
926926
visit_opt(ty, |ty| vis.visit_ty(ty));
@@ -933,7 +933,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
933933
vis.visit_variant_data(variant_data);
934934
vis.visit_generics(generics);
935935
}
936-
ItemKind::Impl {
936+
ItemKind::Impl(box ImplKind {
937937
unsafety: _,
938938
polarity: _,
939939
defaultness: _,
@@ -942,13 +942,13 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
942942
of_trait,
943943
self_ty,
944944
items,
945-
} => {
945+
}) => {
946946
vis.visit_generics(generics);
947947
visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref));
948948
vis.visit_ty(self_ty);
949949
items.flat_map_in_place(|item| vis.flat_map_impl_item(item));
950950
}
951-
ItemKind::Trait(_is_auto, _unsafety, generics, bounds, items) => {
951+
ItemKind::Trait(box TraitKind(.., generics, bounds, items)) => {
952952
vis.visit_generics(generics);
953953
visit_bounds(bounds, vis);
954954
items.flat_map_in_place(|item| vis.flat_map_trait_item(item));
@@ -976,12 +976,12 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
976976
visitor.visit_ty(ty);
977977
visit_opt(expr, |expr| visitor.visit_expr(expr));
978978
}
979-
AssocItemKind::Fn(_, sig, generics, body) => {
979+
AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => {
980980
visitor.visit_generics(generics);
981981
visit_fn_sig(sig, visitor);
982982
visit_opt(body, |body| visitor.visit_block(body));
983983
}
984-
AssocItemKind::TyAlias(_, generics, bounds, ty) => {
984+
AssocItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
985985
visitor.visit_generics(generics);
986986
visit_bounds(bounds, visitor);
987987
visit_opt(ty, |ty| visitor.visit_ty(ty));
@@ -1066,12 +1066,12 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
10661066
visitor.visit_ty(ty);
10671067
visit_opt(expr, |expr| visitor.visit_expr(expr));
10681068
}
1069-
ForeignItemKind::Fn(_, sig, generics, body) => {
1069+
ForeignItemKind::Fn(box FnKind(_, sig, generics, body)) => {
10701070
visitor.visit_generics(generics);
10711071
visit_fn_sig(sig, visitor);
10721072
visit_opt(body, |body| visitor.visit_block(body));
10731073
}
1074-
ForeignItemKind::TyAlias(_, generics, bounds, ty) => {
1074+
ForeignItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
10751075
visitor.visit_generics(generics);
10761076
visit_bounds(bounds, visitor);
10771077
visit_opt(ty, |ty| visitor.visit_ty(ty));

compiler/rustc_ast/src/visit.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
292292
visitor.visit_ty(typ);
293293
walk_list!(visitor, visit_expr, expr);
294294
}
295-
ItemKind::Fn(_, ref sig, ref generics, ref body) => {
295+
ItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body)) => {
296296
visitor.visit_generics(generics);
297297
let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
298298
visitor.visit_fn(kind, item.span, item.id)
@@ -302,7 +302,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
302302
walk_list!(visitor, visit_foreign_item, &foreign_module.items);
303303
}
304304
ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
305-
ItemKind::TyAlias(_, ref generics, ref bounds, ref ty) => {
305+
ItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref ty)) => {
306306
visitor.visit_generics(generics);
307307
walk_list!(visitor, visit_param_bound, bounds);
308308
walk_list!(visitor, visit_ty, ty);
@@ -311,7 +311,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
311311
visitor.visit_generics(generics);
312312
visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
313313
}
314-
ItemKind::Impl {
314+
ItemKind::Impl(box ImplKind {
315315
unsafety: _,
316316
polarity: _,
317317
defaultness: _,
@@ -320,7 +320,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
320320
ref of_trait,
321321
ref self_ty,
322322
ref items,
323-
} => {
323+
}) => {
324324
visitor.visit_generics(generics);
325325
walk_list!(visitor, visit_trait_ref, of_trait);
326326
visitor.visit_ty(self_ty);
@@ -331,7 +331,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
331331
visitor.visit_generics(generics);
332332
visitor.visit_variant_data(struct_definition);
333333
}
334-
ItemKind::Trait(.., ref generics, ref bounds, ref items) => {
334+
ItemKind::Trait(box TraitKind(.., ref generics, ref bounds, ref items)) => {
335335
visitor.visit_generics(generics);
336336
walk_list!(visitor, visit_param_bound, bounds);
337337
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
@@ -543,12 +543,12 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
543543
visitor.visit_ty(ty);
544544
walk_list!(visitor, visit_expr, expr);
545545
}
546-
ForeignItemKind::Fn(_, sig, generics, body) => {
546+
ForeignItemKind::Fn(box FnKind(_, sig, generics, body)) => {
547547
visitor.visit_generics(generics);
548548
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref());
549549
visitor.visit_fn(kind, span, id);
550550
}
551-
ForeignItemKind::TyAlias(_, generics, bounds, ty) => {
551+
ForeignItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
552552
visitor.visit_generics(generics);
553553
walk_list!(visitor, visit_param_bound, bounds);
554554
walk_list!(visitor, visit_ty, ty);
@@ -653,12 +653,12 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
653653
visitor.visit_ty(ty);
654654
walk_list!(visitor, visit_expr, expr);
655655
}
656-
AssocItemKind::Fn(_, sig, generics, body) => {
656+
AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => {
657657
visitor.visit_generics(generics);
658658
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref());
659659
visitor.visit_fn(kind, span, id);
660660
}
661-
AssocItemKind::TyAlias(_, generics, bounds, ty) => {
661+
AssocItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
662662
visitor.visit_generics(generics);
663663
walk_list!(visitor, visit_param_bound, bounds);
664664
walk_list!(visitor, visit_ty, ty);

0 commit comments

Comments
 (0)