From 422208ae52564c3b8cbf8416ffd1c92e9c80a3c0 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Tue, 21 Feb 2023 15:18:10 +0100 Subject: [PATCH] Use UnordSet instead of FxHashSet for names_imported_by_glob_use query. --- compiler/rustc_data_structures/src/unord.rs | 33 ++++++++++++++++++- compiler/rustc_middle/src/query/mod.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 5 ++- compiler/rustc_middle/src/ty/query.rs | 2 +- compiler/rustc_passes/src/dead.rs | 2 +- .../clippy_lints/src/wildcard_imports.rs | 10 ++---- 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_data_structures/src/unord.rs b/compiler/rustc_data_structures/src/unord.rs index f35f18e51cb..5c2435a0122 100644 --- a/compiler/rustc_data_structures/src/unord.rs +++ b/compiler/rustc_data_structures/src/unord.rs @@ -109,6 +109,12 @@ pub fn flat_map(self, f: F) -> UnordItems> } } +impl UnordItems> { + pub fn empty() -> Self { + UnordItems(std::iter::empty()) + } +} + impl<'a, T: Clone + 'a, I: Iterator> UnordItems<&'a T, I> { #[inline] pub fn cloned(self) -> UnordItems> { @@ -133,6 +139,20 @@ pub fn into_sorted(self, hcx: &HCX) -> Vec items } + #[inline] + pub fn into_sorted_stable_ord(self, use_stable_sort: bool) -> Vec + where + T: Ord + StableOrd, + { + let mut items: Vec = self.0.collect(); + if use_stable_sort { + items.sort(); + } else { + items.sort_unstable() + } + items + } + pub fn into_sorted_small_vec(self, hcx: &HCX) -> SmallVec<[T; LEN]> where T: ToStableHashKey, @@ -175,6 +195,11 @@ pub fn len(&self) -> usize { self.inner.len() } + #[inline] + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } + #[inline] pub fn insert(&mut self, v: V) -> bool { self.inner.insert(v) @@ -253,7 +278,7 @@ pub fn into_sorted(self, hcx: &HCX, cache_sort_key: bool) -> Vec // We can safely extend this UnordSet from a set of unordered values because that // won't expose the internal ordering anywhere. #[inline] - pub fn extend>(&mut self, items: UnordItems) { + pub fn extend_unord>(&mut self, items: UnordItems) { self.inner.extend(items.0) } @@ -277,6 +302,12 @@ fn from_iter>(iter: T) -> Self { } } +impl From> for UnordSet { + fn from(value: FxHashSet) -> Self { + UnordSet { inner: value } + } +} + impl> HashStable for UnordSet { #[inline] fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index be5aa49a0d0..9f25d0b2e43 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1826,7 +1826,7 @@ query maybe_unused_trait_imports(_: ()) -> &'tcx FxIndexSet { desc { "fetching potentially unused trait imports" } } - query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx FxHashSet { + query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx UnordSet { desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id.to_def_id()) } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 0333198c203..4c3525d8a11 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -37,6 +37,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::steal::Steal; use rustc_data_structures::sync::{self, Lock, Lrc, MappedReadGuard, ReadGuard, WorkerLocal}; +use rustc_data_structures::unord::UnordSet; use rustc_errors::{ DecorateLint, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, MultiSpan, }; @@ -2488,7 +2489,9 @@ pub fn provide(providers: &mut ty::query::Providers) { providers.maybe_unused_trait_imports = |tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports; providers.names_imported_by_glob_use = |tcx, id| { - tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default()) + tcx.arena.alloc(UnordSet::from( + tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default(), + )) }; providers.extern_mod_stmt_cnum = diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 10c37b74483..3c185912776 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -41,7 +41,7 @@ use rustc_ast as ast; use rustc_ast::expand::allocator::AllocatorKind; use rustc_attr as attr; -use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; +use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet}; use rustc_data_structures::steal::Steal; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::Lrc; diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 8a9c4cc8f7f..75273d1fb7d 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -371,7 +371,7 @@ fn visit_variant_data(&mut self, def: &'tcx hir::VariantData<'tcx>) { } if tcx.visibility(def_id).is_public() { Some(def_id) } else { None } }); - Extend::extend(&mut self.live_symbols, live_fields); + self.live_symbols.extend(live_fields); intravisit::walk_struct_def(self, def); } diff --git a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs index e4d1ee195c4..e105452e1c5 100644 --- a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs +++ b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs @@ -155,14 +155,10 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { ) }; - let imports_string = if used_imports.len() == 1 { - used_imports.iter().next().unwrap().to_string() + let mut imports = used_imports.items().map(ToString::to_string).into_sorted_stable_ord(false); + let imports_string = if imports.len() == 1 { + imports.pop().unwrap() } else { - let mut imports = used_imports - .iter() - .map(ToString::to_string) - .collect::>(); - imports.sort(); if braced_glob { imports.join(", ") } else {