Skip to content

Commit 8e768a5

Browse files
Merge #8374
8374: Intern TypeRefs stored in Body r=jonas-schievink a=jonas-schievink Minor improvement to memory usage (1 MB or so) bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 7d39b13 + a25fbdb commit 8e768a5

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

crates/hir_def/src/body/lower.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::{
3030
LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField,
3131
Statement,
3232
},
33+
intern::Interned,
3334
item_scope::BuiltinShadowMode,
3435
path::{GenericArgs, Path},
3536
type_ref::{Mutability, Rawness, TypeRef},
@@ -432,7 +433,7 @@ impl ExprCollector<'_> {
432433
}
433434
ast::Expr::CastExpr(e) => {
434435
let expr = self.collect_expr_opt(e.expr());
435-
let type_ref = Box::new(TypeRef::from_ast_opt(&self.ctx(), e.ty()));
436+
let type_ref = Interned::new(TypeRef::from_ast_opt(&self.ctx(), e.ty()));
436437
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
437438
}
438439
ast::Expr::RefExpr(e) => {
@@ -466,15 +467,16 @@ impl ExprCollector<'_> {
466467
if let Some(pl) = e.param_list() {
467468
for param in pl.params() {
468469
let pat = self.collect_pat_opt(param.pat());
469-
let type_ref = param.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
470+
let type_ref =
471+
param.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
470472
args.push(pat);
471473
arg_types.push(type_ref);
472474
}
473475
}
474476
let ret_type = e
475477
.ret_type()
476478
.and_then(|r| r.ty())
477-
.map(|it| Box::new(TypeRef::from_ast(&self.ctx(), it)));
479+
.map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
478480
let body = self.collect_expr_opt(e.body());
479481
self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
480482
}
@@ -629,7 +631,8 @@ impl ExprCollector<'_> {
629631
return;
630632
}
631633
let pat = self.collect_pat_opt(stmt.pat());
632-
let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
634+
let type_ref =
635+
stmt.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
633636
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
634637
self.statements_in_scope.push(Statement::Let { pat, type_ref, initializer });
635638
}

crates/hir_def/src/expr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use syntax::ast::RangeOp;
1818

1919
use crate::{
2020
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
21+
intern::Interned,
2122
path::{GenericArgs, Path},
2223
type_ref::{Mutability, Rawness, TypeRef},
2324
BlockId,
@@ -131,7 +132,7 @@ pub enum Expr {
131132
},
132133
Cast {
133134
expr: ExprId,
134-
type_ref: Box<TypeRef>,
135+
type_ref: Interned<TypeRef>,
135136
},
136137
Ref {
137138
expr: ExprId,
@@ -161,8 +162,8 @@ pub enum Expr {
161162
},
162163
Lambda {
163164
args: Vec<PatId>,
164-
arg_types: Vec<Option<TypeRef>>,
165-
ret_type: Option<Box<TypeRef>>,
165+
arg_types: Vec<Option<Interned<TypeRef>>>,
166+
ret_type: Option<Interned<TypeRef>>,
166167
body: ExprId,
167168
},
168169
Tuple {
@@ -240,7 +241,7 @@ pub struct RecordLitField {
240241

241242
#[derive(Debug, Clone, Eq, PartialEq)]
242243
pub enum Statement {
243-
Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> },
244+
Let { pat: PatId, type_ref: Option<Interned<TypeRef>>, initializer: Option<ExprId> },
244245
Expr(ExprId),
245246
}
246247

0 commit comments

Comments
 (0)