Skip to content

Commit a85f584

Browse files
committed
Auto merge of rust-lang#86266 - LeSeulArtichaut:box-thir-adt, r=davidtwco
Box `thir::ExprKind::Adt` for performance `Adt` is the biggest variant in the enum and probably isn't used very often compared to the other expr kinds, so boxing it should be beneficial for performance. We need a perf test to be sure.
2 parents 9fef8d9 + 5e802e5 commit a85f584

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

compiler/rustc_middle/src/thir.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ pub struct Block {
9797
pub safety_mode: BlockSafety,
9898
}
9999

100+
#[derive(Debug, HashStable)]
101+
pub struct Adt<'tcx> {
102+
pub adt_def: &'tcx AdtDef,
103+
pub variant_index: VariantIdx,
104+
pub substs: SubstsRef<'tcx>,
105+
106+
/// Optional user-given substs: for something like `let x =
107+
/// Bar::<T> { ... }`.
108+
pub user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
109+
110+
pub fields: Box<[FieldExpr]>,
111+
pub base: Option<FruInfo<'tcx>>,
112+
}
113+
100114
#[derive(Copy, Clone, Debug, HashStable)]
101115
pub enum BlockSafety {
102116
Safe,
@@ -145,7 +159,7 @@ pub enum StmtKind<'tcx> {
145159

146160
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
147161
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
148-
rustc_data_structures::static_assert_size!(Expr<'_>, 144);
162+
rustc_data_structures::static_assert_size!(Expr<'_>, 104);
149163

150164
/// The Thir trait implementor lowers their expressions (`&'tcx H::Expr`)
151165
/// into instances of this `Expr` enum. This lowering can be done
@@ -304,18 +318,7 @@ pub enum ExprKind<'tcx> {
304318
Tuple {
305319
fields: Box<[ExprId]>,
306320
},
307-
Adt {
308-
adt_def: &'tcx AdtDef,
309-
variant_index: VariantIdx,
310-
substs: SubstsRef<'tcx>,
311-
312-
/// Optional user-given substs: for something like `let x =
313-
/// Bar::<T> { ... }`.
314-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
315-
316-
fields: Box<[FieldExpr]>,
317-
base: Option<FruInfo<'tcx>>,
318-
},
321+
Adt(Box<Adt<'tcx>>),
319322
PlaceTypeAscription {
320323
source: ExprId,
321324
/// Type that the user gave to this expression

compiler/rustc_mir_build/src/build/expr/into.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
264264
this.cfg.push_assign(block, source_info, destination, address_of);
265265
block.unit()
266266
}
267-
ExprKind::Adt { adt_def, variant_index, substs, user_ty, ref fields, ref base } => {
267+
ExprKind::Adt(box Adt {
268+
adt_def,
269+
variant_index,
270+
substs,
271+
user_ty,
272+
ref fields,
273+
ref base,
274+
}) => {
268275
// See the notes for `ExprKind::Array` in `as_rvalue` and for
269276
// `ExprKind::Borrow` above.
270277
let is_union = adt_def.is_union();

compiler/rustc_mir_build/src/check_unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
195195
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
196196
self.requires_unsafe(expr.span, UseOfInlineAssembly);
197197
}
198-
ExprKind::Adt {
198+
ExprKind::Adt(box Adt {
199199
adt_def,
200200
variant_index: _,
201201
substs: _,
202202
user_ty: _,
203203
fields: _,
204204
base: _,
205-
} => match self.tcx.layout_scalar_valid_range(adt_def.did) {
205+
}) => match self.tcx.layout_scalar_valid_range(adt_def.did) {
206206
(Bound::Unbounded, Bound::Unbounded) => {}
207207
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
208208
},

compiler/rustc_mir_build/src/thir/cx/expr.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ impl<'tcx> Cx<'tcx> {
228228
expr: self.mirror_expr(e),
229229
})
230230
.collect();
231-
ExprKind::Adt {
231+
ExprKind::Adt(Box::new(Adt {
232232
adt_def,
233233
substs,
234234
variant_index: index,
235235
fields: field_refs,
236236
user_ty,
237237
base: None,
238-
}
238+
}))
239239
} else {
240240
ExprKind::Call {
241241
ty: self.typeck_results().node_type(fun.hir_id),
@@ -362,7 +362,7 @@ impl<'tcx> Cx<'tcx> {
362362
let user_provided_types = self.typeck_results().user_provided_types();
363363
let user_ty = user_provided_types.get(expr.hir_id).copied();
364364
debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty);
365-
ExprKind::Adt {
365+
ExprKind::Adt(Box::new(Adt {
366366
adt_def: adt,
367367
variant_index: VariantIdx::new(0),
368368
substs,
@@ -375,7 +375,7 @@ impl<'tcx> Cx<'tcx> {
375375
.copied()
376376
.collect(),
377377
}),
378-
}
378+
}))
379379
}
380380
AdtKind::Enum => {
381381
let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
@@ -388,14 +388,14 @@ impl<'tcx> Cx<'tcx> {
388388
self.typeck_results().user_provided_types();
389389
let user_ty = user_provided_types.get(expr.hir_id).copied();
390390
debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty);
391-
ExprKind::Adt {
391+
ExprKind::Adt(Box::new(Adt {
392392
adt_def: adt,
393393
variant_index: index,
394394
substs,
395395
user_ty,
396396
fields: self.field_refs(fields),
397397
base: None,
398-
}
398+
}))
399399
}
400400
_ => {
401401
span_bug!(expr.span, "unexpected res: {:?}", res);
@@ -906,14 +906,14 @@ impl<'tcx> Cx<'tcx> {
906906
match ty.kind() {
907907
// A unit struct/variant which is used as a value.
908908
// We return a completely different ExprKind here to account for this special case.
909-
ty::Adt(adt_def, substs) => ExprKind::Adt {
909+
ty::Adt(adt_def, substs) => ExprKind::Adt(Box::new(Adt {
910910
adt_def,
911911
variant_index: adt_def.variant_index_with_ctor_id(def_id),
912912
substs,
913913
user_ty: user_provided_type,
914914
fields: box [],
915915
base: None,
916-
},
916+
})),
917917
_ => bug!("unexpected ty: {:?}", ty),
918918
}
919919
}

compiler/rustc_mir_build/src/thir/visit.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_middle::thir::*;
1+
use rustc_middle::thir::{self, *};
22
use rustc_middle::ty::Const;
33

44
pub trait Visitor<'a, 'tcx: 'a>: Sized {
@@ -94,7 +94,14 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
9494
visitor.visit_expr(&visitor.thir()[field]);
9595
}
9696
}
97-
Adt { ref fields, ref base, adt_def: _, variant_index: _, substs: _, user_ty: _ } => {
97+
Adt(box thir::Adt {
98+
ref fields,
99+
ref base,
100+
adt_def: _,
101+
variant_index: _,
102+
substs: _,
103+
user_ty: _,
104+
}) => {
98105
for field in &**fields {
99106
visitor.visit_expr(&visitor.thir()[field.expr]);
100107
}

0 commit comments

Comments
 (0)