Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bc5e048

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 78f2104 commit bc5e048

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

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,29 @@ fn exported_symbols_provider_local(
321321

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

324+
let visibilities = tcx.effective_visibilities(());
325+
let is_local_to_current_crate = |ty: Ty<'_>| {
326+
let no_refs = ty.peel_refs();
327+
let root_def_id = match no_refs.kind() {
328+
rustc_type_ir::Adt(adt_def, _) => adt_def.did(),
329+
rustc_type_ir::Closure(closure, _) => *closure,
330+
rustc_type_ir::FnDef(def_id, _) => *def_id,
331+
rustc_type_ir::Coroutine(def_id, _) => *def_id,
332+
rustc_type_ir::CoroutineClosure(def_id, _) => *def_id,
333+
rustc_type_ir::CoroutineWitness(def_id, _) => *def_id,
334+
rustc_type_ir::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+
};
324347
// The symbols created in this loop are sorted below it
325348
#[allow(rustc::potential_query_instability)]
326349
for (mono_item, data) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
@@ -349,7 +372,21 @@ fn exported_symbols_provider_local(
349372

350373
match *mono_item {
351374
MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
352-
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 {
353390
let symbol = ExportedSymbol::Generic(def, args);
354391
symbols.push((
355392
symbol,
@@ -362,16 +399,41 @@ fn exported_symbols_provider_local(
362399
}
363400
}
364401
MonoItem::Fn(Instance { def: InstanceKind::DropGlue(_, Some(ty)), args }) => {
365-
// A little sanity-check
366-
assert_eq!(args.non_erasable_generics().next(), Some(GenericArgKind::Type(ty)));
367-
symbols.push((
368-
ExportedSymbol::DropGlue(ty),
369-
SymbolExportInfo {
370-
level: SymbolExportLevel::Rust,
371-
kind: SymbolExportKind::Text,
372-
used: false,
373-
},
374-
));
402+
let Some(GenericArgKind::Type(typ)) = args.non_erasable_generics().next()
403+
else {
404+
bug!("Expected a type argument for drop glue");
405+
};
406+
let should_export = {
407+
let root_identifier = match typ.kind() {
408+
rustc_type_ir::Adt(def, args) => Some((def.did(), args)),
409+
rustc_type_ir::Closure(id, args) => Some((*id, args)),
410+
_ => None,
411+
};
412+
if let Some((did, args)) = root_identifier {
413+
did.as_local().is_some_and(|local_did| {
414+
visibilities.public_at_level(local_did).is_some()
415+
}) || args.types().all(|arg| {
416+
arg.walk().all(|ty| {
417+
let Some(ty) = ty.as_type() else {
418+
return true;
419+
};
420+
!is_local_to_current_crate(ty)
421+
})
422+
})
423+
} else {
424+
true
425+
}
426+
};
427+
if should_export {
428+
symbols.push((
429+
ExportedSymbol::DropGlue(ty),
430+
SymbolExportInfo {
431+
level: SymbolExportLevel::Rust,
432+
kind: SymbolExportKind::Text,
433+
used: false,
434+
},
435+
));
436+
}
375437
}
376438
MonoItem::Fn(Instance {
377439
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),

0 commit comments

Comments
 (0)