Skip to content

Commit 5d65e8c

Browse files
committed
Reduce the size of hir::Expr.
From 104 bytes to 72 bytes on x86-64. This slightly reduces instruction counts. Also add an assertion about the size.
1 parent e544947 commit 5d65e8c

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

src/librustc/hir/lowering.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3859,15 +3859,15 @@ impl<'a> LoweringContext<'a> {
38593859
hir::ExprKind::Call(f, args.iter().map(|x| self.lower_expr(x)).collect())
38603860
}
38613861
ExprKind::MethodCall(ref seg, ref args) => {
3862-
let hir_seg = self.lower_path_segment(
3862+
let hir_seg = P(self.lower_path_segment(
38633863
e.span,
38643864
seg,
38653865
ParamMode::Optional,
38663866
0,
38673867
ParenthesizedGenericArgs::Err,
38683868
ImplTraitContext::disallowed(),
38693869
None,
3870-
);
3870+
));
38713871
let args = args.iter().map(|x| self.lower_expr(x)).collect();
38723872
hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args)
38733873
}
@@ -4148,7 +4148,7 @@ impl<'a> LoweringContext<'a> {
41484148
node: if is_unit {
41494149
hir::ExprKind::Path(struct_path)
41504150
} else {
4151-
hir::ExprKind::Struct(struct_path, fields, None)
4151+
hir::ExprKind::Struct(P(struct_path), fields, None)
41524152
},
41534153
span: e.span,
41544154
attrs: e.attrs.clone(),
@@ -4220,13 +4220,13 @@ impl<'a> LoweringContext<'a> {
42204220
hir::ExprKind::InlineAsm(P(hir_asm), outputs, inputs)
42214221
}
42224222
ExprKind::Struct(ref path, ref fields, ref maybe_expr) => hir::ExprKind::Struct(
4223-
self.lower_qpath(
4223+
P(self.lower_qpath(
42244224
e.id,
42254225
&None,
42264226
path,
42274227
ParamMode::Optional,
42284228
ImplTraitContext::disallowed(),
4229-
),
4229+
)),
42304230
fields.iter().map(|x| self.lower_field(x)).collect(),
42314231
maybe_expr.as_ref().map(|x| P(self.lower_expr(x))),
42324232
),

src/librustc/hir/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,10 @@ pub struct Expr {
13191319
pub hir_id: HirId,
13201320
}
13211321

1322+
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
1323+
#[cfg(target_arch = "x86_64")]
1324+
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 72);
1325+
13221326
impl Expr {
13231327
pub fn precedence(&self) -> ExprPrecedence {
13241328
match self.node {
@@ -1438,7 +1442,7 @@ pub enum ExprKind {
14381442
/// and the remaining elements are the rest of the arguments.
14391443
/// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
14401444
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d])`.
1441-
MethodCall(PathSegment, Span, HirVec<Expr>),
1445+
MethodCall(P<PathSegment>, Span, HirVec<Expr>),
14421446
/// A tuple (e.g., `(a, b, c ,d)`).
14431447
Tup(HirVec<Expr>),
14441448
/// A binary operation (e.g., `a + b`, `a * b`).
@@ -1506,7 +1510,7 @@ pub enum ExprKind {
15061510
///
15071511
/// For example, `Foo {x: 1, y: 2}`, or
15081512
/// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
1509-
Struct(QPath, HirVec<Field>, Option<P<Expr>>),
1513+
Struct(P<QPath>, HirVec<Field>, Option<P<Expr>>),
15101514

15111515
/// An array literal constructed from one repeated element.
15121516
///

src/librustc_save_analysis/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,16 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
614614
Some(def) if def != HirDef::Err => def,
615615
_ => self.get_path_def(self.tcx.hir().get_parent_node(id)),
616616
}
617-
},
617+
}
618+
618619
Node::Expr(&hir::Expr {
619620
node: hir::ExprKind::Struct(ref qpath, ..),
620621
..
621-
}) |
622+
}) => {
623+
let hir_id = self.tcx.hir().node_to_hir_id(id);
624+
self.tables.qpath_def(qpath, hir_id)
625+
}
626+
622627
Node::Expr(&hir::Expr {
623628
node: hir::ExprKind::Path(ref qpath),
624629
..

src/librustc_typeck/check/demand.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
430430

431431
match expr.node {
432432
// All built-in range literals but `..=` and `..` desugar to Structs
433-
ExprKind::Struct(QPath::Resolved(None, ref path), _, _) |
433+
ExprKind::Struct(ref qpath, _, _) => {
434+
if let QPath::Resolved(None, ref path) = **qpath {
435+
return is_range_path(&path) && span_is_range_literal(&expr.span);
436+
}
437+
}
434438
// `..` desugars to its struct path
435439
ExprKind::Path(QPath::Resolved(None, ref path)) => {
436440
return is_range_path(&path) && span_is_range_literal(&expr.span);

0 commit comments

Comments
 (0)