From a25fbdb30a708cfecbd7aa4343e32c24bd692f20 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 6 Apr 2021 16:07:45 +0200 Subject: Intern TypeRefs stored in Body Minor improvement to memory usage (1 MB or so) --- crates/hir_def/src/body/lower.rs | 11 +++++++---- crates/hir_def/src/expr.rs | 9 +++++---- 2 files changed, 12 insertions(+), 8 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 1e743e5d5..bfb75a8a5 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -30,6 +30,7 @@ use crate::{ LabelId, Literal, LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, }, + intern::Interned, item_scope::BuiltinShadowMode, path::{GenericArgs, Path}, type_ref::{Mutability, Rawness, TypeRef}, @@ -432,7 +433,7 @@ impl ExprCollector<'_> { } ast::Expr::CastExpr(e) => { let expr = self.collect_expr_opt(e.expr()); - let type_ref = Box::new(TypeRef::from_ast_opt(&self.ctx(), e.ty())); + let type_ref = Interned::new(TypeRef::from_ast_opt(&self.ctx(), e.ty())); self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) } ast::Expr::RefExpr(e) => { @@ -466,7 +467,8 @@ impl ExprCollector<'_> { if let Some(pl) = e.param_list() { for param in pl.params() { let pat = self.collect_pat_opt(param.pat()); - let type_ref = param.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); + let type_ref = + param.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it))); args.push(pat); arg_types.push(type_ref); } @@ -474,7 +476,7 @@ impl ExprCollector<'_> { let ret_type = e .ret_type() .and_then(|r| r.ty()) - .map(|it| Box::new(TypeRef::from_ast(&self.ctx(), it))); + .map(|it| Interned::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) } @@ -629,7 +631,8 @@ impl ExprCollector<'_> { return; } let pat = self.collect_pat_opt(stmt.pat()); - let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it)); + let type_ref = + stmt.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it))); let initializer = stmt.initializer().map(|e| self.collect_expr(e)); self.statements_in_scope.push(Statement::Let { pat, type_ref, initializer }); } diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs index 62a28bdba..b4ad984bd 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs @@ -18,6 +18,7 @@ use syntax::ast::RangeOp; use crate::{ builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, + intern::Interned, path::{GenericArgs, Path}, type_ref::{Mutability, Rawness, TypeRef}, BlockId, @@ -131,7 +132,7 @@ pub enum Expr { }, Cast { expr: ExprId, - type_ref: Box, + type_ref: Interned, }, Ref { expr: ExprId, @@ -161,8 +162,8 @@ pub enum Expr { }, Lambda { args: Vec, - arg_types: Vec>, - ret_type: Option>, + arg_types: Vec>>, + ret_type: Option>, body: ExprId, }, Tuple { @@ -240,7 +241,7 @@ pub struct RecordLitField { #[derive(Debug, Clone, Eq, PartialEq)] pub enum Statement { - Let { pat: PatId, type_ref: Option, initializer: Option }, + Let { pat: PatId, type_ref: Option>, initializer: Option }, Expr(ExprId), } -- cgit v1.2.3