Skip to content

Commit 27302a8

Browse files
committed
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.
1 parent a2aba05 commit 27302a8

File tree

1 file changed

+75
-11
lines changed

1 file changed

+75
-11
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+75-11
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,30 @@ fn exported_symbols_provider_local(
320320

321321
let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
322322

323+
let visibilities = tcx.effective_visibilities(());
324+
let is_local_to_current_crate = |ty: Ty<'_>| {
325+
let no_refs = ty.peel_refs();
326+
#[allow(rustc::usage_of_ty_tykind)]
327+
let root_def_id = match no_refs.kind() {
328+
rustc_type_ir::TyKind::Adt(adt_def, _) => adt_def.did(),
329+
rustc_type_ir::TyKind::Closure(closure, _) => *closure,
330+
rustc_type_ir::TyKind::FnDef(def_id, _) => *def_id,
331+
rustc_type_ir::TyKind::Coroutine(def_id, _) => *def_id,
332+
rustc_type_ir::TyKind::CoroutineClosure(def_id, _) => *def_id,
333+
rustc_type_ir::TyKind::CoroutineWitness(def_id, _) => *def_id,
334+
rustc_type_ir::TyKind::Foreign(def_id) => *def_id,
335+
_ => {
336+
return false;
337+
}
338+
};
339+
let Some(root_def_id) = root_def_id.as_local() else {
340+
return false;
341+
};
342+
343+
let is_local = visibilities.public_at_level(root_def_id).is_none();
344+
345+
is_local
346+
};
323347
// The symbols created in this loop are sorted below it
324348
#[allow(rustc::potential_query_instability)]
325349
for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
@@ -348,7 +372,21 @@ fn exported_symbols_provider_local(
348372

349373
match *mono_item {
350374
MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
351-
if args.non_erasable_generics().next().is_some() {
375+
let mut types = args.types();
376+
let has_generics = args.non_erasable_generics().next().is_some();
377+
let should_export = has_generics
378+
&& (def.as_local().is_some_and(|local_did| {
379+
visibilities.public_at_level(local_did).is_some()
380+
}) || types.all(|arg| {
381+
arg.walk().all(|ty| {
382+
let Some(ty) = ty.as_type() else {
383+
return true;
384+
};
385+
!is_local_to_current_crate(ty)
386+
})
387+
}));
388+
389+
if should_export {
352390
let symbol = ExportedSymbol::Generic(def, args);
353391
symbols.push((
354392
symbol,
@@ -361,16 +399,42 @@ fn exported_symbols_provider_local(
361399
}
362400
}
363401
MonoItem::Fn(Instance { def: InstanceKind::DropGlue(_, Some(ty)), args }) => {
364-
// A little sanity-check
365-
assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
366-
symbols.push((
367-
ExportedSymbol::DropGlue(ty),
368-
SymbolExportInfo {
369-
level: SymbolExportLevel::Rust,
370-
kind: SymbolExportKind::Text,
371-
used: false,
372-
},
373-
));
402+
let Some(GenericArgKind::Type(typ)) = args.non_erasable_generics().next()
403+
else {
404+
bug!("Expected a type argument for drop glue");
405+
};
406+
#[allow(rustc::usage_of_ty_tykind)]
407+
let should_export = {
408+
let root_identifier = match typ.kind() {
409+
rustc_type_ir::TyKind::Adt(def, args) => Some((def.did(), args)),
410+
rustc_type_ir::TyKind::Closure(id, args) => Some((*id, args)),
411+
_ => None,
412+
};
413+
if let Some((did, args)) = root_identifier {
414+
did.as_local().is_some_and(|local_did| {
415+
visibilities.public_at_level(local_did).is_some()
416+
}) || args.types().all(|arg| {
417+
arg.walk().all(|ty| {
418+
let Some(ty) = ty.as_type() else {
419+
return true;
420+
};
421+
!is_local_to_current_crate(ty)
422+
})
423+
})
424+
} else {
425+
true
426+
}
427+
};
428+
if should_export {
429+
symbols.push((
430+
ExportedSymbol::DropGlue(ty),
431+
SymbolExportInfo {
432+
level: SymbolExportLevel::Rust,
433+
kind: SymbolExportKind::Text,
434+
used: false,
435+
},
436+
));
437+
}
374438
}
375439
MonoItem::Fn(Instance {
376440
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),

0 commit comments

Comments
 (0)