Skip to content

Commit 4314615

Browse files
committed
Arena-allocate hir::Lifetime.
This shrinks `hir::Ty` from 72 to 48 bytes. `visit_lifetime` is added to the HIR stats collector because these types are now stored in memory on their own, instead of being within other types.
1 parent 512bd84 commit 4314615

File tree

6 files changed

+65
-56
lines changed

6 files changed

+65
-56
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11961196
let lifetime_bound = this.elided_dyn_bound(t.span);
11971197
(bounds, lifetime_bound)
11981198
});
1199-
let kind = hir::TyKind::TraitObject(bounds, lifetime_bound, TraitObjectSyntax::None);
1199+
let kind = hir::TyKind::TraitObject(bounds, &lifetime_bound, TraitObjectSyntax::None);
12001200
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
12011201
}
12021202

@@ -1934,8 +1934,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19341934
let res = res.unwrap_or(
19351935
self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error),
19361936
);
1937-
let l = self.new_named_lifetime_with_res(id, span, ident, res);
1938-
hir::GenericArg::Lifetime(l)
1937+
hir::GenericArg::Lifetime(self.new_named_lifetime_with_res(id, span, ident, res))
19391938
},
19401939
));
19411940

@@ -2004,7 +2003,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20042003
}
20052004
}
20062005

2007-
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
2006+
fn lower_lifetime(&mut self, l: &Lifetime) -> &'hir hir::Lifetime {
20082007
let span = self.lower_span(l.ident.span);
20092008
let ident = self.lower_ident(l.ident);
20102009
self.new_named_lifetime(l.id, l.id, span, ident)
@@ -2017,7 +2016,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20172016
span: Span,
20182017
ident: Ident,
20192018
res: LifetimeRes,
2020-
) -> hir::Lifetime {
2019+
) -> &'hir hir::Lifetime {
20212020
let name = match res {
20222021
LifetimeRes::Param { param, .. } => {
20232022
let p_name = ParamName::Plain(ident);
@@ -2038,7 +2037,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20382037
};
20392038

20402039
debug!(?name);
2041-
hir::Lifetime { hir_id: self.lower_node_id(id), span: self.lower_span(span), name }
2040+
self.arena.alloc(hir::Lifetime {
2041+
hir_id: self.lower_node_id(id),
2042+
span: self.lower_span(span),
2043+
name,
2044+
})
20422045
}
20432046

20442047
#[instrument(level = "debug", skip(self))]
@@ -2048,7 +2051,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20482051
new_id: NodeId,
20492052
span: Span,
20502053
ident: Ident,
2051-
) -> hir::Lifetime {
2054+
) -> &'hir hir::Lifetime {
20522055
let res = self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error);
20532056
self.new_named_lifetime_with_res(new_id, span, ident, res)
20542057
}
@@ -2462,14 +2465,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24622465
/// bound, like the bound in `Box<dyn Debug>`. This method is not invoked
24632466
/// when the bound is written, even if it is written with `'_` like in
24642467
/// `Box<dyn Debug + '_>`. In those cases, `lower_lifetime` is invoked.
2465-
fn elided_dyn_bound(&mut self, span: Span) -> hir::Lifetime {
2468+
fn elided_dyn_bound(&mut self, span: Span) -> &'hir hir::Lifetime {
24662469
let r = hir::Lifetime {
24672470
hir_id: self.next_id(),
24682471
span: self.lower_span(span),
24692472
name: hir::LifetimeName::ImplicitObjectLifetimeDefault,
24702473
};
24712474
debug!("elided_dyn_bound: r={:?}", r);
2472-
r
2475+
self.arena.alloc(r)
24732476
}
24742477
}
24752478

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl InferArg {
259259

260260
#[derive(Debug, HashStable_Generic)]
261261
pub enum GenericArg<'hir> {
262-
Lifetime(Lifetime),
262+
Lifetime(&'hir Lifetime),
263263
Type(&'hir Ty<'hir>),
264264
Const(ConstArg),
265265
Infer(InferArg),
@@ -430,7 +430,7 @@ pub enum GenericBound<'hir> {
430430
Trait(PolyTraitRef<'hir>, TraitBoundModifier),
431431
// FIXME(davidtwco): Introduce `PolyTraitRef::LangItem`
432432
LangItemTrait(LangItem, Span, HirId, &'hir GenericArgs<'hir>),
433-
Outlives(Lifetime),
433+
Outlives(&'hir Lifetime),
434434
}
435435

436436
impl GenericBound<'_> {
@@ -756,7 +756,7 @@ impl<'hir> WhereBoundPredicate<'hir> {
756756
pub struct WhereRegionPredicate<'hir> {
757757
pub span: Span,
758758
pub in_where_clause: bool,
759-
pub lifetime: Lifetime,
759+
pub lifetime: &'hir Lifetime,
760760
pub bounds: GenericBounds<'hir>,
761761
}
762762

@@ -2499,7 +2499,7 @@ pub enum TyKind<'hir> {
24992499
/// A raw pointer (i.e., `*const T` or `*mut T`).
25002500
Ptr(MutTy<'hir>),
25012501
/// A reference (i.e., `&'a T` or `&'a mut T`).
2502-
Rptr(Lifetime, MutTy<'hir>),
2502+
Rptr(&'hir Lifetime, MutTy<'hir>),
25032503
/// A bare function (e.g., `fn(usize) -> bool`).
25042504
BareFn(&'hir BareFnTy<'hir>),
25052505
/// The never type (`!`).
@@ -2518,7 +2518,7 @@ pub enum TyKind<'hir> {
25182518
OpaqueDef(ItemId, &'hir [GenericArg<'hir>]),
25192519
/// A trait object type `Bound1 + Bound2 + Bound3`
25202520
/// where `Bound` is a trait or a lifetime.
2521-
TraitObject(&'hir [PolyTraitRef<'hir>], Lifetime, TraitObjectSyntax),
2521+
TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax),
25222522
/// Unused for now.
25232523
Typeof(AnonConst),
25242524
/// `TyKind::Infer` means the type should be inferred instead of it having been
@@ -3474,7 +3474,7 @@ mod size_asserts {
34743474
static_assert_size!(ForeignItem<'_>, 72);
34753475
static_assert_size!(ForeignItemKind<'_>, 40);
34763476
#[cfg(not(bootstrap))]
3477-
static_assert_size!(GenericArg<'_>, 32);
3477+
static_assert_size!(GenericArg<'_>, 24);
34783478
static_assert_size!(GenericBound<'_>, 48);
34793479
static_assert_size!(Generics<'_>, 56);
34803480
static_assert_size!(Impl<'_>, 80);
@@ -3494,9 +3494,9 @@ mod size_asserts {
34943494
static_assert_size!(Stmt<'_>, 32);
34953495
static_assert_size!(StmtKind<'_>, 16);
34963496
#[cfg(not(bootstrap))]
3497-
static_assert_size!(TraitItem<'static>, 88);
3497+
static_assert_size!(TraitItem<'_>, 88);
34983498
#[cfg(not(bootstrap))]
34993499
static_assert_size!(TraitItemKind<'_>, 48);
3500-
static_assert_size!(Ty<'_>, 72);
3501-
static_assert_size!(TyKind<'_>, 56);
3500+
static_assert_size!(Ty<'_>, 48);
3501+
static_assert_size!(TyKind<'_>, 32);
35023502
}

compiler/rustc_passes/src/hir_stats.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
437437
}
438438
}
439439

440+
fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
441+
self.record("Lifetime", Id::Node(lifetime.hir_id), lifetime);
442+
hir_visit::walk_lifetime(self, lifetime)
443+
}
444+
440445
fn visit_path(&mut self, path: &'v hir::Path<'v>, _id: hir::HirId) {
441446
self.record("Path", Id::None, path);
442447
hir_visit::walk_path(self, path)

src/librustdoc/clean/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn clean_poly_trait_ref_with_bindings<'tcx>(
190190
)
191191
}
192192

193-
fn clean_lifetime<'tcx>(lifetime: hir::Lifetime, cx: &mut DocContext<'tcx>) -> Lifetime {
193+
fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) -> Lifetime {
194194
let def = cx.tcx.named_region(lifetime.hir_id);
195195
if let Some(
196196
rl::Region::EarlyBound(node_id)
@@ -495,7 +495,7 @@ fn clean_generic_param<'tcx>(
495495
.filter(|bp| !bp.in_where_clause)
496496
.flat_map(|bp| bp.bounds)
497497
.map(|bound| match bound {
498-
hir::GenericBound::Outlives(lt) => clean_lifetime(*lt, cx),
498+
hir::GenericBound::Outlives(lt) => clean_lifetime(lt, cx),
499499
_ => panic!(),
500500
})
501501
.collect()
@@ -1392,7 +1392,7 @@ fn maybe_expand_private_type_alias<'tcx>(
13921392
}
13931393
_ => None,
13941394
});
1395-
if let Some(lt) = lifetime.cloned() {
1395+
if let Some(lt) = lifetime {
13961396
let lt_def_id = cx.tcx.hir().local_def_id(param.hir_id);
13971397
let cleaned =
13981398
if !lt.is_elided() { clean_lifetime(lt, cx) } else { Lifetime::elided() };

src/test/ui/stats/hir-stats.stderr

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -119,59 +119,60 @@ hir-stats HIR STATS
119119
hir-stats Name Accumulated Size Count Item Size
120120
hir-stats ----------------------------------------------------------------
121121
hir-stats ForeignItemRef 24 ( 0.2%) 1 24
122+
hir-stats Lifetime 32 ( 0.3%) 1 32
122123
hir-stats Mod 32 ( 0.3%) 1 32
123124
hir-stats ExprField 40 ( 0.4%) 1 40
124125
hir-stats TraitItemRef 56 ( 0.6%) 2 28
125-
hir-stats Param 64 ( 0.6%) 2 32
126-
hir-stats Local 64 ( 0.6%) 1 64
126+
hir-stats Local 64 ( 0.7%) 1 64
127+
hir-stats Param 64 ( 0.7%) 2 32
127128
hir-stats InlineAsm 72 ( 0.7%) 1 72
128129
hir-stats ImplItemRef 72 ( 0.7%) 2 36
129-
hir-stats FieldDef 96 ( 0.9%) 2 48
130-
hir-stats Arm 96 ( 0.9%) 2 48
131-
hir-stats Body 96 ( 0.9%) 3 32
132-
hir-stats Stmt 96 ( 0.9%) 3 32
130+
hir-stats Body 96 ( 1.0%) 3 32
131+
hir-stats GenericArg 96 ( 1.0%) 4 24
132+
hir-stats - Type 24 ( 0.2%) 1
133+
hir-stats - Lifetime 72 ( 0.7%) 3
134+
hir-stats FieldDef 96 ( 1.0%) 2 48
135+
hir-stats Arm 96 ( 1.0%) 2 48
136+
hir-stats Stmt 96 ( 1.0%) 3 32
133137
hir-stats - Local 32 ( 0.3%) 1
134138
hir-stats - Semi 32 ( 0.3%) 1
135139
hir-stats - Expr 32 ( 0.3%) 1
136140
hir-stats FnDecl 120 ( 1.2%) 3 40
137141
hir-stats Attribute 128 ( 1.3%) 4 32
138-
hir-stats GenericArg 128 ( 1.3%) 4 32
139-
hir-stats - Type 32 ( 0.3%) 1
140-
hir-stats - Lifetime 96 ( 0.9%) 3
141-
hir-stats GenericArgs 144 ( 1.4%) 3 48
142+
hir-stats GenericArgs 144 ( 1.5%) 3 48
142143
hir-stats Variant 160 ( 1.6%) 2 80
143-
hir-stats GenericBound 192 ( 1.9%) 4 48
144-
hir-stats - Trait 192 ( 1.9%) 4
145-
hir-stats WherePredicate 216 ( 2.1%) 3 72
146-
hir-stats - BoundPredicate 216 ( 2.1%) 3
147-
hir-stats Block 288 ( 2.8%) 6 48
148-
hir-stats GenericParam 400 ( 3.9%) 5 80
149-
hir-stats Pat 440 ( 4.3%) 5 88
144+
hir-stats WherePredicate 168 ( 1.7%) 3 56
145+
hir-stats - BoundPredicate 168 ( 1.7%) 3
146+
hir-stats GenericBound 192 ( 2.0%) 4 48
147+
hir-stats - Trait 192 ( 2.0%) 4
148+
hir-stats Block 288 ( 3.0%) 6 48
149+
hir-stats GenericParam 400 ( 4.1%) 5 80
150+
hir-stats Pat 440 ( 4.5%) 5 88
150151
hir-stats - Wild 88 ( 0.9%) 1
151152
hir-stats - Struct 88 ( 0.9%) 1
152-
hir-stats - Binding 264 ( 2.6%) 3
153-
hir-stats Generics 560 ( 5.5%) 10 56
154-
hir-stats Expr 768 ( 7.6%) 12 64
155-
hir-stats - Path 64 ( 0.6%) 1
156-
hir-stats - Struct 64 ( 0.6%) 1
157-
hir-stats - Match 64 ( 0.6%) 1
158-
hir-stats - InlineAsm 64 ( 0.6%) 1
153+
hir-stats - Binding 264 ( 2.7%) 3
154+
hir-stats Generics 560 ( 5.7%) 10 56
155+
hir-stats Ty 720 ( 7.4%) 15 48
156+
hir-stats - Ptr 48 ( 0.5%) 1
157+
hir-stats - Rptr 48 ( 0.5%) 1
158+
hir-stats - Path 624 ( 6.4%) 13
159+
hir-stats Expr 768 ( 7.9%) 12 64
160+
hir-stats - Path 64 ( 0.7%) 1
161+
hir-stats - Struct 64 ( 0.7%) 1
162+
hir-stats - Match 64 ( 0.7%) 1
163+
hir-stats - InlineAsm 64 ( 0.7%) 1
159164
hir-stats - Lit 128 ( 1.3%) 2
160-
hir-stats - Block 384 ( 3.8%) 6
161-
hir-stats Item 960 ( 9.4%) 12 80
165+
hir-stats - Block 384 ( 3.9%) 6
166+
hir-stats Item 960 ( 9.8%) 12 80
162167
hir-stats - Trait 80 ( 0.8%) 1
163168
hir-stats - Enum 80 ( 0.8%) 1
164169
hir-stats - ExternCrate 80 ( 0.8%) 1
165170
hir-stats - ForeignMod 80 ( 0.8%) 1
166171
hir-stats - Impl 80 ( 0.8%) 1
167172
hir-stats - Fn 160 ( 1.6%) 2
168-
hir-stats - Use 400 ( 3.9%) 5
169-
hir-stats Ty 1_080 (10.6%) 15 72
170-
hir-stats - Ptr 72 ( 0.7%) 1
171-
hir-stats - Rptr 72 ( 0.7%) 1
172-
hir-stats - Path 936 ( 9.2%) 13
173-
hir-stats Path 1_536 (15.1%) 32 48
174-
hir-stats PathSegment 2_240 (22.0%) 40 56
173+
hir-stats - Use 400 ( 4.1%) 5
174+
hir-stats Path 1_536 (15.7%) 32 48
175+
hir-stats PathSegment 2_240 (23.0%) 40 56
175176
hir-stats ----------------------------------------------------------------
176-
hir-stats Total 10_168
177+
hir-stats Total 9_760
177178
hir-stats

src/tools/clippy/clippy_utils/src/hir_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
929929
}
930930
}
931931

932-
pub fn hash_lifetime(&mut self, lifetime: Lifetime) {
932+
pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
933933
std::mem::discriminant(&lifetime.name).hash(&mut self.s);
934934
if let LifetimeName::Param(param_id, ref name) = lifetime.name {
935935
std::mem::discriminant(name).hash(&mut self.s);

0 commit comments

Comments
 (0)