Auto merge of #16329 - Veykril:perfperf, r=Veykril

internal: Some minor perf/memory improvements
This commit is contained in:
bors 2024-01-09 20:51:23 +00:00
commit ae6e737724
16 changed files with 317 additions and 269 deletions

View File

@ -44,6 +44,7 @@ pub trait Upcast<T: ?Sized> {
} }
pub const DEFAULT_PARSE_LRU_CAP: usize = 128; pub const DEFAULT_PARSE_LRU_CAP: usize = 128;
pub const DEFAULT_BORROWCK_LRU_CAP: usize = 256;
pub trait FileLoader { pub trait FileLoader {
/// Text of the file. /// Text of the file.

View File

@ -355,7 +355,7 @@ fn parse_comma_sep<S>(subtree: &tt::Subtree<S>) -> Vec<SmolStr> {
} }
impl AttrsWithOwner { impl AttrsWithOwner {
pub(crate) fn attrs_with_owner(db: &dyn DefDatabase, owner: AttrDefId) -> Self { pub fn attrs_with_owner(db: &dyn DefDatabase, owner: AttrDefId) -> Self {
Self { attrs: db.attrs(owner), owner } Self { attrs: db.attrs(owner), owner }
} }

View File

@ -210,13 +210,10 @@ fn fields_attrs_source_map(
#[salsa::invoke(AttrsWithOwner::attrs_query)] #[salsa::invoke(AttrsWithOwner::attrs_query)]
fn attrs(&self, def: AttrDefId) -> Attrs; fn attrs(&self, def: AttrDefId) -> Attrs;
#[salsa::transparent]
#[salsa::invoke(lang_item::lang_attr_query)] #[salsa::invoke(lang_item::lang_attr_query)]
fn lang_attr(&self, def: AttrDefId) -> Option<LangItem>; fn lang_attr(&self, def: AttrDefId) -> Option<LangItem>;
#[salsa::transparent]
#[salsa::invoke(AttrsWithOwner::attrs_with_owner)]
fn attrs_with_owner(&self, def: AttrDefId) -> AttrsWithOwner;
// endregion:attrs // endregion:attrs
#[salsa::invoke(LangItems::lang_item_query)] #[salsa::invoke(LangItems::lang_item_query)]
@ -240,7 +237,7 @@ fn fields_attrs_source_map(
// endregion:visibilities // endregion:visibilities
#[salsa::invoke(LangItems::crate_lang_items_query)] #[salsa::invoke(LangItems::crate_lang_items_query)]
fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>; fn crate_lang_items(&self, krate: CrateId) -> Option<Arc<LangItems>>;
fn crate_supports_no_std(&self, crate_id: CrateId) -> bool; fn crate_supports_no_std(&self, crate_id: CrateId) -> bool;
} }

View File

@ -107,11 +107,11 @@ pub fn is_trait_self(&self) -> bool {
impl_from!(TypeParamData, ConstParamData for TypeOrConstParamData); impl_from!(TypeParamData, ConstParamData for TypeOrConstParamData);
/// Data about the generic parameters of a function, struct, impl, etc. /// Data about the generic parameters of a function, struct, impl, etc.
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash)] #[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct GenericParams { pub struct GenericParams {
pub type_or_consts: Arena<TypeOrConstParamData>, pub type_or_consts: Arena<TypeOrConstParamData>,
pub lifetimes: Arena<LifetimeParamData>, pub lifetimes: Arena<LifetimeParamData>,
pub where_predicates: Vec<WherePredicate>, pub where_predicates: Box<[WherePredicate]>,
} }
/// A single predicate from a where clause, i.e. `where Type: Trait`. Combined /// A single predicate from a where clause, i.e. `where Type: Trait`. Combined
@ -142,109 +142,14 @@ pub enum WherePredicateTypeTarget {
TypeOrConstParam(LocalTypeOrConstParamId), TypeOrConstParam(LocalTypeOrConstParamId),
} }
impl GenericParams { #[derive(Clone, Default)]
/// Iterator of type_or_consts field pub(crate) struct GenericParamsCollector {
pub fn iter( pub(crate) type_or_consts: Arena<TypeOrConstParamData>,
&self, lifetimes: Arena<LifetimeParamData>,
) -> impl DoubleEndedIterator<Item = (Idx<TypeOrConstParamData>, &TypeOrConstParamData)> { where_predicates: Vec<WherePredicate>,
self.type_or_consts.iter() }
}
pub(crate) fn generic_params_query(
db: &dyn DefDatabase,
def: GenericDefId,
) -> Interned<GenericParams> {
let _p = profile::span("generic_params_query");
let krate = def.module(db).krate;
let cfg_options = db.crate_graph();
let cfg_options = &cfg_options[krate].cfg_options;
// Returns the generic parameters that are enabled under the current `#[cfg]` options
let enabled_params = |params: &Interned<GenericParams>, item_tree: &ItemTree| {
let enabled = |param| item_tree.attrs(db, krate, param).is_cfg_enabled(cfg_options);
// In the common case, no parameters will by disabled by `#[cfg]` attributes.
// Therefore, make a first pass to check if all parameters are enabled and, if so,
// clone the `Interned<GenericParams>` instead of recreating an identical copy.
let all_type_or_consts_enabled =
params.type_or_consts.iter().all(|(idx, _)| enabled(idx.into()));
let all_lifetimes_enabled = params.lifetimes.iter().all(|(idx, _)| enabled(idx.into()));
if all_type_or_consts_enabled && all_lifetimes_enabled {
params.clone()
} else {
Interned::new(GenericParams {
type_or_consts: all_type_or_consts_enabled
.then(|| params.type_or_consts.clone())
.unwrap_or_else(|| {
params
.type_or_consts
.iter()
.filter_map(|(idx, param)| {
enabled(idx.into()).then(|| param.clone())
})
.collect()
}),
lifetimes: all_lifetimes_enabled
.then(|| params.lifetimes.clone())
.unwrap_or_else(|| {
params
.lifetimes
.iter()
.filter_map(|(idx, param)| {
enabled(idx.into()).then(|| param.clone())
})
.collect()
}),
where_predicates: params.where_predicates.clone(),
})
}
};
macro_rules! id_to_generics {
($id:ident) => {{
let id = $id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
enabled_params(&item.generic_params, &tree)
}};
}
match def {
GenericDefId::FunctionId(id) => {
let loc = id.lookup(db);
let tree = loc.id.item_tree(db);
let item = &tree[loc.id.value];
let enabled_params = enabled_params(&item.explicit_generic_params, &tree);
let mut generic_params = GenericParams::clone(&enabled_params);
let module = loc.container.module(db);
let func_data = db.function_data(id);
// Don't create an `Expander` if not needed since this
// could cause a reparse after the `ItemTree` has been created due to the spanmap.
let mut expander =
Lazy::new(|| (module.def_map(db), Expander::new(db, loc.id.file_id(), module)));
for param in func_data.params.iter() {
generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
}
Interned::new(generic_params)
}
GenericDefId::AdtId(AdtId::StructId(id)) => id_to_generics!(id),
GenericDefId::AdtId(AdtId::EnumId(id)) => id_to_generics!(id),
GenericDefId::AdtId(AdtId::UnionId(id)) => id_to_generics!(id),
GenericDefId::TraitId(id) => id_to_generics!(id),
GenericDefId::TraitAliasId(id) => id_to_generics!(id),
GenericDefId::TypeAliasId(id) => id_to_generics!(id),
GenericDefId::ImplId(id) => id_to_generics!(id),
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
Interned::new(GenericParams::default())
}
}
}
impl GenericParamsCollector {
pub(crate) fn fill( pub(crate) fn fill(
&mut self, &mut self,
lower_ctx: &LowerCtx<'_>, lower_ctx: &LowerCtx<'_>,
@ -444,11 +349,131 @@ pub(crate) fn fill_implicit_impl_trait_args(
}); });
} }
pub(crate) fn shrink_to_fit(&mut self) { pub(crate) fn finish(self) -> GenericParams {
let Self { lifetimes, type_or_consts: types, where_predicates } = self; let Self { mut lifetimes, mut type_or_consts, where_predicates } = self;
lifetimes.shrink_to_fit(); lifetimes.shrink_to_fit();
types.shrink_to_fit(); type_or_consts.shrink_to_fit();
where_predicates.shrink_to_fit(); GenericParams {
type_or_consts,
lifetimes,
where_predicates: where_predicates.into_boxed_slice(),
}
}
}
impl GenericParams {
/// Iterator of type_or_consts field
pub fn iter(
&self,
) -> impl DoubleEndedIterator<Item = (Idx<TypeOrConstParamData>, &TypeOrConstParamData)> {
self.type_or_consts.iter()
}
pub(crate) fn generic_params_query(
db: &dyn DefDatabase,
def: GenericDefId,
) -> Interned<GenericParams> {
let _p = profile::span("generic_params_query");
let krate = def.module(db).krate;
let cfg_options = db.crate_graph();
let cfg_options = &cfg_options[krate].cfg_options;
// Returns the generic parameters that are enabled under the current `#[cfg]` options
let enabled_params = |params: &Interned<GenericParams>, item_tree: &ItemTree| {
let enabled = |param| item_tree.attrs(db, krate, param).is_cfg_enabled(cfg_options);
// In the common case, no parameters will by disabled by `#[cfg]` attributes.
// Therefore, make a first pass to check if all parameters are enabled and, if so,
// clone the `Interned<GenericParams>` instead of recreating an identical copy.
let all_type_or_consts_enabled =
params.type_or_consts.iter().all(|(idx, _)| enabled(idx.into()));
let all_lifetimes_enabled = params.lifetimes.iter().all(|(idx, _)| enabled(idx.into()));
if all_type_or_consts_enabled && all_lifetimes_enabled {
params.clone()
} else {
Interned::new(GenericParams {
type_or_consts: all_type_or_consts_enabled
.then(|| params.type_or_consts.clone())
.unwrap_or_else(|| {
params
.type_or_consts
.iter()
.filter_map(|(idx, param)| {
enabled(idx.into()).then(|| param.clone())
})
.collect()
}),
lifetimes: all_lifetimes_enabled
.then(|| params.lifetimes.clone())
.unwrap_or_else(|| {
params
.lifetimes
.iter()
.filter_map(|(idx, param)| {
enabled(idx.into()).then(|| param.clone())
})
.collect()
}),
where_predicates: params.where_predicates.clone(),
})
}
};
macro_rules! id_to_generics {
($id:ident) => {{
let id = $id.lookup(db).id;
let tree = id.item_tree(db);
let item = &tree[id.value];
enabled_params(&item.generic_params, &tree)
}};
}
match def {
GenericDefId::FunctionId(id) => {
let loc = id.lookup(db);
let tree = loc.id.item_tree(db);
let item = &tree[loc.id.value];
let enabled_params = enabled_params(&item.explicit_generic_params, &tree);
let module = loc.container.module(db);
let func_data = db.function_data(id);
if func_data.params.is_empty() {
enabled_params
} else {
let mut generic_params = GenericParamsCollector {
type_or_consts: enabled_params.type_or_consts.clone(),
lifetimes: enabled_params.lifetimes.clone(),
where_predicates: enabled_params.where_predicates.clone().into(),
};
// Don't create an `Expander` if not needed since this
// could cause a reparse after the `ItemTree` has been created due to the spanmap.
let mut expander = Lazy::new(|| {
(module.def_map(db), Expander::new(db, loc.id.file_id(), module))
});
for param in func_data.params.iter() {
generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
}
Interned::new(generic_params.finish())
}
}
GenericDefId::AdtId(AdtId::StructId(id)) => id_to_generics!(id),
GenericDefId::AdtId(AdtId::EnumId(id)) => id_to_generics!(id),
GenericDefId::AdtId(AdtId::UnionId(id)) => id_to_generics!(id),
GenericDefId::TraitId(id) => id_to_generics!(id),
GenericDefId::TraitAliasId(id) => id_to_generics!(id),
GenericDefId::TypeAliasId(id) => id_to_generics!(id),
GenericDefId::ImplId(id) => id_to_generics!(id),
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
Interned::new(GenericParams {
type_or_consts: Default::default(),
lifetimes: Default::default(),
where_predicates: Default::default(),
})
}
}
} }
pub fn find_type_by_name(&self, name: &Name, parent: GenericDefId) -> Option<TypeParamId> { pub fn find_type_by_name(&self, name: &Name, parent: GenericDefId) -> Option<TypeParamId> {

View File

@ -116,8 +116,7 @@ pub enum TypeRef {
Path(Path), Path(Path),
RawPtr(Box<TypeRef>, Mutability), RawPtr(Box<TypeRef>, Mutability),
Reference(Box<TypeRef>, Option<LifetimeRef>, Mutability), Reference(Box<TypeRef>, Option<LifetimeRef>, Mutability),
// FIXME: for full const generics, the latter element (length) here is going to have to be an // FIXME: This should be Array(Box<TypeRef>, Ast<ConstArg>),
// expression that is further lowered later in hir_ty.
Array(Box<TypeRef>, ConstRef), Array(Box<TypeRef>, ConstRef),
Slice(Box<TypeRef>), Slice(Box<TypeRef>),
/// A fn pointer. Last element of the vector is the return type. /// A fn pointer. Last element of the vector is the return type.

View File

@ -6,7 +6,7 @@
use syntax::ast::{self, HasModuleItem, HasTypeBounds}; use syntax::ast::{self, HasModuleItem, HasTypeBounds};
use crate::{ use crate::{
generics::{GenericParams, TypeParamData, TypeParamProvenance}, generics::{GenericParams, GenericParamsCollector, TypeParamData, TypeParamProvenance},
type_ref::{LifetimeRef, TraitBoundModifier, TraitRef}, type_ref::{LifetimeRef, TraitBoundModifier, TraitRef},
LocalLifetimeParamId, LocalTypeOrConstParamId, LocalLifetimeParamId, LocalTypeOrConstParamId,
}; };
@ -386,17 +386,16 @@ fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>>
flags |= FnFlags::HAS_UNSAFE_KW; flags |= FnFlags::HAS_UNSAFE_KW;
} }
let mut res = Function { let res = Function {
name, name,
visibility, visibility,
explicit_generic_params: Interned::new(GenericParams::default()), explicit_generic_params: self.lower_generic_params(HasImplicitSelf::No, func),
abi, abi,
params, params,
ret_type: Interned::new(ret_type), ret_type: Interned::new(ret_type),
ast_id, ast_id,
flags, flags,
}; };
res.explicit_generic_params = self.lower_generic_params(HasImplicitSelf::No, func);
Some(id(self.data().functions.alloc(res))) Some(id(self.data().functions.alloc(res)))
} }
@ -604,7 +603,7 @@ fn lower_generic_params(
has_implicit_self: HasImplicitSelf, has_implicit_self: HasImplicitSelf,
node: &dyn ast::HasGenericParams, node: &dyn ast::HasGenericParams,
) -> Interned<GenericParams> { ) -> Interned<GenericParams> {
let mut generics = GenericParams::default(); let mut generics = GenericParamsCollector::default();
if let HasImplicitSelf::Yes(bounds) = has_implicit_self { if let HasImplicitSelf::Yes(bounds) = has_implicit_self {
// Traits and trait aliases get the Self type as an implicit first type parameter. // Traits and trait aliases get the Self type as an implicit first type parameter.
@ -642,8 +641,7 @@ fn lower_generic_params(
}; };
generics.fill(&self.body_ctx, node, add_param_attrs); generics.fill(&self.body_ctx, node, add_param_attrs);
generics.shrink_to_fit(); Interned::new(generics.finish())
Interned::new(generics)
} }
fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Box<[Interned<TypeBound>]> { fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Box<[Interned<TypeBound>]> {

View File

@ -87,7 +87,10 @@ pub fn target(&self, item: LangItem) -> Option<LangItemTarget> {
} }
/// Salsa query. This will look for lang items in a specific crate. /// Salsa query. This will look for lang items in a specific crate.
pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<LangItems> { pub(crate) fn crate_lang_items_query(
db: &dyn DefDatabase,
krate: CrateId,
) -> Option<Arc<LangItems>> {
let _p = profile::span("crate_lang_items_query"); let _p = profile::span("crate_lang_items_query");
let mut lang_items = LangItems::default(); let mut lang_items = LangItems::default();
@ -150,7 +153,11 @@ pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Ar
} }
} }
Arc::new(lang_items) if lang_items.items.is_empty() {
None
} else {
Some(Arc::new(lang_items))
}
} }
/// Salsa query. Look for a lang item, starting from the specified crate and recursively /// Salsa query. Look for a lang item, starting from the specified crate and recursively
@ -161,9 +168,9 @@ pub(crate) fn lang_item_query(
item: LangItem, item: LangItem,
) -> Option<LangItemTarget> { ) -> Option<LangItemTarget> {
let _p = profile::span("lang_item_query"); let _p = profile::span("lang_item_query");
let lang_items = db.crate_lang_items(start_crate); if let Some(target) =
let start_crate_target = lang_items.items.get(&item); db.crate_lang_items(start_crate).and_then(|it| it.items.get(&item).copied())
if let Some(&target) = start_crate_target { {
return Some(target); return Some(target);
} }
db.crate_graph()[start_crate] db.crate_graph()[start_crate]

View File

@ -167,7 +167,7 @@ fn binder_kind(
} }
}); });
}) })
.map(|block_id| self.db.trait_impls_in_block(block_id)); .filter_map(|block_id| self.db.trait_impls_in_block(block_id));
let id_to_chalk = |id: hir_def::ImplId| id.to_chalk(self.db); let id_to_chalk = |id: hir_def::ImplId| id.to_chalk(self.db);
let mut result = vec![]; let mut result = vec![];
@ -183,7 +183,8 @@ fn binder_kind(
def_blocks def_blocks
.into_iter() .into_iter()
.flatten() .flatten()
.for_each(|it| f(&self.db.trait_impls_in_block(it))); .filter_map(|it| self.db.trait_impls_in_block(it))
.for_each(|it| f(&it));
} }
fps => { fps => {
let mut f = let mut f =
@ -198,7 +199,8 @@ fn binder_kind(
def_blocks def_blocks
.into_iter() .into_iter()
.flatten() .flatten()
.for_each(|it| f(&self.db.trait_impls_in_block(it))); .filter_map(|it| self.db.trait_impls_in_block(it))
.for_each(|it| f(&it));
} }
} }

View File

@ -34,6 +34,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
#[salsa::invoke(crate::infer::infer_query)] #[salsa::invoke(crate::infer::infer_query)]
fn infer_query(&self, def: DefWithBodyId) -> Arc<InferenceResult>; fn infer_query(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
// region:mir
#[salsa::invoke(crate::mir::mir_body_query)] #[salsa::invoke(crate::mir::mir_body_query)]
#[salsa::cycle(crate::mir::mir_body_recover)] #[salsa::cycle(crate::mir::mir_body_recover)]
fn mir_body(&self, def: DefWithBodyId) -> Result<Arc<MirBody>, MirLowerError>; fn mir_body(&self, def: DefWithBodyId) -> Result<Arc<MirBody>, MirLowerError>;
@ -61,20 +63,6 @@ fn monomorphized_mir_body_for_closure(
#[salsa::invoke(crate::mir::borrowck_query)] #[salsa::invoke(crate::mir::borrowck_query)]
fn borrowck(&self, def: DefWithBodyId) -> Result<Arc<[BorrowckResult]>, MirLowerError>; fn borrowck(&self, def: DefWithBodyId) -> Result<Arc<[BorrowckResult]>, MirLowerError>;
#[salsa::invoke(crate::lower::ty_query)]
#[salsa::cycle(crate::lower::ty_recover)]
fn ty(&self, def: TyDefId) -> Binders<Ty>;
#[salsa::invoke(crate::lower::value_ty_query)]
fn value_ty(&self, def: ValueTyDefId) -> Binders<Ty>;
#[salsa::invoke(crate::lower::impl_self_ty_query)]
#[salsa::cycle(crate::lower::impl_self_ty_recover)]
fn impl_self_ty(&self, def: ImplId) -> Binders<Ty>;
#[salsa::invoke(crate::lower::const_param_ty_query)]
fn const_param_ty(&self, def: ConstParamId) -> Ty;
#[salsa::invoke(crate::consteval::const_eval_query)] #[salsa::invoke(crate::consteval::const_eval_query)]
#[salsa::cycle(crate::consteval::const_eval_recover)] #[salsa::cycle(crate::consteval::const_eval_recover)]
fn const_eval( fn const_eval(
@ -92,6 +80,22 @@ fn const_eval(
#[salsa::cycle(crate::consteval::const_eval_discriminant_recover)] #[salsa::cycle(crate::consteval::const_eval_discriminant_recover)]
fn const_eval_discriminant(&self, def: EnumVariantId) -> Result<i128, ConstEvalError>; fn const_eval_discriminant(&self, def: EnumVariantId) -> Result<i128, ConstEvalError>;
// endregion:mir
#[salsa::invoke(crate::lower::ty_query)]
#[salsa::cycle(crate::lower::ty_recover)]
fn ty(&self, def: TyDefId) -> Binders<Ty>;
#[salsa::invoke(crate::lower::value_ty_query)]
fn value_ty(&self, def: ValueTyDefId) -> Binders<Ty>;
#[salsa::invoke(crate::lower::impl_self_ty_query)]
#[salsa::cycle(crate::lower::impl_self_ty_recover)]
fn impl_self_ty(&self, def: ImplId) -> Binders<Ty>;
#[salsa::invoke(crate::lower::const_param_ty_query)]
fn const_param_ty(&self, def: ConstParamId) -> Ty;
#[salsa::invoke(crate::lower::impl_trait_query)] #[salsa::invoke(crate::lower::impl_trait_query)]
fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>; fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>;
@ -158,7 +162,7 @@ fn generic_predicates_for_param(
fn inherent_impls_in_crate(&self, krate: CrateId) -> Arc<InherentImpls>; fn inherent_impls_in_crate(&self, krate: CrateId) -> Arc<InherentImpls>;
#[salsa::invoke(InherentImpls::inherent_impls_in_block_query)] #[salsa::invoke(InherentImpls::inherent_impls_in_block_query)]
fn inherent_impls_in_block(&self, block: BlockId) -> Arc<InherentImpls>; fn inherent_impls_in_block(&self, block: BlockId) -> Option<Arc<InherentImpls>>;
/// Collects all crates in the dependency graph that have impls for the /// Collects all crates in the dependency graph that have impls for the
/// given fingerprint. This is only used for primitive types and types /// given fingerprint. This is only used for primitive types and types
@ -175,7 +179,7 @@ fn incoherent_inherent_impl_crates(
fn trait_impls_in_crate(&self, krate: CrateId) -> Arc<TraitImpls>; fn trait_impls_in_crate(&self, krate: CrateId) -> Arc<TraitImpls>;
#[salsa::invoke(TraitImpls::trait_impls_in_block_query)] #[salsa::invoke(TraitImpls::trait_impls_in_block_query)]
fn trait_impls_in_block(&self, block: BlockId) -> Arc<TraitImpls>; fn trait_impls_in_block(&self, block: BlockId) -> Option<Arc<TraitImpls>>;
#[salsa::invoke(TraitImpls::trait_impls_in_deps_query)] #[salsa::invoke(TraitImpls::trait_impls_in_deps_query)]
fn trait_impls_in_deps(&self, krate: CrateId) -> Arc<[Arc<TraitImpls>]>; fn trait_impls_in_deps(&self, krate: CrateId) -> Arc<[Arc<TraitImpls>]>;

View File

@ -1601,7 +1601,7 @@ fn implicitly_sized_clauses<'a>(
pub(crate) fn generic_defaults_query( pub(crate) fn generic_defaults_query(
db: &dyn HirDatabase, db: &dyn HirDatabase,
def: GenericDefId, def: GenericDefId,
) -> Arc<[Binders<chalk_ir::GenericArg<Interner>>]> { ) -> Arc<[Binders<crate::GenericArg>]> {
let resolver = def.resolver(db.upcast()); let resolver = def.resolver(db.upcast());
let ctx = TyLoweringContext::new(db, &resolver, def.into()) let ctx = TyLoweringContext::new(db, &resolver, def.into())
.with_type_param_mode(ParamLoweringMode::Variable); .with_type_param_mode(ParamLoweringMode::Variable);

View File

@ -132,34 +132,40 @@ pub fn for_trait_impl(ty: &Ty) -> Option<TyFingerprint> {
TyFingerprint::Scalar(Scalar::Float(FloatTy::F64)), TyFingerprint::Scalar(Scalar::Float(FloatTy::F64)),
]; ];
type TraitFpMap = FxHashMap<TraitId, FxHashMap<Option<TyFingerprint>, Box<[ImplId]>>>;
type TraitFpMapCollector = FxHashMap<TraitId, FxHashMap<Option<TyFingerprint>, Vec<ImplId>>>;
/// Trait impls defined or available in some crate. /// Trait impls defined or available in some crate.
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct TraitImpls { pub struct TraitImpls {
// If the `Option<TyFingerprint>` is `None`, the impl may apply to any self type. // If the `Option<TyFingerprint>` is `None`, the impl may apply to any self type.
map: FxHashMap<TraitId, FxHashMap<Option<TyFingerprint>, Vec<ImplId>>>, map: TraitFpMap,
} }
impl TraitImpls { impl TraitImpls {
pub(crate) fn trait_impls_in_crate_query(db: &dyn HirDatabase, krate: CrateId) -> Arc<Self> { pub(crate) fn trait_impls_in_crate_query(db: &dyn HirDatabase, krate: CrateId) -> Arc<Self> {
let _p = profile::span("trait_impls_in_crate_query").detail(|| format!("{krate:?}")); let _p = profile::span("trait_impls_in_crate_query").detail(|| format!("{krate:?}"));
let mut impls = Self { map: FxHashMap::default() }; let mut impls = FxHashMap::default();
let crate_def_map = db.crate_def_map(krate); Self::collect_def_map(db, &mut impls, &db.crate_def_map(krate));
impls.collect_def_map(db, &crate_def_map);
impls.shrink_to_fit();
Arc::new(impls) Arc::new(Self::finish(impls))
} }
pub(crate) fn trait_impls_in_block_query(db: &dyn HirDatabase, block: BlockId) -> Arc<Self> { pub(crate) fn trait_impls_in_block_query(
db: &dyn HirDatabase,
block: BlockId,
) -> Option<Arc<Self>> {
let _p = profile::span("trait_impls_in_block_query"); let _p = profile::span("trait_impls_in_block_query");
let mut impls = Self { map: FxHashMap::default() }; let mut impls = FxHashMap::default();
let block_def_map = db.block_def_map(block); Self::collect_def_map(db, &mut impls, &db.block_def_map(block));
impls.collect_def_map(db, &block_def_map);
impls.shrink_to_fit();
Arc::new(impls) if impls.is_empty() {
None
} else {
Some(Arc::new(Self::finish(impls)))
}
} }
pub(crate) fn trait_impls_in_deps_query( pub(crate) fn trait_impls_in_deps_query(
@ -174,15 +180,16 @@ pub(crate) fn trait_impls_in_deps_query(
) )
} }
fn shrink_to_fit(&mut self) { fn finish(map: TraitFpMapCollector) -> TraitImpls {
self.map.shrink_to_fit(); TraitImpls {
self.map.values_mut().for_each(|map| { map: map
map.shrink_to_fit(); .into_iter()
map.values_mut().for_each(Vec::shrink_to_fit); .map(|(k, v)| (k, v.into_iter().map(|(k, v)| (k, v.into_boxed_slice())).collect()))
}); .collect(),
}
} }
fn collect_def_map(&mut self, db: &dyn HirDatabase, def_map: &DefMap) { fn collect_def_map(db: &dyn HirDatabase, map: &mut TraitFpMapCollector, def_map: &DefMap) {
for (_module_id, module_data) in def_map.modules() { for (_module_id, module_data) in def_map.modules() {
for impl_id in module_data.scope.impls() { for impl_id in module_data.scope.impls() {
// Reservation impls should be ignored during trait resolution, so we never need // Reservation impls should be ignored during trait resolution, so we never need
@ -200,12 +207,7 @@ fn collect_def_map(&mut self, db: &dyn HirDatabase, def_map: &DefMap) {
}; };
let self_ty = db.impl_self_ty(impl_id); let self_ty = db.impl_self_ty(impl_id);
let self_ty_fp = TyFingerprint::for_trait_impl(self_ty.skip_binders()); let self_ty_fp = TyFingerprint::for_trait_impl(self_ty.skip_binders());
self.map map.entry(target_trait).or_default().entry(self_ty_fp).or_default().push(impl_id);
.entry(target_trait)
.or_default()
.entry(self_ty_fp)
.or_default()
.push(impl_id);
} }
// To better support custom derives, collect impls in all unnamed const items. // To better support custom derives, collect impls in all unnamed const items.
@ -213,7 +215,7 @@ fn collect_def_map(&mut self, db: &dyn HirDatabase, def_map: &DefMap) {
for konst in collect_unnamed_consts(db, &module_data.scope) { for konst in collect_unnamed_consts(db, &module_data.scope) {
let body = db.body(konst.into()); let body = db.body(konst.into());
for (_, block_def_map) in body.blocks(db.upcast()) { for (_, block_def_map) in body.blocks(db.upcast()) {
self.collect_def_map(db, &block_def_map); Self::collect_def_map(db, map, &block_def_map);
} }
} }
} }
@ -281,7 +283,10 @@ pub(crate) fn inherent_impls_in_crate_query(db: &dyn HirDatabase, krate: CrateId
Arc::new(impls) Arc::new(impls)
} }
pub(crate) fn inherent_impls_in_block_query(db: &dyn HirDatabase, block: BlockId) -> Arc<Self> { pub(crate) fn inherent_impls_in_block_query(
db: &dyn HirDatabase,
block: BlockId,
) -> Option<Arc<Self>> {
let _p = profile::span("inherent_impls_in_block_query"); let _p = profile::span("inherent_impls_in_block_query");
let mut impls = Self { map: FxHashMap::default(), invalid_impls: Vec::default() }; let mut impls = Self { map: FxHashMap::default(), invalid_impls: Vec::default() };
@ -289,7 +294,11 @@ pub(crate) fn inherent_impls_in_block_query(db: &dyn HirDatabase, block: BlockId
impls.collect_def_map(db, &block_def_map); impls.collect_def_map(db, &block_def_map);
impls.shrink_to_fit(); impls.shrink_to_fit();
Arc::new(impls) if impls.map.is_empty() && impls.invalid_impls.is_empty() {
None
} else {
Some(Arc::new(impls))
}
} }
fn shrink_to_fit(&mut self) { fn shrink_to_fit(&mut self) {
@ -737,7 +746,7 @@ fn lookup_impl_assoc_item_for_trait_ref(
let impls = db.trait_impls_in_deps(env.krate); let impls = db.trait_impls_in_deps(env.krate);
let self_impls = match self_ty.kind(Interner) { let self_impls = match self_ty.kind(Interner) {
TyKind::Adt(id, _) => { TyKind::Adt(id, _) => {
id.0.module(db.upcast()).containing_block().map(|it| db.trait_impls_in_block(it)) id.0.module(db.upcast()).containing_block().and_then(|it| db.trait_impls_in_block(it))
} }
_ => None, _ => None,
}; };
@ -1254,7 +1263,7 @@ fn iterate_inherent_methods(
}; };
while let Some(block_id) = block { while let Some(block_id) = block {
let impls = db.inherent_impls_in_block(block_id); if let Some(impls) = db.inherent_impls_in_block(block_id) {
impls_for_self_ty( impls_for_self_ty(
&impls, &impls,
self_ty, self_ty,
@ -1265,6 +1274,7 @@ fn iterate_inherent_methods(
module, module,
callback, callback,
)?; )?;
}
block = db.block_def_map(block_id).parent().and_then(|module| module.containing_block()); block = db.block_def_map(block_id).parent().and_then(|module| module.containing_block());
} }

View File

@ -35,7 +35,7 @@ macro_rules! impl_has_attrs {
impl HasAttrs for $def { impl HasAttrs for $def {
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner { fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
let def = AttrDefId::$def_id(self.into()); let def = AttrDefId::$def_id(self.into());
db.attrs_with_owner(def) AttrsWithOwner::attrs_with_owner(db.upcast(), def)
} }
fn attr_id(self) -> AttrDefId { fn attr_id(self) -> AttrDefId {
AttrDefId::$def_id(self.into()) AttrDefId::$def_id(self.into())

View File

@ -15,8 +15,8 @@
InternExternBlockQuery, InternExternCrateQuery, InternFunctionQuery, InternImplQuery, InternExternBlockQuery, InternExternCrateQuery, InternFunctionQuery, InternImplQuery,
InternInTypeConstQuery, InternMacro2Query, InternMacroRulesQuery, InternProcMacroQuery, InternInTypeConstQuery, InternMacro2Query, InternMacroRulesQuery, InternProcMacroQuery,
InternStaticQuery, InternStructQuery, InternTraitAliasQuery, InternTraitQuery, InternStaticQuery, InternStructQuery, InternTraitAliasQuery, InternTraitQuery,
InternTypeAliasQuery, InternUnionQuery, InternUseQuery, LangAttrQuery, LangItemQuery, InternTypeAliasQuery, InternUnionQuery, InternUseQuery, LangItemQuery, Macro2DataQuery,
Macro2DataQuery, MacroRulesDataQuery, ProcMacroDataQuery, StaticDataQuery, StructDataQuery, MacroRulesDataQuery, ProcMacroDataQuery, StaticDataQuery, StructDataQuery,
StructDataWithDiagnosticsQuery, TraitAliasDataQuery, TraitDataQuery, StructDataWithDiagnosticsQuery, TraitAliasDataQuery, TraitDataQuery,
TraitDataWithDiagnosticsQuery, TypeAliasDataQuery, UnionDataQuery, TraitDataWithDiagnosticsQuery, TypeAliasDataQuery, UnionDataQuery,
UnionDataWithDiagnosticsQuery, VariantsAttrsQuery, VariantsAttrsSourceMapQuery, UnionDataWithDiagnosticsQuery, VariantsAttrsQuery, VariantsAttrsSourceMapQuery,

View File

@ -84,73 +84,11 @@ macro_rules! purge_each_query {
)*} )*}
} }
purge_each_query![ purge_each_query![
// SourceDatabase // SymbolsDatabase
base_db::ParseQuery crate::symbol_index::ModuleSymbolsQuery
base_db::CrateGraphQuery crate::symbol_index::LibrarySymbolsQuery
crate::symbol_index::LocalRootsQuery
// SourceDatabaseExt crate::symbol_index::LibraryRootsQuery
base_db::FileTextQuery
base_db::FileSourceRootQuery
base_db::SourceRootQuery
base_db::SourceRootCratesQuery
// ExpandDatabase
hir::db::AstIdMapQuery
hir::db::DeclMacroExpanderQuery
hir::db::ExpandProcMacroQuery
hir::db::InternMacroCallQuery
hir::db::InternSyntaxContextQuery
hir::db::MacroArgQuery
hir::db::ParseMacroExpansionQuery
hir::db::RealSpanMapQuery
hir::db::ProcMacrosQuery
// DefDatabase
hir::db::FileItemTreeQuery
hir::db::CrateDefMapQueryQuery
hir::db::BlockDefMapQuery
hir::db::StructDataQuery
hir::db::StructDataWithDiagnosticsQuery
hir::db::UnionDataQuery
hir::db::UnionDataWithDiagnosticsQuery
hir::db::EnumDataQuery
hir::db::EnumDataWithDiagnosticsQuery
hir::db::ImplDataQuery
hir::db::ImplDataWithDiagnosticsQuery
hir::db::TraitDataQuery
hir::db::TraitDataWithDiagnosticsQuery
hir::db::TraitAliasDataQuery
hir::db::TypeAliasDataQuery
hir::db::FunctionDataQuery
hir::db::ConstDataQuery
hir::db::StaticDataQuery
hir::db::Macro2DataQuery
hir::db::MacroRulesDataQuery
hir::db::ProcMacroDataQuery
hir::db::BodyWithSourceMapQuery
hir::db::BodyQuery
hir::db::ExprScopesQuery
hir::db::GenericParamsQuery
hir::db::VariantsAttrsQuery
hir::db::FieldsAttrsQuery
hir::db::VariantsAttrsSourceMapQuery
hir::db::FieldsAttrsSourceMapQuery
hir::db::AttrsQuery
hir::db::CrateLangItemsQuery
hir::db::LangItemQuery
hir::db::ImportMapQuery
hir::db::FieldVisibilitiesQuery
hir::db::FunctionVisibilityQuery
hir::db::ConstVisibilityQuery
hir::db::CrateSupportsNoStdQuery
hir::db::BlockItemTreeQueryQuery
hir::db::ExternCrateDeclDataQuery
hir::db::LangAttrQuery
hir::db::InternAnonymousConstQuery
hir::db::InternExternCrateQuery
hir::db::InternInTypeConstQuery
hir::db::InternUseQuery
// HirDatabase // HirDatabase
hir::db::InferQueryQuery hir::db::InferQueryQuery
hir::db::MirBodyQuery hir::db::MirBodyQuery
@ -194,14 +132,50 @@ macro_rules! purge_each_query {
hir::db::TraitSolveQueryQuery hir::db::TraitSolveQueryQuery
hir::db::ProgramClausesForChalkEnvQuery hir::db::ProgramClausesForChalkEnvQuery
// SymbolsDatabase // DefDatabase
crate::symbol_index::ModuleSymbolsQuery hir::db::FileItemTreeQuery
crate::symbol_index::LibrarySymbolsQuery hir::db::CrateDefMapQueryQuery
crate::symbol_index::LocalRootsQuery hir::db::BlockDefMapQuery
crate::symbol_index::LibraryRootsQuery hir::db::StructDataQuery
hir::db::StructDataWithDiagnosticsQuery
// LineIndexDatabase hir::db::UnionDataQuery
crate::LineIndexQuery hir::db::UnionDataWithDiagnosticsQuery
hir::db::EnumDataQuery
hir::db::EnumDataWithDiagnosticsQuery
hir::db::ImplDataQuery
hir::db::ImplDataWithDiagnosticsQuery
hir::db::TraitDataQuery
hir::db::TraitDataWithDiagnosticsQuery
hir::db::TraitAliasDataQuery
hir::db::TypeAliasDataQuery
hir::db::FunctionDataQuery
hir::db::ConstDataQuery
hir::db::StaticDataQuery
hir::db::Macro2DataQuery
hir::db::MacroRulesDataQuery
hir::db::ProcMacroDataQuery
hir::db::BodyWithSourceMapQuery
hir::db::BodyQuery
hir::db::ExprScopesQuery
hir::db::GenericParamsQuery
hir::db::VariantsAttrsQuery
hir::db::FieldsAttrsQuery
hir::db::VariantsAttrsSourceMapQuery
hir::db::FieldsAttrsSourceMapQuery
hir::db::AttrsQuery
hir::db::CrateLangItemsQuery
hir::db::LangItemQuery
hir::db::ImportMapQuery
hir::db::FieldVisibilitiesQuery
hir::db::FunctionVisibilityQuery
hir::db::ConstVisibilityQuery
hir::db::CrateSupportsNoStdQuery
hir::db::BlockItemTreeQueryQuery
hir::db::ExternCrateDeclDataQuery
hir::db::InternAnonymousConstQuery
hir::db::InternExternCrateQuery
hir::db::InternInTypeConstQuery
hir::db::InternUseQuery
// InternDatabase // InternDatabase
hir::db::InternFunctionQuery hir::db::InternFunctionQuery
@ -219,6 +193,30 @@ macro_rules! purge_each_query {
hir::db::InternMacro2Query hir::db::InternMacro2Query
hir::db::InternProcMacroQuery hir::db::InternProcMacroQuery
hir::db::InternMacroRulesQuery hir::db::InternMacroRulesQuery
// ExpandDatabase
hir::db::AstIdMapQuery
hir::db::DeclMacroExpanderQuery
hir::db::ExpandProcMacroQuery
hir::db::InternMacroCallQuery
hir::db::InternSyntaxContextQuery
hir::db::MacroArgQuery
hir::db::ParseMacroExpansionQuery
hir::db::RealSpanMapQuery
hir::db::ProcMacrosQuery
// LineIndexDatabase
crate::LineIndexQuery
// SourceDatabase
base_db::ParseQuery
base_db::CrateGraphQuery
// SourceDatabaseExt
base_db::FileTextQuery
base_db::FileSourceRootQuery
base_db::SourceRootQuery
base_db::SourceRootCratesQuery
]; ];
acc.sort_by_key(|it| std::cmp::Reverse(it.1)); acc.sort_by_key(|it| std::cmp::Reverse(it.1));

View File

@ -145,7 +145,7 @@ pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
db.set_local_roots_with_durability(Default::default(), Durability::HIGH); db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
db.set_library_roots_with_durability(Default::default(), Durability::HIGH); db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
db.set_expand_proc_attr_macros_with_durability(false, Durability::HIGH); db.set_expand_proc_attr_macros_with_durability(false, Durability::HIGH);
db.update_parse_query_lru_capacity(lru_capacity); db.update_base_query_lru_capacities(lru_capacity);
db.setup_syntax_context_root(); db.setup_syntax_context_root();
db db
} }
@ -154,11 +154,12 @@ pub fn enable_proc_attr_macros(&mut self) {
self.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH); self.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
} }
pub fn update_parse_query_lru_capacity(&mut self, lru_capacity: Option<usize>) { pub fn update_base_query_lru_capacities(&mut self, lru_capacity: Option<usize>) {
let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_PARSE_LRU_CAP); let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_PARSE_LRU_CAP);
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity); base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
// macro expansions are usually rather small, so we can afford to keep more of them alive // macro expansions are usually rather small, so we can afford to keep more of them alive
hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(4 * lru_capacity); hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(4 * lru_capacity);
hir::db::BorrowckQuery.in_db_mut(self).set_lru_capacity(base_db::DEFAULT_BORROWCK_LRU_CAP);
} }
pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, usize>) { pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, usize>) {
@ -176,6 +177,12 @@ pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, usi
.copied() .copied()
.unwrap_or(4 * base_db::DEFAULT_PARSE_LRU_CAP), .unwrap_or(4 * base_db::DEFAULT_PARSE_LRU_CAP),
); );
hir_db::BorrowckQuery.in_db_mut(self).set_lru_capacity(
lru_capacities
.get(stringify!(BorrowckQuery))
.copied()
.unwrap_or(base_db::DEFAULT_BORROWCK_LRU_CAP),
);
macro_rules! update_lru_capacity_per_query { macro_rules! update_lru_capacity_per_query {
($( $module:ident :: $query:ident )*) => {$( ($( $module:ident :: $query:ident )*) => {$(

View File

@ -171,7 +171,7 @@ pub fn new(lru_capacity: Option<usize>) -> AnalysisHost {
} }
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) { pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
self.db.update_parse_query_lru_capacity(lru_capacity); self.db.update_base_query_lru_capacities(lru_capacity);
} }
pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, usize>) { pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, usize>) {