From f7e6b186e1d2f3a31b8e21d0885e13f12f12d71b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 3 Apr 2021 20:56:53 +0200 Subject: Intern `ModPath` in `Import` Minor savings only --- crates/hir_def/src/item_tree.rs | 2 +- crates/hir_def/src/item_tree/lower.rs | 2 +- crates/hir_def/src/nameres/collector.rs | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) (limited to 'crates/hir_def/src') diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 69a313c7e..dd80cef23 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -529,7 +529,7 @@ impl Index> for ItemTree { /// A desugared `use` import. #[derive(Debug, Clone, Eq, PartialEq)] pub struct Import { - pub path: ModPath, + pub path: Interned, pub alias: Option, pub visibility: RawVisibilityId, pub is_glob: bool, diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index 5247379c5..ead7cd7a4 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs @@ -577,7 +577,7 @@ impl Ctx { &self.hygiene, |path, _use_tree, is_glob, alias| { imports.push(id(tree.imports.alloc(Import { - path, + path: Interned::new(path), alias, visibility, is_glob, diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index c8f494707..c2e445b68 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -23,6 +23,7 @@ use crate::{ attr::Attrs, db::DefDatabase, derive_macro_as_call_id, + intern::Interned, item_scope::{ImportType, PerNsGlobImports}, item_tree::{ self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroDef, MacroRules, Mod, ModItem, @@ -139,7 +140,7 @@ enum ImportSource { #[derive(Clone, Debug, Eq, PartialEq)] struct Import { - path: ModPath, + path: Interned, alias: Option, visibility: RawVisibility, is_glob: bool, @@ -181,7 +182,10 @@ impl Import { let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into()); let visibility = &tree[it.visibility]; Self { - path: ModPath::from_segments(PathKind::Plain, iter::once(it.name.clone())), + path: Interned::new(ModPath::from_segments( + PathKind::Plain, + iter::once(it.name.clone()), + )), alias: it.alias.clone(), visibility: visibility.clone(), is_glob: false, -- cgit v1.2.3 From ee4b5a34d8aa789ebc521926123fba79eebe5981 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 3 Apr 2021 20:58:42 +0200 Subject: Use bitflags to compress function properties Very minor savings, only 1 MB or so --- crates/hir_def/src/data.rs | 57 +++++++++++++++++++++++------- crates/hir_def/src/item_tree.rs | 31 ++++++++-------- crates/hir_def/src/item_tree/lower.rs | 66 +++++++++++++++++++++-------------- 3 files changed, 101 insertions(+), 53 deletions(-) (limited to 'crates/hir_def/src') diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index 31f994681..b409fb45c 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs @@ -10,7 +10,7 @@ use crate::{ body::Expander, db::DefDatabase, intern::Interned, - item_tree::{AssocItem, FunctionQualifier, ItemTreeId, ModItem, Param}, + item_tree::{AssocItem, FnFlags, ItemTreeId, ModItem, Param}, type_ref::{TraitRef, TypeBound, TypeRef}, visibility::RawVisibility, AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, @@ -23,14 +23,9 @@ pub struct FunctionData { pub params: Vec>, pub ret_type: Interned, pub attrs: Attrs, - /// True if the first param is `self`. This is relevant to decide whether this - /// can be called as a method. - pub has_self_param: bool, - pub has_body: bool, - pub qualifier: FunctionQualifier, - pub is_in_extern_block: bool, - pub is_varargs: bool, pub visibility: RawVisibility, + pub abi: Option>, + flags: FnFlags, } impl FunctionData { @@ -53,6 +48,11 @@ impl FunctionData { .next_back() .map_or(false, |param| matches!(item_tree[param], Param::Varargs)); + let mut flags = func.flags; + if is_varargs { + flags |= FnFlags::IS_VARARGS; + } + Arc::new(FunctionData { name: func.name.clone(), params: enabled_params @@ -64,14 +64,45 @@ impl FunctionData { .collect(), ret_type: func.ret_type.clone(), attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()), - has_self_param: func.has_self_param, - has_body: func.has_body, - qualifier: func.qualifier.clone(), - is_in_extern_block: func.is_in_extern_block, - is_varargs, visibility: item_tree[func.visibility].clone(), + abi: func.abi.clone(), + flags, }) } + + pub fn has_body(&self) -> bool { + self.flags.contains(FnFlags::HAS_BODY) + } + + /// True if the first param is `self`. This is relevant to decide whether this + /// can be called as a method. + pub fn has_self_param(&self) -> bool { + self.flags.contains(FnFlags::HAS_SELF_PARAM) + } + + pub fn is_default(&self) -> bool { + self.flags.contains(FnFlags::IS_DEFAULT) + } + + pub fn is_const(&self) -> bool { + self.flags.contains(FnFlags::IS_CONST) + } + + pub fn is_async(&self) -> bool { + self.flags.contains(FnFlags::IS_ASYNC) + } + + pub fn is_unsafe(&self) -> bool { + self.flags.contains(FnFlags::IS_UNSAFE) + } + + pub fn is_in_extern_block(&self) -> bool { + self.flags.contains(FnFlags::IS_IN_EXTERN_BLOCK) + } + + pub fn is_varargs(&self) -> bool { + self.flags.contains(FnFlags::IS_VARARGS) + } } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index dd80cef23..c6d700977 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -24,7 +24,7 @@ use la_arena::{Arena, Idx, RawIdx}; use profile::Count; use rustc_hash::FxHashMap; use smallvec::SmallVec; -use syntax::{ast, match_ast, SmolStr, SyntaxKind}; +use syntax::{ast, match_ast, SyntaxKind}; use crate::{ attr::{Attrs, RawAttrs}, @@ -556,15 +556,11 @@ pub struct Function { pub name: Name, pub visibility: RawVisibilityId, pub generic_params: GenericParamsId, - pub has_self_param: bool, - pub has_body: bool, - pub qualifier: FunctionQualifier, - /// Whether the function is located in an `extern` block (*not* whether it is an - /// `extern "abi" fn`). - pub is_in_extern_block: bool, + pub abi: Option>, pub params: IdRange, pub ret_type: Interned, pub ast_id: FileAstId, + pub(crate) flags: FnFlags, } #[derive(Debug, Clone, Eq, PartialEq)] @@ -573,13 +569,20 @@ pub enum Param { Varargs, } -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FunctionQualifier { - pub is_default: bool, - pub is_const: bool, - pub is_async: bool, - pub is_unsafe: bool, - pub abi: Option, +bitflags::bitflags! { + /// NOTE: Shared with `FunctionData` + pub(crate) struct FnFlags: u8 { + const HAS_SELF_PARAM = 1 << 0; + const HAS_BODY = 1 << 1; + const IS_DEFAULT = 1 << 2; + const IS_CONST = 1 << 3; + const IS_ASYNC = 1 << 4; + const IS_UNSAFE = 1 << 5; + /// Whether the function is located in an `extern` block (*not* whether it is an + /// `extern "abi" fn`). + const IS_IN_EXTERN_BLOCK = 1 << 6; + const IS_VARARGS = 1 << 7; + } } #[derive(Debug, Clone, Eq, PartialEq)] diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index ead7cd7a4..39e8403b0 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs @@ -395,39 +395,51 @@ impl Ctx { ret_type }; - let has_body = func.body().is_some(); + let abi = func.abi().map(|abi| { + // FIXME: Abi::abi() -> Option? + match abi.syntax().last_token() { + Some(tok) if tok.kind() == SyntaxKind::STRING => { + // FIXME: Better way to unescape? + Interned::new_str(tok.text().trim_matches('"')) + } + _ => { + // `extern` default to be `extern "C"`. + Interned::new_str("C") + } + } + }); let ast_id = self.source_ast_id_map.ast_id(func); - let qualifier = FunctionQualifier { - is_default: func.default_token().is_some(), - is_const: func.const_token().is_some(), - is_async: func.async_token().is_some(), - is_unsafe: func.unsafe_token().is_some(), - abi: func.abi().map(|abi| { - // FIXME: Abi::abi() -> Option? - match abi.syntax().last_token() { - Some(tok) if tok.kind() == SyntaxKind::STRING => { - // FIXME: Better way to unescape? - tok.text().trim_matches('"').into() - } - _ => { - // `extern` default to be `extern "C"`. - "C".into() - } - } - }), - }; + + let mut flags = FnFlags::empty(); + if func.body().is_some() { + flags |= FnFlags::HAS_BODY; + } + if has_self_param { + flags |= FnFlags::HAS_SELF_PARAM; + } + if func.default_token().is_some() { + flags |= FnFlags::IS_DEFAULT; + } + if func.const_token().is_some() { + flags |= FnFlags::IS_CONST; + } + if func.async_token().is_some() { + flags |= FnFlags::IS_ASYNC; + } + if func.unsafe_token().is_some() { + flags |= FnFlags::IS_UNSAFE; + } + let mut res = Function { name, visibility, generic_params: GenericParamsId::EMPTY, - has_self_param, - has_body, - qualifier, - is_in_extern_block: false, + abi, params, ret_type: Interned::new(ret_type), ast_id, + flags, }; res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func); @@ -640,8 +652,10 @@ impl Ctx { ast::ExternItem::Fn(ast) => { let func_id = self.lower_function(&ast)?; let func = &mut self.data().functions[func_id.index]; - func.qualifier.is_unsafe = is_intrinsic_fn_unsafe(&func.name); - func.is_in_extern_block = true; + if is_intrinsic_fn_unsafe(&func.name) { + func.flags |= FnFlags::IS_UNSAFE; + } + func.flags |= FnFlags::IS_IN_EXTERN_BLOCK; func_id.into() } ast::ExternItem::Static(ast) => { -- cgit v1.2.3