Skip to content

Commit

Permalink
Merge #8284
Browse files Browse the repository at this point in the history
8284: Reduce memory usage by using global `Arc`-based interning r=jonas-schievink a=jonas-schievink

This saves around 50 mb when running `analysis-stats` on r-a itself. Not a lot, but this infra can be easily reused to intern more stuff.

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
  • Loading branch information
bors[bot] and jonas-schievink committed Apr 1, 2021
2 parents ea8feca + afd83e0 commit ebaf8ab
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 136 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl HirDisplay for Function {
let ret_type = if !qual.is_async {
&data.ret_type
} else {
match &data.ret_type {
match &*data.ret_type {
TypeRef::ImplTrait(bounds) => match &bounds[0] {
TypeBound::Path(path) => {
path.segments().iter().last().unwrap().args_and_bindings.unwrap().bindings
Expand Down
8 changes: 4 additions & 4 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ impl SelfParam {
func_data
.params
.first()
.map(|param| match *param {
.map(|param| match &**param {
TypeRef::Reference(.., mutability) => match mutability {
hir_def::type_ref::Mutability::Shared => Access::Shared,
hir_def::type_ref::Mutability::Mut => Access::Exclusive,
Expand Down Expand Up @@ -1011,7 +1011,7 @@ impl Const {
}

pub fn type_ref(self, db: &dyn HirDatabase) -> TypeRef {
db.const_data(self.id).type_ref.clone()
db.const_data(self.id).type_ref.as_ref().clone()
}
}

Expand Down Expand Up @@ -1101,7 +1101,7 @@ impl TypeAlias {
}

pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> {
db.type_alias_data(self.id).type_ref.clone()
db.type_alias_data(self.id).type_ref.as_deref().cloned()
}

pub fn ty(self, db: &dyn HirDatabase) -> Type {
Expand Down Expand Up @@ -1615,7 +1615,7 @@ impl Impl {
// FIXME: the return type is wrong. This should be a hir version of
// `TraitRef` (ie, resolved `TypeRef`).
pub fn trait_(self, db: &dyn HirDatabase) -> Option<TraitRef> {
db.impl_data(self.id).target_trait.clone()
db.impl_data(self.id).target_trait.as_deref().cloned()
}

pub fn self_ty(self, db: &dyn HirDatabase) -> Type {
Expand Down
1 change: 1 addition & 0 deletions crates/hir_def/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ doctest = false

[dependencies]
cov-mark = { version = "1.1", features = ["thread-local"] }
dashmap = { version = "4.0.2", features = ["raw-api"] }
log = "0.4.8"
once_cell = "1.3.1"
rustc-hash = "1.1.0"
Expand Down
9 changes: 5 additions & 4 deletions crates/hir_def/src/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tt::{Delimiter, DelimiterKind, Leaf, Subtree, TokenTree};
use crate::{
body::{CfgExpander, LowerCtx},
db::DefDatabase,
intern::Interned,
item_tree::{AttrOwner, Field, Fields, ItemTree, ModItem, RawVisibilityId},
src::HasChildSource,
src::HasSource,
Expand Down Expand Up @@ -58,7 +59,7 @@ pub enum VariantData {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldData {
pub name: Name,
pub type_ref: TypeRef,
pub type_ref: Interned<TypeRef>,
pub visibility: RawVisibility,
}

Expand Down Expand Up @@ -292,7 +293,7 @@ fn lower_struct(
|| Either::Left(fd.clone()),
|| FieldData {
name: Name::new_tuple_field(i),
type_ref: TypeRef::from_ast_opt(&ctx, fd.ty()),
type_ref: Interned::new(TypeRef::from_ast_opt(&ctx, fd.ty())),
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
},
);
Expand All @@ -309,7 +310,7 @@ fn lower_struct(
|| Either::Right(fd.clone()),
|| FieldData {
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
type_ref: TypeRef::from_ast_opt(&ctx, fd.ty()),
type_ref: Interned::new(TypeRef::from_ast_opt(&ctx, fd.ty())),
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
},
);
Expand Down Expand Up @@ -358,7 +359,7 @@ fn lower_field(
) -> FieldData {
FieldData {
name: field.name.clone(),
type_ref: item_tree[field.type_ref].clone(),
type_ref: field.type_ref.clone(),
visibility: item_tree[override_visibility.unwrap_or(field.visibility)].clone(),
}
}
7 changes: 4 additions & 3 deletions crates/hir_def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tt::Subtree;

use crate::{
db::DefDatabase,
intern::Interned,
item_tree::{ItemTreeId, ItemTreeNode},
nameres::ModuleSource,
path::{ModPath, PathKind},
Expand Down Expand Up @@ -98,7 +99,7 @@ impl RawAttrs {
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
index: i as u32,
input: Some(AttrInput::Literal(SmolStr::new(doc))),
path: ModPath::from(hir_expand::name!(doc)),
path: Interned::new(ModPath::from(hir_expand::name!(doc))),
}),
})
.collect::<Arc<_>>();
Expand Down Expand Up @@ -510,7 +511,7 @@ impl AttrSourceMap {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attr {
index: u32,
pub(crate) path: ModPath,
pub(crate) path: Interned<ModPath>,
pub(crate) input: Option<AttrInput>,
}

Expand All @@ -524,7 +525,7 @@ pub enum AttrInput {

impl Attr {
fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> {
let path = ModPath::from_src(ast.path()?, hygiene)?;
let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?);
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
let value = match lit.kind() {
ast::LiteralKind::String(string) => string.value()?.into(),
Expand Down
29 changes: 15 additions & 14 deletions crates/hir_def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
attr::Attrs,
body::Expander,
db::DefDatabase,
intern::Interned,
item_tree::{AssocItem, FunctionQualifier, ItemTreeId, ModItem, Param},
type_ref::{TraitRef, TypeBound, TypeRef},
visibility::RawVisibility,
Expand All @@ -19,8 +20,8 @@ use crate::{
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionData {
pub name: Name,
pub params: Vec<TypeRef>,
pub ret_type: TypeRef,
pub params: Vec<Interned<TypeRef>>,
pub ret_type: Interned<TypeRef>,
pub attrs: Attrs,
/// True if the first param is `self`. This is relevant to decide whether this
/// can be called as a method.
Expand Down Expand Up @@ -57,11 +58,11 @@ impl FunctionData {
params: enabled_params
.clone()
.filter_map(|id| match &item_tree[id] {
Param::Normal(ty) => Some(item_tree[*ty].clone()),
Param::Normal(ty) => Some(ty.clone()),
Param::Varargs => None,
})
.collect(),
ret_type: item_tree[func.ret_type].clone(),
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,
Expand All @@ -76,7 +77,7 @@ impl FunctionData {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeAliasData {
pub name: Name,
pub type_ref: Option<TypeRef>,
pub type_ref: Option<Interned<TypeRef>>,
pub visibility: RawVisibility,
pub is_extern: bool,
/// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl).
Expand All @@ -94,7 +95,7 @@ impl TypeAliasData {

Arc::new(TypeAliasData {
name: typ.name.clone(),
type_ref: typ.type_ref.map(|id| item_tree[id].clone()),
type_ref: typ.type_ref.clone(),
visibility: item_tree[typ.visibility].clone(),
is_extern: typ.is_extern,
bounds: typ.bounds.to_vec(),
Expand Down Expand Up @@ -156,8 +157,8 @@ impl TraitData {

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImplData {
pub target_trait: Option<TraitRef>,
pub self_ty: TypeRef,
pub target_trait: Option<Interned<TraitRef>>,
pub self_ty: Interned<TypeRef>,
pub items: Vec<AssocItemId>,
pub is_negative: bool,
}
Expand All @@ -169,8 +170,8 @@ impl ImplData {

let item_tree = impl_loc.id.item_tree(db);
let impl_def = &item_tree[impl_loc.id.value];
let target_trait = impl_def.target_trait.map(|id| item_tree[id].clone());
let self_ty = item_tree[impl_def.self_ty].clone();
let target_trait = impl_def.target_trait.clone();
let self_ty = impl_def.self_ty.clone();
let is_negative = impl_def.is_negative;
let module_id = impl_loc.container;
let container = AssocContainerId::ImplId(id);
Expand All @@ -195,7 +196,7 @@ impl ImplData {
pub struct ConstData {
/// const _: () = ();
pub name: Option<Name>,
pub type_ref: TypeRef,
pub type_ref: Interned<TypeRef>,
pub visibility: RawVisibility,
}

Expand All @@ -207,7 +208,7 @@ impl ConstData {

Arc::new(ConstData {
name: konst.name.clone(),
type_ref: item_tree[konst.type_ref].clone(),
type_ref: konst.type_ref.clone(),
visibility: item_tree[konst.visibility].clone(),
})
}
Expand All @@ -216,7 +217,7 @@ impl ConstData {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StaticData {
pub name: Option<Name>,
pub type_ref: TypeRef,
pub type_ref: Interned<TypeRef>,
pub visibility: RawVisibility,
pub mutable: bool,
pub is_extern: bool,
Expand All @@ -230,7 +231,7 @@ impl StaticData {

Arc::new(StaticData {
name: Some(statik.name.clone()),
type_ref: item_tree[statik.type_ref].clone(),
type_ref: statik.type_ref.clone(),
visibility: item_tree[statik.visibility].clone(),
mutable: statik.mutable,
is_extern: statik.is_extern,
Expand Down
Loading

0 comments on commit ebaf8ab

Please sign in to comment.