From fb1f544e2479addd5957688e297ea04ddf0cf249 Mon Sep 17 00:00:00 2001 From: Alexandru Macovei Date: Tue, 30 Mar 2021 23:06:57 +0300 Subject: Use Box'es to reduce size of hir_def::expr::Expr from 128 to 72 bytes (on 64bit systems) Rationale: only a minority of variants used almost half the size. By keeping large members (especially in Option) behind a box the memory cost is only payed when the large variants are needed. This reduces the size Vec needs to allocate. --- crates/hir_def/src/body/lower.rs | 16 ++++++++++------ crates/hir_def/src/expr.rs | 8 ++++---- crates/hir_ty/src/infer/expr.rs | 10 ++++++++-- 3 files changed, 22 insertions(+), 12 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 63e89a1f4..73e7aee33 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -322,8 +322,10 @@ impl ExprCollector<'_> { Vec::new() }; let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing); - let generic_args = - e.generic_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it)); + let generic_args = e + .generic_arg_list() + .and_then(|it| GenericArgs::from_ast(&self.ctx(), it)) + .map(Box::new); self.alloc_expr( Expr::MethodCall { receiver, method_name, args, generic_args }, syntax_ptr, @@ -385,7 +387,7 @@ impl ExprCollector<'_> { self.alloc_expr(Expr::Yield { expr }, syntax_ptr) } ast::Expr::RecordExpr(e) => { - let path = e.path().and_then(|path| self.expander.parse_path(path)); + let path = e.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); let record_lit = if let Some(nfl) = e.record_expr_field_list() { let fields = nfl .fields() @@ -430,7 +432,7 @@ impl ExprCollector<'_> { } ast::Expr::CastExpr(e) => { let expr = self.collect_expr_opt(e.expr()); - let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty()); + let type_ref = Box::new(TypeRef::from_ast_opt(&self.ctx(), e.ty())); self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) } ast::Expr::RefExpr(e) => { @@ -469,8 +471,10 @@ impl ExprCollector<'_> { arg_types.push(type_ref); } } - let ret_type = - e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&self.ctx(), it)); + let ret_type = e + .ret_type() + .and_then(|r| r.ty()) + .map(|it| Box::new(TypeRef::from_ast(&self.ctx(), it))); let body = self.collect_expr_opt(e.body()); self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr) } diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs index 6c7376fad..ba00b9609 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs @@ -86,7 +86,7 @@ pub enum Expr { receiver: ExprId, method_name: Name, args: Vec, - generic_args: Option, + generic_args: Option>, }, Match { expr: ExprId, @@ -106,7 +106,7 @@ pub enum Expr { expr: Option, }, RecordLit { - path: Option, + path: Option>, fields: Vec, spread: Option, }, @@ -131,7 +131,7 @@ pub enum Expr { }, Cast { expr: ExprId, - type_ref: TypeRef, + type_ref: Box, }, Ref { expr: ExprId, @@ -162,7 +162,7 @@ pub enum Expr { Lambda { args: Vec, arg_types: Vec>, - ret_type: Option, + ret_type: Option>, body: ExprId, }, Tuple { diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index ff564106b..2fcc7c549 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -317,7 +317,13 @@ impl<'a> InferenceContext<'a> { self.normalize_associated_types_in(ret_ty) } Expr::MethodCall { receiver, args, method_name, generic_args } => self - .infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()), + .infer_method_call( + tgt_expr, + *receiver, + &args, + &method_name, + generic_args.as_deref(), + ), Expr::Match { expr, arms } => { let input_ty = self.infer_expr(*expr, &Expectation::none()); @@ -398,7 +404,7 @@ impl<'a> InferenceContext<'a> { TyKind::Never.intern(&Interner) } Expr::RecordLit { path, fields, spread } => { - let (ty, def_id) = self.resolve_variant(path.as_ref()); + let (ty, def_id) = self.resolve_variant(path.as_deref()); if let Some(variant) = def_id { self.write_variant_resolution(tgt_expr.into(), variant); } -- cgit v1.2.3