From a9dd08cbf0cde2bb9c6ad6c4a9ed99abca1fc6d5 Mon Sep 17 00:00:00 2001 From: lcnr Date: Wed, 25 Sep 2024 14:58:42 +0200 Subject: [PATCH] SsoHashMap --- .../src/infer/relate/type_relating.rs | 4 ++-- compiler/rustc_infer/src/infer/resolve.rs | 23 ++++++++----------- compiler/rustc_middle/src/ty/fold.rs | 5 ++-- .../rustc_next_trait_solver/src/resolve.rs | 4 ++-- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_infer/src/infer/relate/type_relating.rs b/compiler/rustc_infer/src/infer/relate/type_relating.rs index 6167beccd8a5..d4ad9c383db3 100644 --- a/compiler/rustc_infer/src/infer/relate/type_relating.rs +++ b/compiler/rustc_infer/src/infer/relate/type_relating.rs @@ -1,4 +1,4 @@ -use rustc_data_structures::fx::FxHashSet; +use rustc_data_structures::sso::SsoHashSet; use rustc_middle::traits::solve::Goal; use rustc_middle::ty::relate::{ Relate, RelateResult, TypeRelation, relate_args_invariantly, relate_args_with_variances, @@ -17,7 +17,7 @@ pub struct TypeRelating<'combine, 'a, 'tcx> { fields: &'combine mut CombineFields<'a, 'tcx>, structurally_relate_aliases: StructurallyRelateAliases, ambient_variance: ty::Variance, - cache: FxHashSet<(ty::Variance, Ty<'tcx>, Ty<'tcx>)>, + cache: SsoHashSet<(ty::Variance, Ty<'tcx>, Ty<'tcx>)>, } impl<'combine, 'infcx, 'tcx> TypeRelating<'combine, 'infcx, 'tcx> { diff --git a/compiler/rustc_infer/src/infer/resolve.rs b/compiler/rustc_infer/src/infer/resolve.rs index 1a162c4dbcf9..b80406d197fa 100644 --- a/compiler/rustc_infer/src/infer/resolve.rs +++ b/compiler/rustc_infer/src/infer/resolve.rs @@ -1,4 +1,4 @@ -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::sso::SsoHashMap; use rustc_middle::bug; use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::visit::TypeVisitableExt; @@ -16,7 +16,7 @@ use super::{FixupError, FixupResult, InferCtxt}; /// points for correctness. pub struct OpportunisticVarResolver<'a, 'tcx> { infcx: &'a InferCtxt<'tcx>, - cache: FxHashMap, Ty<'tcx>>, + cache: SsoHashMap, Ty<'tcx>>, } impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> { @@ -33,19 +33,16 @@ impl<'a, 'tcx> TypeFolder> for OpportunisticVarResolver<'a, 'tcx> { #[inline] fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { - if let Some(&ty) = self.cache.get(&t) { - return ty; - } - - let res = if !t.has_non_region_infer() { + if !t.has_non_region_infer() { t // micro-optimize -- if there is nothing in this type that this fold affects... + } else if let Some(&ty) = self.cache.get(&t) { + return ty; } else { - let t = self.infcx.shallow_resolve(t); - t.super_fold_with(self) - }; - - assert!(self.cache.insert(t, res).is_none()); - res + let shallow = self.infcx.shallow_resolve(t); + let res = shallow.super_fold_with(self); + assert!(self.cache.insert(t, res).is_none()); + res + } } fn fold_const(&mut self, ct: Const<'tcx>) -> Const<'tcx> { diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index c98e5c26cd71..6884366e8fb6 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -1,4 +1,5 @@ -use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; +use rustc_data_structures::fx::FxIndexMap; +use rustc_data_structures::sso::SsoHashMap; use rustc_hir::def_id::DefId; pub use rustc_type_ir::fold::{ FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable, shift_region, shift_vars, @@ -165,7 +166,7 @@ struct BoundVarReplacer<'tcx, D> { delegate: D, - cache: FxHashMap<(ty::DebruijnIndex, Ty<'tcx>), Ty<'tcx>>, + cache: SsoHashMap<(ty::DebruijnIndex, Ty<'tcx>), Ty<'tcx>>, } impl<'tcx, D: BoundVarReplacerDelegate<'tcx>> BoundVarReplacer<'tcx, D> { diff --git a/compiler/rustc_next_trait_solver/src/resolve.rs b/compiler/rustc_next_trait_solver/src/resolve.rs index 3b97028dbabf..f0fd79f6e481 100644 --- a/compiler/rustc_next_trait_solver/src/resolve.rs +++ b/compiler/rustc_next_trait_solver/src/resolve.rs @@ -1,4 +1,4 @@ -use rustc_type_ir::data_structures::HashMap; +use rustc_data_structures::sso::SsoHashMap; use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; use rustc_type_ir::inherent::*; use rustc_type_ir::visit::TypeVisitableExt; @@ -16,7 +16,7 @@ where I: Interner, { delegate: &'a D, - cache: HashMap, + cache: SsoHashMap, } impl<'a, D: SolverDelegate> EagerResolver<'a, D> {