Skip to content

Commit 005fc0f

Browse files
committed
Auto merge of #106977 - michaelwoerister:unord_id_collections, r=oli-obk
Use UnordMap and UnordSet for id collections (DefIdMap, LocalDefIdMap, etc) This PR changes the `rustc_data_structures::define_id_collections!` macro to use `UnordMap` and `UnordSet` instead of `FxHashMap` and `FxHashSet`. This should account for a large portion of hash-maps being used in places where they can cause trouble. The changes required are moderate but non-zero: - In some places the collections are extracted into sorted vecs. - There are a few instances where for-loops have been changed to extends. ~~Let's see what the performance impact is. With a bit more refactoring, we might be able to get rid of some of the additional sorting -- but the change set is already big enough. Unless there's a performance impact, I'd like to do further changes in subsequent PRs.~~ Performance does not seem to be negatively affected ([perf-run here](#106977 (comment))). Part of [MCP 533](rust-lang/compiler-team#533). r? `@ghost`
2 parents 21f6839 + f219771 commit 005fc0f

File tree

18 files changed

+394
-119
lines changed

18 files changed

+394
-119
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression};
88
use rustc_codegen_ssa::traits::{ConstMethods, CoverageInfoMethods};
99
use rustc_data_structures::fx::FxIndexSet;
1010
use rustc_hir::def::DefKind;
11-
use rustc_hir::def_id::DefIdSet;
11+
use rustc_hir::def_id::DefId;
1212
use rustc_llvm::RustString;
1313
use rustc_middle::bug;
1414
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
@@ -291,7 +291,7 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
291291

292292
let ignore_unused_generics = tcx.sess.instrument_coverage_except_unused_generics();
293293

294-
let eligible_def_ids: DefIdSet = tcx
294+
let eligible_def_ids: Vec<DefId> = tcx
295295
.mir_keys(())
296296
.iter()
297297
.filter_map(|local_def_id| {
@@ -317,7 +317,9 @@ fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
317317

318318
let codegenned_def_ids = tcx.codegened_and_inlined_items(());
319319

320-
for &non_codegenned_def_id in eligible_def_ids.difference(codegenned_def_ids) {
320+
for non_codegenned_def_id in
321+
eligible_def_ids.into_iter().filter(|id| !codegenned_def_ids.contains(id))
322+
{
321323
let codegen_fn_attrs = tcx.codegen_fn_attrs(non_codegenned_def_id);
322324

323325
// If a function is marked `#[no_coverage]`, then skip generating a

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,15 @@ fn exported_symbols_provider_local(
173173
return &[];
174174
}
175175

176-
let mut symbols: Vec<_> = tcx
177-
.reachable_non_generics(LOCAL_CRATE)
178-
.iter()
179-
.map(|(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info))
180-
.collect();
176+
// FIXME: Sorting this is unnecessary since we are sorting later anyway.
177+
// Can we skip the later sorting?
178+
let mut symbols: Vec<_> = tcx.with_stable_hashing_context(|hcx| {
179+
tcx.reachable_non_generics(LOCAL_CRATE)
180+
.to_sorted(&hcx, true)
181+
.into_iter()
182+
.map(|(&def_id, &info)| (ExportedSymbol::NonGeneric(def_id), info))
183+
.collect()
184+
});
181185

182186
if tcx.entry_fn(()).is_some() {
183187
let exported_symbol =

compiler/rustc_codegen_ssa/src/base.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -964,16 +964,19 @@ pub fn provide(providers: &mut Providers) {
964964
};
965965

966966
let (defids, _) = tcx.collect_and_partition_mono_items(cratenum);
967-
for id in &*defids {
967+
968+
let any_for_speed = defids.items().any(|id| {
968969
let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
969970
match optimize {
970-
attr::OptimizeAttr::None => continue,
971-
attr::OptimizeAttr::Size => continue,
972-
attr::OptimizeAttr::Speed => {
973-
return for_speed;
974-
}
971+
attr::OptimizeAttr::None | attr::OptimizeAttr::Size => false,
972+
attr::OptimizeAttr::Speed => true,
975973
}
974+
});
975+
976+
if any_for_speed {
977+
return for_speed;
976978
}
979+
977980
tcx.sess.opts.optimize
978981
};
979982
}

compiler/rustc_data_structures/src/fx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub type IndexEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>;
1111
#[macro_export]
1212
macro_rules! define_id_collections {
1313
($map_name:ident, $set_name:ident, $entry_name:ident, $key:ty) => {
14-
pub type $map_name<T> = $crate::fx::FxHashMap<$key, T>;
15-
pub type $set_name = $crate::fx::FxHashSet<$key>;
14+
pub type $map_name<T> = $crate::unord::UnordMap<$key, T>;
15+
pub type $set_name = $crate::unord::UnordSet<$key>;
1616
pub type $entry_name<'a, T> = $crate::fx::StdEntry<'a, $key, T>;
1717
};
1818
}

0 commit comments

Comments
 (0)