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 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'crates/hir_def/src/body/lower.rs') 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) } -- cgit v1.2.3 From 32304d14a1a5e316615028ffd7bcfcff682fbe56 Mon Sep 17 00:00:00 2001 From: Alexandru Macovei Date: Wed, 31 Mar 2021 00:55:18 +0300 Subject: Use Box'es to reduce the size of hir_def::expr::Pat from 112 to 64 bytes on 64bit --- crates/hir_def/src/body/lower.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/hir_def/src/body/lower.rs') diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 73e7aee33..1e743e5d5 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -759,7 +759,7 @@ impl ExprCollector<'_> { } } ast::Pat::TupleStructPat(p) => { - let path = p.path().and_then(|path| self.expander.parse_path(path)); + let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); let (args, ellipsis) = self.collect_tuple_pat(p.fields()); Pat::TupleStruct { path, args, ellipsis } } @@ -769,7 +769,7 @@ impl ExprCollector<'_> { Pat::Ref { pat, mutability } } ast::Pat::PathPat(p) => { - let path = p.path().and_then(|path| self.expander.parse_path(path)); + let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); path.map(Pat::Path).unwrap_or(Pat::Missing) } ast::Pat::OrPat(p) => { @@ -783,7 +783,7 @@ impl ExprCollector<'_> { } ast::Pat::WildcardPat(_) => Pat::Wild, ast::Pat::RecordPat(p) => { - let path = p.path().and_then(|path| self.expander.parse_path(path)); + let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new); let args: Vec<_> = p .record_pat_field_list() .expect("every struct should have a field list") -- cgit v1.2.3