]> git.lizzy.rs Git - rust.git/commitdiff
Reduce the size of `hir::Expr`.
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 7 Feb 2019 05:03:57 +0000 (16:03 +1100)
committerNicholas Nethercote <nnethercote@mozilla.com>
Thu, 14 Feb 2019 00:52:03 +0000 (11:52 +1100)
From 104 bytes to 72 bytes on x86-64. This slightly reduces instruction
counts.

Also add an assertion about the size.

src/librustc/hir/lowering.rs
src/librustc/hir/mod.rs
src/librustc_save_analysis/lib.rs
src/librustc_typeck/check/demand.rs

index cc5b105bad0d42a80e03f3b54ee714e6ffccdb53..84487c40f874508f4d6ad469d941139d1109266f 100644 (file)
@@ -3859,7 +3859,7 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                 hir::ExprKind::Call(f, args.iter().map(|x| self.lower_expr(x)).collect())
             }
             ExprKind::MethodCall(ref seg, ref args) => {
-                let hir_seg = self.lower_path_segment(
+                let hir_seg = P(self.lower_path_segment(
                     e.span,
                     seg,
                     ParamMode::Optional,
@@ -3867,7 +3867,7 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                     ParenthesizedGenericArgs::Err,
                     ImplTraitContext::disallowed(),
                     None,
-                );
+                ));
                 let args = args.iter().map(|x| self.lower_expr(x)).collect();
                 hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args)
             }
@@ -4148,7 +4148,7 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                     node: if is_unit {
                         hir::ExprKind::Path(struct_path)
                     } else {
-                        hir::ExprKind::Struct(struct_path, fields, None)
+                        hir::ExprKind::Struct(P(struct_path), fields, None)
                     },
                     span: e.span,
                     attrs: e.attrs.clone(),
@@ -4220,13 +4220,13 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
                 hir::ExprKind::InlineAsm(P(hir_asm), outputs, inputs)
             }
             ExprKind::Struct(ref path, ref fields, ref maybe_expr) => hir::ExprKind::Struct(
-                self.lower_qpath(
+                P(self.lower_qpath(
                     e.id,
                     &None,
                     path,
                     ParamMode::Optional,
                     ImplTraitContext::disallowed(),
-                ),
+                )),
                 fields.iter().map(|x| self.lower_field(x)).collect(),
                 maybe_expr.as_ref().map(|x| P(self.lower_expr(x))),
             ),
index bf16ec0be83e797b3dbefac96690ff3d10e60404..e389b918d3c35b348c87de2d6403393bcf129d92 100644 (file)
@@ -1319,6 +1319,10 @@ pub struct Expr {
     pub hir_id: HirId,
 }
 
+// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
+#[cfg(target_arch = "x86_64")]
+static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 72);
+
 impl Expr {
     pub fn precedence(&self) -> ExprPrecedence {
         match self.node {
@@ -1438,7 +1442,7 @@ pub enum ExprKind {
     /// and the remaining elements are the rest of the arguments.
     /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
     /// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d])`.
-    MethodCall(PathSegment, Span, HirVec<Expr>),
+    MethodCall(P<PathSegment>, Span, HirVec<Expr>),
     /// A tuple (e.g., `(a, b, c ,d)`).
     Tup(HirVec<Expr>),
     /// A binary operation (e.g., `a + b`, `a * b`).
@@ -1506,7 +1510,7 @@ pub enum ExprKind {
     ///
     /// For example, `Foo {x: 1, y: 2}`, or
     /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
-    Struct(QPath, HirVec<Field>, Option<P<Expr>>),
+    Struct(P<QPath>, HirVec<Field>, Option<P<Expr>>),
 
     /// An array literal constructed from one repeated element.
     ///
index 8d20c44a5a4ef0c78883eefc25fb7695b4625c9a..8ab9a8e8dda86484356ea118ca322675e466888b 100644 (file)
@@ -614,11 +614,16 @@ pub fn get_path_def(&self, id: NodeId) -> HirDef {
                     Some(def) if def != HirDef::Err => def,
                     _ => self.get_path_def(self.tcx.hir().get_parent_node(id)),
                 }
-            },
+            }
+
             Node::Expr(&hir::Expr {
                 node: hir::ExprKind::Struct(ref qpath, ..),
                 ..
-            }) |
+            }) => {
+                let hir_id = self.tcx.hir().node_to_hir_id(id);
+                self.tables.qpath_def(qpath, hir_id)
+            }
+
             Node::Expr(&hir::Expr {
                 node: hir::ExprKind::Path(ref qpath),
                 ..
index 33e93b582e5401bc40a7c48918c85dae53c891a0..f6a0fd5caccdb2bb035ede154598da47f335deda 100644 (file)
@@ -430,7 +430,11 @@ fn is_range_literal(&self, expr: &hir::Expr) -> bool {
 
         match expr.node {
             // All built-in range literals but `..=` and `..` desugar to Structs
-            ExprKind::Struct(QPath::Resolved(None, ref path), _, _) |
+            ExprKind::Struct(ref qpath, _, _) => {
+                if let QPath::Resolved(None, ref path) = **qpath {
+                    return is_range_path(&path) && span_is_range_literal(&expr.span);
+                }
+            }
             // `..` desugars to its struct path
             ExprKind::Path(QPath::Resolved(None, ref path)) => {
                 return is_range_path(&path) && span_is_range_literal(&expr.span);