8334: Intern and shrink more data to reduce memory usage r=jonas-schievink a=jonas-schievink

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-04-05 02:29:09 +00:00 committed by GitHub
commit 58924cfae1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 75 additions and 99 deletions

View file

@ -13,6 +13,7 @@ use crate::{
data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData},
generics::GenericParams,
import_map::ImportMap,
intern::Interned,
item_tree::ItemTree,
lang_item::{LangItemTarget, LangItems},
nameres::DefMap,
@ -113,7 +114,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>;
#[salsa::invoke(GenericParams::generic_params_query)]
fn generic_params(&self, def: GenericDefId) -> Arc<GenericParams>;
fn generic_params(&self, def: GenericDefId) -> Interned<GenericParams>;
#[salsa::invoke(Attrs::variants_attrs_query)]
fn variants_attrs(&self, def: EnumId) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>>;

View file

@ -2,7 +2,6 @@
//! structs, impls, traits, etc. This module provides a common HIR for these
//! generic parameters. See also the `Generics` type and the `generics_of` query
//! in rustc.
use std::sync::Arc;
use base_db::FileId;
use either::Either;
@ -18,6 +17,7 @@ use crate::{
child_by_source::ChildBySource,
db::DefDatabase,
dyn_map::DynMap,
intern::Interned,
keys,
src::{HasChildSource, HasSource},
type_ref::{LifetimeRef, TypeBound, TypeRef},
@ -26,27 +26,27 @@ use crate::{
};
/// Data about a generic type parameter (to a function, struct, impl, ...).
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct TypeParamData {
pub name: Option<Name>,
pub default: Option<TypeRef>,
pub default: Option<Interned<TypeRef>>,
pub provenance: TypeParamProvenance,
}
/// Data about a generic lifetime parameter (to a function, struct, impl, ...).
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct LifetimeParamData {
pub name: Name,
}
/// Data about a generic const parameter (to a function, struct, impl, ...).
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct ConstParamData {
pub name: Name,
pub ty: TypeRef,
pub ty: Interned<TypeRef>,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum TypeParamProvenance {
TypeParamList,
TraitSelf,
@ -54,7 +54,7 @@ pub enum TypeParamProvenance {
}
/// Data about the generic parameters of a function, struct, impl, etc.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash)]
pub struct GenericParams {
pub types: Arena<TypeParamData>,
pub lifetimes: Arena<LifetimeParamData>,
@ -66,16 +66,16 @@ pub struct GenericParams {
/// where clauses like `where T: Foo + Bar` are turned into multiple of these.
/// It might still result in multiple actual predicates though, because of
/// associated type bindings like `Iterator<Item = u32>`.
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum WherePredicate {
TypeBound { target: WherePredicateTypeTarget, bound: TypeBound },
Lifetime { target: LifetimeRef, bound: LifetimeRef },
ForLifetime { lifetimes: Box<[Name]>, target: WherePredicateTypeTarget, bound: TypeBound },
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum WherePredicateTypeTarget {
TypeRef(TypeRef),
TypeRef(Interned<TypeRef>),
/// For desugared where predicates that can directly refer to a type param.
TypeParam(LocalTypeParamId),
}
@ -91,7 +91,7 @@ impl GenericParams {
pub(crate) fn generic_params_query(
db: &dyn DefDatabase,
def: GenericDefId,
) -> Arc<GenericParams> {
) -> Interned<GenericParams> {
let _p = profile::span("generic_params_query");
let generics = match def {
@ -99,47 +99,49 @@ impl GenericParams {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
tree[item.generic_params].clone()
item.generic_params.clone()
}
GenericDefId::AdtId(AdtId::StructId(id)) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
tree[item.generic_params].clone()
item.generic_params.clone()
}
GenericDefId::AdtId(AdtId::EnumId(id)) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
tree[item.generic_params].clone()
item.generic_params.clone()
}
GenericDefId::AdtId(AdtId::UnionId(id)) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
tree[item.generic_params].clone()
item.generic_params.clone()
}
GenericDefId::TraitId(id) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
tree[item.generic_params].clone()
item.generic_params.clone()
}
GenericDefId::TypeAliasId(id) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
tree[item.generic_params].clone()
item.generic_params.clone()
}
GenericDefId::ImplId(id) => {
let id = id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
tree[item.generic_params].clone()
item.generic_params.clone()
}
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
Interned::new(GenericParams::default())
}
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => GenericParams::default(),
};
Arc::new(generics)
generics
}
fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) {
@ -217,6 +219,7 @@ impl GenericParams {
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => FileId(!0).into(),
};
generics.shrink_to_fit();
(generics, InFile::new(file_id, sm))
}
@ -256,7 +259,8 @@ impl GenericParams {
for type_param in params.type_params() {
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
// FIXME: Use `Path::from_src`
let default = type_param.default_type().map(|it| TypeRef::from_ast(lower_ctx, it));
let default =
type_param.default_type().map(|it| Interned::new(TypeRef::from_ast(lower_ctx, it)));
let param = TypeParamData {
name: Some(name.clone()),
default,
@ -280,7 +284,7 @@ impl GenericParams {
for const_param in params.const_params() {
let name = const_param.name().map_or_else(Name::missing, |it| it.as_name());
let ty = const_param.ty().map_or(TypeRef::Error, |it| TypeRef::from_ast(lower_ctx, it));
let param = ConstParamData { name, ty };
let param = ConstParamData { name, ty: Interned::new(ty) };
let param_id = self.consts.alloc(param);
sm.const_params.insert(param_id, const_param.clone());
}
@ -334,11 +338,11 @@ impl GenericParams {
(Either::Left(type_ref), bound) => match hrtb_lifetimes {
Some(hrtb_lifetimes) => WherePredicate::ForLifetime {
lifetimes: hrtb_lifetimes.clone(),
target: WherePredicateTypeTarget::TypeRef(type_ref),
target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
bound,
},
None => WherePredicate::TypeBound {
target: WherePredicateTypeTarget::TypeRef(type_ref),
target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
bound,
},
},
@ -369,6 +373,14 @@ impl GenericParams {
});
}
pub(crate) fn shrink_to_fit(&mut self) {
let Self { consts, lifetimes, types, where_predicates } = self;
consts.shrink_to_fit();
lifetimes.shrink_to_fit();
types.shrink_to_fit();
where_predicates.shrink_to_fit();
}
pub fn find_type_by_name(&self, name: &Name) -> Option<LocalTypeParamId> {
self.types
.iter()

View file

@ -14,6 +14,8 @@ use dashmap::{lock::RwLockWriteGuard, DashMap, SharedValue};
use once_cell::sync::OnceCell;
use rustc_hash::FxHasher;
use crate::generics::GenericParams;
type InternMap<T> = DashMap<Arc<T>, (), BuildHasherDefault<FxHasher>>;
type Guard<T> =
RwLockWriteGuard<'static, HashMap<Arc<T>, SharedValue<()>, BuildHasherDefault<FxHasher>>>;
@ -194,4 +196,10 @@ macro_rules! impl_internable {
)+ };
}
impl_internable!(crate::type_ref::TypeRef, crate::type_ref::TraitRef, crate::path::ModPath, str);
impl_internable!(
crate::type_ref::TypeRef,
crate::type_ref::TraitRef,
crate::path::ModPath,
GenericParams,
str
);

View file

@ -58,13 +58,6 @@ impl fmt::Debug for RawVisibilityId {
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct GenericParamsId(u32);
impl GenericParamsId {
pub const EMPTY: Self = GenericParamsId(u32::max_value());
}
/// The item tree of a source file.
#[derive(Debug, Default, Eq, PartialEq)]
pub struct ItemTree {
@ -146,7 +139,6 @@ impl ItemTree {
macro_rules,
macro_defs,
vis,
generics,
inner_items,
} = &mut **data;
@ -170,7 +162,6 @@ impl ItemTree {
macro_defs.shrink_to_fit();
vis.arena.shrink_to_fit();
generics.arena.shrink_to_fit();
inner_items.shrink_to_fit();
}
@ -241,32 +232,6 @@ static VIS_PUB: RawVisibility = RawVisibility::Public;
static VIS_PRIV: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)));
static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate));
#[derive(Default, Debug, Eq, PartialEq)]
struct GenericParamsStorage {
arena: Arena<GenericParams>,
}
impl GenericParamsStorage {
fn alloc(&mut self, params: GenericParams) -> GenericParamsId {
if params.types.is_empty()
&& params.lifetimes.is_empty()
&& params.consts.is_empty()
&& params.where_predicates.is_empty()
{
return GenericParamsId::EMPTY;
}
GenericParamsId(self.arena.alloc(params).into_raw().into())
}
}
static EMPTY_GENERICS: GenericParams = GenericParams {
types: Arena::new(),
lifetimes: Arena::new(),
consts: Arena::new(),
where_predicates: Vec::new(),
};
#[derive(Default, Debug, Eq, PartialEq)]
struct ItemTreeData {
imports: Arena<Import>,
@ -289,7 +254,6 @@ struct ItemTreeData {
macro_defs: Arena<MacroDef>,
vis: ItemVisibilities,
generics: GenericParamsStorage,
inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>,
}
@ -508,17 +472,6 @@ impl Index<RawVisibilityId> for ItemTree {
}
}
impl Index<GenericParamsId> for ItemTree {
type Output = GenericParams;
fn index(&self, index: GenericParamsId) -> &Self::Output {
match index {
GenericParamsId::EMPTY => &EMPTY_GENERICS,
_ => &self.data().generics.arena[Idx::from_raw(index.0.into())],
}
}
}
impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
type Output = N;
fn index(&self, id: FileItemTreeId<N>) -> &N {
@ -555,7 +508,7 @@ pub struct ExternCrate {
pub struct Function {
pub name: Name,
pub visibility: RawVisibilityId,
pub generic_params: GenericParamsId,
pub generic_params: Interned<GenericParams>,
pub abi: Option<Interned<str>>,
pub params: IdRange<Param>,
pub ret_type: Interned<TypeRef>,
@ -590,7 +543,7 @@ impl FnFlags {
pub struct Struct {
pub name: Name,
pub visibility: RawVisibilityId,
pub generic_params: GenericParamsId,
pub generic_params: Interned<GenericParams>,
pub fields: Fields,
pub ast_id: FileAstId<ast::Struct>,
pub kind: StructDefKind,
@ -610,7 +563,7 @@ pub enum StructDefKind {
pub struct Union {
pub name: Name,
pub visibility: RawVisibilityId,
pub generic_params: GenericParamsId,
pub generic_params: Interned<GenericParams>,
pub fields: Fields,
pub ast_id: FileAstId<ast::Union>,
}
@ -619,7 +572,7 @@ pub struct Union {
pub struct Enum {
pub name: Name,
pub visibility: RawVisibilityId,
pub generic_params: GenericParamsId,
pub generic_params: Interned<GenericParams>,
pub variants: IdRange<Variant>,
pub ast_id: FileAstId<ast::Enum>,
}
@ -648,7 +601,7 @@ pub struct Static {
pub struct Trait {
pub name: Name,
pub visibility: RawVisibilityId,
pub generic_params: GenericParamsId,
pub generic_params: Interned<GenericParams>,
pub is_auto: bool,
pub is_unsafe: bool,
pub bounds: Box<[TypeBound]>,
@ -658,7 +611,7 @@ pub struct Trait {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Impl {
pub generic_params: GenericParamsId,
pub generic_params: Interned<GenericParams>,
pub target_trait: Option<Interned<TraitRef>>,
pub self_ty: Interned<TypeRef>,
pub is_negative: bool,
@ -672,7 +625,7 @@ pub struct TypeAlias {
pub visibility: RawVisibilityId,
/// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
pub bounds: Box<[TypeBound]>,
pub generic_params: GenericParamsId,
pub generic_params: Interned<GenericParams>,
pub type_ref: Option<Interned<TypeRef>>,
pub is_extern: bool,
pub ast_id: FileAstId<ast::TypeAlias>,

View file

@ -434,7 +434,7 @@ impl Ctx {
let mut res = Function {
name,
visibility,
generic_params: GenericParamsId::EMPTY,
generic_params: Interned::new(GenericParams::default()),
abi,
params,
ret_type: Interned::new(ret_type),
@ -682,7 +682,7 @@ impl Ctx {
&mut self,
owner: GenericsOwner<'_>,
node: &impl ast::GenericParamsOwner,
) -> GenericParamsId {
) -> Interned<GenericParams> {
// Generics are part of item headers and may contain inner items we need to collect.
if let Some(params) = node.generic_param_list() {
self.collect_inner_items(params.syntax());
@ -698,7 +698,7 @@ impl Ctx {
&mut self,
owner: GenericsOwner<'_>,
node: &impl ast::GenericParamsOwner,
) -> GenericParamsId {
) -> Interned<GenericParams> {
let mut sm = &mut Default::default();
let mut generics = GenericParams::default();
match owner {
@ -739,7 +739,8 @@ impl Ctx {
}
}
self.data().generics.alloc(generics)
generics.shrink_to_fit();
Interned::new(generics)
}
fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec<TypeBound> {

View file

@ -27,6 +27,7 @@ pub mod dyn_map;
pub mod keys;
pub mod item_tree;
pub mod intern;
pub mod adt;
pub mod data;
@ -49,7 +50,6 @@ pub mod import_map;
#[cfg(test)]
mod test_db;
mod intern;
use std::{
hash::{Hash, Hasher},

View file

@ -122,7 +122,7 @@ impl ModPath {
pub struct Path {
/// Type based path like `<T>::foo`.
/// Note that paths like `<Type as Trait>::foo` are desugard to `Trait::<Self=Type>::foo`.
type_anchor: Option<Box<TypeRef>>,
type_anchor: Option<Interned<TypeRef>>,
mod_path: Interned<ModPath>,
/// Invariant: the same len as `self.mod_path.segments`
generic_args: Vec<Option<Arc<GenericArgs>>>,

View file

@ -69,7 +69,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
match trait_ref {
// <T>::foo
None => {
type_anchor = Some(Box::new(self_type));
type_anchor = Some(Interned::new(self_type));
kind = PathKind::Plain;
}
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo

View file

@ -14,6 +14,7 @@ use crate::{
db::DefDatabase,
expr::{ExprId, LabelId, PatId},
generics::GenericParams,
intern::Interned,
item_scope::{BuiltinShadowMode, BUILTIN_SCOPE},
nameres::DefMap,
path::{ModPath, PathKind},
@ -50,7 +51,7 @@ enum Scope {
/// All the items and imported names of a module
ModuleScope(ModuleItemMap),
/// Brings the generic parameters of an item into scope
GenericParams { def: GenericDefId, params: Arc<GenericParams> },
GenericParams { def: GenericDefId, params: Interned<GenericParams> },
/// Brings `Self` in `impl` block into scope
ImplDefScope(ImplId),
/// Brings `Self` in enum, struct and union definitions into scope

View file

@ -11,7 +11,7 @@ use crate::{
nameres::DefMap,
path::{ModPath, PathKind},
resolver::HasResolver,
FunctionId, HasModule, LocalFieldId, ModuleDefId, ModuleId, VariantId,
FunctionId, HasModule, LocalFieldId, ModuleId, VariantId,
};
/// Visibility of an item, not yet resolved.
@ -25,7 +25,7 @@ pub enum RawVisibility {
}
impl RawVisibility {
pub(crate) const fn private() -> RawVisibility {
pub(crate) fn private() -> RawVisibility {
RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)))
}
@ -217,6 +217,6 @@ pub(crate) fn field_visibilities_query(
/// Resolve visibility of a function.
pub(crate) fn function_visibility_query(db: &dyn DefDatabase, def: FunctionId) -> Visibility {
let resolver = ModuleDefId::from(def).module(db).unwrap().resolver(db);
let resolver = def.resolver(db);
db.function_data(def).visibility.resolve(db, &resolver)
}

View file

@ -9,6 +9,7 @@ use hir_def::{
generics::{
GenericParams, TypeParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
},
intern::Interned,
path::Path,
resolver::{HasResolver, TypeNs},
type_ref::TypeRef,
@ -32,11 +33,10 @@ fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec<TraitId> {
.filter_map(|pred| match pred {
WherePredicate::ForLifetime { target, bound, .. }
| WherePredicate::TypeBound { target, bound } => match target {
WherePredicateTypeTarget::TypeRef(TypeRef::Path(p))
if p == &Path::from(name![Self]) =>
{
bound.as_path()
}
WherePredicateTypeTarget::TypeRef(type_ref) => match &**type_ref {
TypeRef::Path(p) if p == &Path::from(name![Self]) => bound.as_path(),
_ => None,
},
WherePredicateTypeTarget::TypeParam(local_id) if Some(*local_id) == trait_self => {
bound.as_path()
}
@ -159,7 +159,7 @@ pub(crate) fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics {
#[derive(Debug)]
pub(crate) struct Generics {
def: GenericDefId,
pub(crate) params: Arc<GenericParams>,
pub(crate) params: Interned<GenericParams>,
parent_generics: Option<Box<Generics>>,
}

View file

@ -90,7 +90,7 @@ impl<T> Idx<T> {
}
/// Yet another index-based arena.
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Arena<T> {
data: Vec<T>,
}