Skip to content

Commit 5dd1877

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 5dd1877

File tree

1 file changed

+73
-11
lines changed

1 file changed

+73
-11
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+73-11
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,29 @@ 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+
let root_def_id = match no_refs.kind() {
327+
rustc_type_ir::Adt(adt_def, _) => adt_def.did(),
328+
rustc_type_ir::Closure(closure, _) => *closure,
329+
rustc_type_ir::FnDef(def_id, _) => *def_id,
330+
rustc_type_ir::Coroutine(def_id, _) => *def_id,
331+
rustc_type_ir::CoroutineClosure(def_id, _) => *def_id,
332+
rustc_type_ir::CoroutineWitness(def_id, _) => *def_id,
333+
rustc_type_ir::Foreign(def_id) => *def_id,
334+
_ => {
335+
return false;
336+
}
337+
};
338+
let Some(root_def_id) = root_def_id.as_local() else {
339+
return false;
340+
};
341+
342+
let is_local = visibilities.public_at_level(root_def_id).is_none();
343+
344+
is_local
345+
};
323346
// The symbols created in this loop are sorted below it
324347
#[allow(rustc::potential_query_instability)]
325348
for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
@@ -348,7 +371,21 @@ fn exported_symbols_provider_local(
348371

349372
match *mono_item {
350373
MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
351-
if args.non_erasable_generics().next().is_some() {
374+
let mut types = args.types();
375+
let has_generics = args.non_erasable_generics().next().is_some();
376+
let should_export = has_generics
377+
&& (def.as_local().is_some_and(|local_did| {
378+
visibilities.public_at_level(local_did).is_some()
379+
}) || types.all(|arg| {
380+
arg.walk().all(|ty| {
381+
let Some(ty) = ty.as_type() else {
382+
return true;
383+
};
384+
!is_local_to_current_crate(ty)
385+
})
386+
}));
387+
388+
if should_export {
352389
let symbol = ExportedSymbol::Generic(def, args);
353390
symbols.push((
354391
symbol,
@@ -361,16 +398,41 @@ fn exported_symbols_provider_local(
361398
}
362399
}
363400
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-
));
401+
let Some(GenericArgKind::Type(typ)) = args.non_erasable_generics().next()
402+
else {
403+
bug!("Expected a type argument for drop glue");
404+
};
405+
let should_export = {
406+
let root_identifier = match typ.kind() {
407+
rustc_type_ir::Adt(def, args) => Some((def.did(), args)),
408+
rustc_type_ir::Closure(id, args) => Some((*id, args)),
409+
_ => None,
410+
};
411+
if let Some((did, args)) = root_identifier {
412+
did.as_local().is_some_and(|local_did| {
413+
visibilities.public_at_level(local_did).is_some()
414+
}) || args.types().all(|arg| {
415+
arg.walk().all(|ty| {
416+
let Some(ty) = ty.as_type() else {
417+
return true;
418+
};
419+
!is_local_to_current_crate(ty)
420+
})
421+
})
422+
} else {
423+
true
424+
}
425+
};
426+
if should_export {
427+
symbols.push((
428+
ExportedSymbol::DropGlue(ty),
429+
SymbolExportInfo {
430+
level: SymbolExportLevel::Rust,
431+
kind: SymbolExportKind::Text,
432+
used: false,
433+
},
434+
));
435+
}
374436
}
375437
MonoItem::Fn(Instance {
376438
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),

0 commit comments

Comments
 (0)