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:
@@ -21,6 +21,7 @@ use rustc_hir::def_id::{CrateNum, LOCAL_CRATE, LocalDefId, StableCrateId};
|
|||||||
use rustc_hir::definitions::Definitions;
|
use rustc_hir::definitions::Definitions;
|
||||||
use rustc_index::IndexVec;
|
use rustc_index::IndexVec;
|
||||||
use rustc_middle::bug;
|
use rustc_middle::bug;
|
||||||
|
use rustc_middle::ty::data_structures::IndexSet;
|
||||||
use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
|
use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
|
||||||
use rustc_proc_macro::bridge::client::ProcMacro;
|
use rustc_proc_macro::bridge::client::ProcMacro;
|
||||||
use rustc_session::config::{
|
use rustc_session::config::{
|
||||||
@@ -281,7 +282,7 @@ impl CStore {
|
|||||||
.filter_map(|(cnum, data)| data.as_deref_mut().map(|data| (cnum, data)))
|
.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) {
|
if !deps.contains(&cnum) {
|
||||||
let data = self.get_crate_data(cnum);
|
let data = self.get_crate_data(cnum);
|
||||||
for dep in data.dependencies() {
|
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> {
|
pub(crate) fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> IndexSet<CrateNum> {
|
||||||
let mut deps = Vec::new();
|
let mut deps = IndexSet::default();
|
||||||
if cnum == LOCAL_CRATE {
|
if cnum == LOCAL_CRATE {
|
||||||
for (cnum, _) in self.iter_crate_data() {
|
for (cnum, _) in self.iter_crate_data() {
|
||||||
self.push_dependencies_in_postorder(&mut deps, cnum);
|
self.push_dependencies_in_postorder(&mut deps, cnum);
|
||||||
@@ -306,10 +307,11 @@ impl CStore {
|
|||||||
deps
|
deps
|
||||||
}
|
}
|
||||||
|
|
||||||
fn crate_dependencies_in_reverse_postorder(&self, cnum: CrateNum) -> Vec<CrateNum> {
|
fn crate_dependencies_in_reverse_postorder(
|
||||||
let mut deps = self.crate_dependencies_in_postorder(cnum);
|
&self,
|
||||||
deps.reverse();
|
cnum: CrateNum,
|
||||||
deps
|
) -> impl Iterator<Item = CrateNum> {
|
||||||
|
self.crate_dependencies_in_postorder(cnum).into_iter().rev()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn injected_panic_runtime(&self) -> Option<CrateNum> {
|
pub(crate) fn injected_panic_runtime(&self) -> Option<CrateNum> {
|
||||||
|
|||||||
@@ -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_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(),
|
has_alloc_error_handler: |tcx, LocalCrate| CStore::from_tcx(tcx).has_alloc_error_handler(),
|
||||||
postorder_cnums: |tcx, ()| {
|
postorder_cnums: |tcx, ()| {
|
||||||
tcx.arena
|
tcx.arena.alloc_from_iter(
|
||||||
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
|
CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE).into_iter(),
|
||||||
|
)
|
||||||
},
|
},
|
||||||
crates: |tcx, ()| {
|
crates: |tcx, ()| {
|
||||||
// The list of loaded crates is now frozen in query cache,
|
// The list of loaded crates is now frozen in query cache,
|
||||||
|
|||||||
Reference in New Issue
Block a user