Skip to content

Commit fb1f544

Browse files
committed
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<Expr> needs to allocate.
1 parent 4bc8a01 commit fb1f544

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

crates/hir_def/src/body/lower.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,10 @@ impl ExprCollector<'_> {
322322
Vec::new()
323323
};
324324
let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
325-
let generic_args =
326-
e.generic_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it));
325+
let generic_args = e
326+
.generic_arg_list()
327+
.and_then(|it| GenericArgs::from_ast(&self.ctx(), it))
328+
.map(Box::new);
327329
self.alloc_expr(
328330
Expr::MethodCall { receiver, method_name, args, generic_args },
329331
syntax_ptr,
@@ -385,7 +387,7 @@ impl ExprCollector<'_> {
385387
self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
386388
}
387389
ast::Expr::RecordExpr(e) => {
388-
let path = e.path().and_then(|path| self.expander.parse_path(path));
390+
let path = e.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
389391
let record_lit = if let Some(nfl) = e.record_expr_field_list() {
390392
let fields = nfl
391393
.fields()
@@ -430,7 +432,7 @@ impl ExprCollector<'_> {
430432
}
431433
ast::Expr::CastExpr(e) => {
432434
let expr = self.collect_expr_opt(e.expr());
433-
let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty());
435+
let type_ref = Box::new(TypeRef::from_ast_opt(&self.ctx(), e.ty()));
434436
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
435437
}
436438
ast::Expr::RefExpr(e) => {
@@ -469,8 +471,10 @@ impl ExprCollector<'_> {
469471
arg_types.push(type_ref);
470472
}
471473
}
472-
let ret_type =
473-
e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&self.ctx(), it));
474+
let ret_type = e
475+
.ret_type()
476+
.and_then(|r| r.ty())
477+
.map(|it| Box::new(TypeRef::from_ast(&self.ctx(), it)));
474478
let body = self.collect_expr_opt(e.body());
475479
self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
476480
}

crates/hir_def/src/expr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub enum Expr {
8686
receiver: ExprId,
8787
method_name: Name,
8888
args: Vec<ExprId>,
89-
generic_args: Option<GenericArgs>,
89+
generic_args: Option<Box<GenericArgs>>,
9090
},
9191
Match {
9292
expr: ExprId,
@@ -106,7 +106,7 @@ pub enum Expr {
106106
expr: Option<ExprId>,
107107
},
108108
RecordLit {
109-
path: Option<Path>,
109+
path: Option<Box<Path>>,
110110
fields: Vec<RecordLitField>,
111111
spread: Option<ExprId>,
112112
},
@@ -131,7 +131,7 @@ pub enum Expr {
131131
},
132132
Cast {
133133
expr: ExprId,
134-
type_ref: TypeRef,
134+
type_ref: Box<TypeRef>,
135135
},
136136
Ref {
137137
expr: ExprId,
@@ -162,7 +162,7 @@ pub enum Expr {
162162
Lambda {
163163
args: Vec<PatId>,
164164
arg_types: Vec<Option<TypeRef>>,
165-
ret_type: Option<TypeRef>,
165+
ret_type: Option<Box<TypeRef>>,
166166
body: ExprId,
167167
},
168168
Tuple {

crates/hir_ty/src/infer/expr.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,13 @@ impl<'a> InferenceContext<'a> {
317317
self.normalize_associated_types_in(ret_ty)
318318
}
319319
Expr::MethodCall { receiver, args, method_name, generic_args } => self
320-
.infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()),
320+
.infer_method_call(
321+
tgt_expr,
322+
*receiver,
323+
&args,
324+
&method_name,
325+
generic_args.as_deref(),
326+
),
321327
Expr::Match { expr, arms } => {
322328
let input_ty = self.infer_expr(*expr, &Expectation::none());
323329

@@ -398,7 +404,7 @@ impl<'a> InferenceContext<'a> {
398404
TyKind::Never.intern(&Interner)
399405
}
400406
Expr::RecordLit { path, fields, spread } => {
401-
let (ty, def_id) = self.resolve_variant(path.as_ref());
407+
let (ty, def_id) = self.resolve_variant(path.as_deref());
402408
if let Some(variant) = def_id {
403409
self.write_variant_resolution(tgt_expr.into(), variant);
404410
}

0 commit comments

Comments
 (0)