shared-generics: Do not share instantiations that cannot be created outside of the current crate

In Zed shared-generics loading takes up a significant chunk of time in incremental build, as rustc deserializes rmeta of all dependencies of a crate.
I've recently realized that shared-generics includes all instantiations of some_generic_function in the following snippet:
```rs
pub fn some_generic_function(_: impl Fn()) {}

pub fn non_generic_function() {
	some_generic_function(|| {});
	some_generic_function(|| {});
	some_generic_function(|| {});
	some_generic_function(|| {});
	some_generic_function(|| {});
	some_generic_function(|| {});
	some_generic_function(|| {});
}
```
even though none of these instantiations can actually be created from outside of `non_generic_function`.

This PR makes shared-generics account for visibilities of generic arguments; an item is only considered for exporting if it is reachable from the outside or if all of it's arguments are visible outside of the local crate.

This PR reduces incremental build time for Zed (touch edito.rs scenario) from 12.4s to 10.4s.

Co-authored-by: bjorn3 <17426603+bjorn3@users.noreply.github.com>
This commit is contained in:
Piotr Osiewicz 2025-03-14 22:49:08 +01:00
parent 10fa3c449f
commit b15584b1a3

View file

@ -164,10 +164,10 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b
tcx.reachable_non_generics(def_id.krate).contains_key(&def_id)
}
fn exported_symbols_provider_local(
tcx: TyCtxt<'_>,
fn exported_symbols_provider_local<'tcx>(
tcx: TyCtxt<'tcx>,
_: LocalCrate,
) -> &[(ExportedSymbol<'_>, SymbolExportInfo)] {
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] {
if !tcx.sess.opts.output_types.should_codegen() {
return &[];
}
@ -321,6 +321,38 @@ fn exported_symbols_provider_local(
let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
// Do not export symbols that cannot be instantiated by downstream crates.
let reachable_set = tcx.reachable_set(());
let is_local_to_current_crate = |ty: Ty<'_>| {
let no_refs = ty.peel_refs();
let root_def_id = match no_refs.kind() {
ty::Closure(closure, _) => *closure,
ty::FnDef(def_id, _) => *def_id,
ty::Coroutine(def_id, _) => *def_id,
ty::CoroutineClosure(def_id, _) => *def_id,
ty::CoroutineWitness(def_id, _) => *def_id,
_ => return false,
};
let Some(root_def_id) = root_def_id.as_local() else {
return false;
};
let is_local = !reachable_set.contains(&root_def_id);
is_local
};
let is_instantiable_downstream =
|did: Option<DefId>, generic_args: GenericArgsRef<'tcx>| {
generic_args
.types()
.chain(did.into_iter().map(move |did| tcx.type_of(did).skip_binder()))
.all(move |arg| {
arg.walk().all(|ty| {
ty.as_type().map_or(true, |ty| !is_local_to_current_crate(ty))
})
})
};
// The symbols created in this loop are sorted below it
#[allow(rustc::potential_query_instability)]
for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
@ -349,7 +381,12 @@ fn exported_symbols_provider_local(
match *mono_item {
MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
if args.non_erasable_generics().next().is_some() {
let has_generics = args.non_erasable_generics().next().is_some();
let should_export =
has_generics && is_instantiable_downstream(Some(def), &args);
if should_export {
let symbol = ExportedSymbol::Generic(def, args);
symbols.push((
symbol,
@ -364,14 +401,24 @@ fn exported_symbols_provider_local(
MonoItem::Fn(Instance { def: InstanceKind::DropGlue(_, Some(ty)), args }) => {
// A little sanity-check
assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
symbols.push((
ExportedSymbol::DropGlue(ty),
SymbolExportInfo {
level: SymbolExportLevel::Rust,
kind: SymbolExportKind::Text,
used: false,
},
));
// Drop glue did is always going to be non-local outside of libcore, thus we don't need to check it's locality (which includes invoking `type_of` query).
let should_export = match ty.kind() {
ty::Adt(_, args) => is_instantiable_downstream(None, args),
ty::Closure(_, args) => is_instantiable_downstream(None, args),
_ => true,
};
if should_export {
symbols.push((
ExportedSymbol::DropGlue(ty),
SymbolExportInfo {
level: SymbolExportLevel::Rust,
kind: SymbolExportKind::Text,
used: false,
},
));
}
}
MonoItem::Fn(Instance {
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),