Auto merge of #141730 - osiewicz:collect-crate-deps-postorder-use-indexset, r=nnethercote

cstore: Use IndexSet as backing store for postorder dependencies

`<rustc_metadata::creader::CStore>::push_dependencies_in_postorder` showed up in new benchmarks from https://github.com/rust-lang/rustc-perf/pull/2143, hence I gave it a shot to remove an obvious O(n) there.

r? nnethercote
This commit is contained in:
bors
2025-06-01 07:40:52 +00:00
2 changed files with 13 additions and 10 deletions

View File

@@ -21,6 +21,7 @@ use rustc_hir::def_id::{CrateNum, LOCAL_CRATE, LocalDefId, StableCrateId};
use rustc_hir::definitions::Definitions;
use rustc_index::IndexVec;
use rustc_middle::bug;
use rustc_middle::ty::data_structures::IndexSet;
use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
use rustc_proc_macro::bridge::client::ProcMacro;
use rustc_session::config::{
@@ -281,7 +282,7 @@ impl CStore {
.filter_map(|(cnum, data)| data.as_deref_mut().map(|data| (cnum, data)))
}
fn push_dependencies_in_postorder(&self, deps: &mut Vec<CrateNum>, cnum: CrateNum) {
fn push_dependencies_in_postorder(&self, deps: &mut IndexSet<CrateNum>, cnum: CrateNum) {
if !deps.contains(&cnum) {
let data = self.get_crate_data(cnum);
for dep in data.dependencies() {
@@ -290,12 +291,12 @@ impl CStore {
}
}
deps.push(cnum);
deps.insert(cnum);
}
}
pub(crate) fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> Vec<CrateNum> {
let mut deps = Vec::new();
pub(crate) fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> IndexSet<CrateNum> {
let mut deps = IndexSet::default();
if cnum == LOCAL_CRATE {
for (cnum, _) in self.iter_crate_data() {
self.push_dependencies_in_postorder(&mut deps, cnum);
@@ -306,10 +307,11 @@ impl CStore {
deps
}
fn crate_dependencies_in_reverse_postorder(&self, cnum: CrateNum) -> Vec<CrateNum> {
let mut deps = self.crate_dependencies_in_postorder(cnum);
deps.reverse();
deps
fn crate_dependencies_in_reverse_postorder(
&self,
cnum: CrateNum,
) -> impl Iterator<Item = CrateNum> {
self.crate_dependencies_in_postorder(cnum).into_iter().rev()
}
pub(crate) fn injected_panic_runtime(&self) -> Option<CrateNum> {

View File

@@ -549,8 +549,9 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
has_global_allocator: |tcx, LocalCrate| CStore::from_tcx(tcx).has_global_allocator(),
has_alloc_error_handler: |tcx, LocalCrate| CStore::from_tcx(tcx).has_alloc_error_handler(),
postorder_cnums: |tcx, ()| {
tcx.arena
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
tcx.arena.alloc_from_iter(
CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE).into_iter(),
)
},
crates: |tcx, ()| {
// The list of loaded crates is now frozen in query cache,