From 7f884487d7c5cdd167e118b88f850c699f3b5344 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 25 Jul 2024 11:14:55 +0200 Subject: [PATCH 1/2] Reduce size of TypeRef by 8 bytes --- crates/hir-def/src/hir/type_ref.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/hir-def/src/hir/type_ref.rs b/crates/hir-def/src/hir/type_ref.rs index 9b05432258..4cca42ed1a 100644 --- a/crates/hir-def/src/hir/type_ref.rs +++ b/crates/hir-def/src/hir/type_ref.rs @@ -121,7 +121,7 @@ pub enum TypeRef { Slice(Box), /// A fn pointer. Last element of the vector is the return type. Fn( - Vec<(Option, TypeRef)>, + Box<[(Option, TypeRef)]>, bool, /*varargs*/ bool, /*is_unsafe*/ Option, /* abi */ @@ -228,7 +228,7 @@ impl TypeRef { }) .collect() } else { - Vec::new() + Vec::with_capacity(1) }; fn lower_abi(abi: ast::Abi) -> Symbol { match abi.abi_string() { @@ -240,7 +240,7 @@ impl TypeRef { let abi = inner.abi().map(lower_abi); params.push((None, ret_ty)); - TypeRef::Fn(params, is_varargs, inner.unsafe_token().is_some(), abi) + TypeRef::Fn(params.into(), is_varargs, inner.unsafe_token().is_some(), abi) } // for types are close enough for our purposes to the inner type for now... ast::Type::ForType(inner) => TypeRef::from_ast_opt(ctx, inner.ty()), @@ -396,7 +396,7 @@ impl TypeBound { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ConstRef { - Scalar(LiteralConstRef), + Scalar(Box), Path(Name), Complex(AstId), } @@ -408,7 +408,7 @@ impl ConstRef { return Self::from_expr(expr, Some(lower_ctx.ast_id(&arg))); } } - Self::Scalar(LiteralConstRef::Unknown) + Self::Scalar(Box::new(LiteralConstRef::Unknown)) } pub(crate) fn from_const_param( @@ -452,10 +452,10 @@ impl ConstRef { ast::Expr::PathExpr(p) if is_path_ident(&p) => { match p.path().and_then(|it| it.segment()).and_then(|it| it.name_ref()) { Some(it) => Self::Path(it.as_name()), - None => Self::Scalar(LiteralConstRef::Unknown), + None => Self::Scalar(Box::new(LiteralConstRef::Unknown)), } } - ast::Expr::Literal(literal) => Self::Scalar(match literal.kind() { + ast::Expr::Literal(literal) => Self::Scalar(Box::new(match literal.kind() { ast::LiteralKind::IntNumber(num) => { num.value().map(LiteralConstRef::UInt).unwrap_or(LiteralConstRef::Unknown) } @@ -464,12 +464,12 @@ impl ConstRef { } ast::LiteralKind::Bool(f) => LiteralConstRef::Bool(f), _ => LiteralConstRef::Unknown, - }), + })), _ => { if let Some(ast_id) = ast_id { Self::Complex(ast_id) } else { - Self::Scalar(LiteralConstRef::Unknown) + Self::Scalar(Box::new(LiteralConstRef::Unknown)) } } } From 0e93e6fee7e7ae11064cd9abd92c1108e3872c32 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 25 Jul 2024 11:35:30 +0200 Subject: [PATCH 2/2] Make legacy_const_generics_indices thin by double boxing as its seldom used --- crates/hir-def/src/data.rs | 5 +++-- crates/hir-ty/src/infer/expr.rs | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/hir-def/src/data.rs b/crates/hir-def/src/data.rs index 3f862f69f8..3a3b540c13 100644 --- a/crates/hir-def/src/data.rs +++ b/crates/hir-def/src/data.rs @@ -40,7 +40,7 @@ pub struct FunctionData { pub attrs: Attrs, pub visibility: RawVisibility, pub abi: Option, - pub legacy_const_generics_indices: Box<[u32]>, + pub legacy_const_generics_indices: Option>>, pub rustc_allow_incoherent_impl: bool, flags: FnFlags, } @@ -91,7 +91,8 @@ impl FunctionData { .tt_values() .next() .map(parse_rustc_legacy_const_generics) - .unwrap_or_default(); + .filter(|it| !it.is_empty()) + .map(Box::new); let rustc_allow_incoherent_impl = attrs.by_key(&sym::rustc_allow_incoherent_impl).exists(); Arc::new(FunctionData { diff --git a/crates/hir-ty/src/infer/expr.rs b/crates/hir-ty/src/infer/expr.rs index 87233fa011..24479a027f 100644 --- a/crates/hir-ty/src/infer/expr.rs +++ b/crates/hir-ty/src/infer/expr.rs @@ -1950,25 +1950,25 @@ impl InferenceContext<'_> { }; let data = self.db.function_data(func); - if data.legacy_const_generics_indices.is_empty() { + let Some(legacy_const_generics_indices) = &data.legacy_const_generics_indices else { return Default::default(); - } + }; // only use legacy const generics if the param count matches with them - if data.params.len() + data.legacy_const_generics_indices.len() != args.len() { + if data.params.len() + legacy_const_generics_indices.len() != args.len() { if args.len() <= data.params.len() { return Default::default(); } else { // there are more parameters than there should be without legacy // const params; use them - let mut indices = data.legacy_const_generics_indices.clone(); + let mut indices = legacy_const_generics_indices.as_ref().clone(); indices.sort(); return indices; } } // check legacy const parameters - for (subst_idx, arg_idx) in data.legacy_const_generics_indices.iter().copied().enumerate() { + for (subst_idx, arg_idx) in legacy_const_generics_indices.iter().copied().enumerate() { let arg = match subst.at(Interner, subst_idx).constant(Interner) { Some(c) => c, None => continue, // not a const parameter? @@ -1981,7 +1981,7 @@ impl InferenceContext<'_> { self.infer_expr(args[arg_idx as usize], &expected); // FIXME: evaluate and unify with the const } - let mut indices = data.legacy_const_generics_indices.clone(); + let mut indices = legacy_const_generics_indices.as_ref().clone(); indices.sort(); indices }