Skip to content

Commit 93b52c8

Browse files
An optimization attempt
1 parent 485c6f4 commit 93b52c8

File tree

7 files changed

+27
-17
lines changed

7 files changed

+27
-17
lines changed

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_data_structures::fx::FxIndexSet;
12
use rustc_hir::def_id::DefId;
23
use rustc_infer::infer::canonical::QueryRegionConstraints;
34
use rustc_infer::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
@@ -36,7 +37,7 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> {
3637
/// our special inference variable there, we would mess that up.
3738
implicit_region_bound: ty::Region<'tcx>,
3839
param_env: ty::ParamEnv<'tcx>,
39-
known_type_outlives: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
40+
known_type_outlives: &'a FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
4041
locations: Locations,
4142
span: Span,
4243
category: ConstraintCategory<'tcx>,
@@ -50,7 +51,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
5051
universal_regions: &'a UniversalRegions<'tcx>,
5152
implicit_region_bound: ty::Region<'tcx>,
5253
param_env: ty::ParamEnv<'tcx>,
53-
known_type_outlives: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
54+
known_type_outlives: &'a FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
5455
locations: Locations,
5556
span: Span,
5657
category: ConstraintCategory<'tcx>,

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_data_structures::frozen::Frozen;
2+
use rustc_data_structures::fx::FxIndexSet;
23
use rustc_data_structures::transitive_relation::{TransitiveRelation, TransitiveRelationBuilder};
34
use rustc_hir::def::DefKind;
45
use rustc_infer::infer::canonical::QueryRegionConstraints;
@@ -42,7 +43,7 @@ type NormalizedInputsAndOutput<'tcx> = Vec<Ty<'tcx>>;
4243

4344
pub(crate) struct CreateResult<'tcx> {
4445
pub(crate) universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
45-
pub(crate) known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
46+
pub(crate) known_type_outlives: FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
4647
pub(crate) normalized_inputs_and_output: NormalizedInputsAndOutput<'tcx>,
4748
}
4849

@@ -59,7 +60,7 @@ pub(crate) fn create<'tcx>(
5960
implicit_region_bound,
6061
constraints,
6162
universal_regions,
62-
known_type_outlives: vec![],
63+
known_type_outlives: FxIndexSet::default(),
6364
outlives: Default::default(),
6465
inverse_outlives: Default::default(),
6566
}
@@ -186,7 +187,7 @@ struct UniversalRegionRelationsBuilder<'a, 'tcx> {
186187

187188
// outputs:
188189
outlives: TransitiveRelationBuilder<RegionVid>,
189-
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
190+
known_type_outlives: FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
190191
inverse_outlives: TransitiveRelationBuilder<RegionVid>,
191192
}
192193

@@ -370,7 +371,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
370371
}
371372
}
372373

373-
self.known_type_outlives.push(outlives);
374+
self.known_type_outlives.insert(outlives);
374375
}
375376

376377
/// Update the type of a single local, which should represent
@@ -416,14 +417,14 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
416417
}
417418

418419
OutlivesBound::RegionSubParam(r_a, param_b) => {
419-
self.known_type_outlives.push(ty::Binder::dummy(ty::OutlivesPredicate(
420+
self.known_type_outlives.insert(ty::Binder::dummy(ty::OutlivesPredicate(
420421
Ty::new_param(self.infcx.tcx, param_b.index, param_b.name),
421422
r_a,
422423
)));
423424
}
424425

425426
OutlivesBound::RegionSubAlias(r_a, alias_b) => {
426-
self.known_type_outlives.push(ty::Binder::dummy(ty::OutlivesPredicate(
427+
self.known_type_outlives.insert(ty::Binder::dummy(ty::OutlivesPredicate(
427428
Ty::new_alias(self.infcx.tcx, alias_b.kind(self.infcx.tcx), alias_b),
428429
r_a,
429430
)));

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ struct TypeChecker<'a, 'tcx> {
552552
/// User type annotations are shared between the main MIR and the MIR of
553553
/// all of the promoted items.
554554
user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>,
555-
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
555+
known_type_outlives: FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
556556
implicit_region_bound: ty::Region<'tcx>,
557557
reported_errors: FxIndexSet<(Ty<'tcx>, Span)>,
558558
universal_regions: &'a UniversalRegions<'tcx>,

compiler/rustc_infer/src/infer/outlives/env.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_data_structures::fx::FxIndexSet;
12
use rustc_data_structures::transitive_relation::TransitiveRelationBuilder;
23
use rustc_middle::{bug, ty};
34

@@ -25,7 +26,7 @@ use crate::infer::free_regions::FreeRegionMap;
2526
pub struct OutlivesEnvironment<'tcx> {
2627
pub param_env: ty::ParamEnv<'tcx>,
2728
free_region_map: FreeRegionMap<'tcx>,
28-
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
29+
known_type_outlives: FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
2930
}
3031

3132
impl<'tcx> OutlivesEnvironment<'tcx> {
@@ -36,7 +37,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
3637
Item = ty::Binder<'tcx, ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>>,
3738
>,
3839
) -> Self {
39-
let mut known_type_outlives = vec![];
40+
let mut known_type_outlives = FxIndexSet::default();
4041
let mut region_relation = TransitiveRelationBuilder::default();
4142

4243
// Record relationships such as `T:'x` that don't go into the
@@ -67,7 +68,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
6768
}
6869
}
6970
ty::GenericArgKind::Type(ty_b) => {
70-
known_type_outlives.push(bound.rebind(ty::OutlivesPredicate(ty_b, r_a)));
71+
known_type_outlives.insert(bound.rebind(ty::OutlivesPredicate(ty_b, r_a)));
7172
}
7273
ty::GenericArgKind::Const(_) => unreachable!(),
7374
}
@@ -86,7 +87,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
8687
}
8788

8889
/// Borrows current `known_type_outlives`.
89-
pub fn known_type_outlives(&self) -> &[ty::PolyTypeOutlivesPredicate<'tcx>] {
90+
pub fn known_type_outlives(&self) -> &FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>> {
9091
&self.known_type_outlives
9192
}
9293
}

compiler/rustc_infer/src/infer/outlives/obligations.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
//! might later infer `?U` to something like `&'b u32`, which would
6060
//! imply that `'b: 'a`.
6161
62+
use rustc_data_structures::fx::FxIndexSet;
6263
use rustc_data_structures::undo_log::UndoLogs;
6364
use rustc_middle::bug;
6465
use rustc_middle::mir::ConstraintCategory;
@@ -226,7 +227,7 @@ where
226227
delegate: D,
227228
tcx: TyCtxt<'tcx>,
228229
implicit_region_bound: Option<ty::Region<'tcx>>,
229-
known_type_outlives: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
230+
known_type_outlives: &'cx FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
230231
) -> Self {
231232
Self {
232233
delegate,

compiler/rustc_infer/src/infer/outlives/test_type_match.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::hash_map::Entry;
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_middle::ty::error::TypeError;
55
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
6+
use rustc_type_ir::fast_reject::{self, TreatParams};
67
use tracing::instrument;
78

89
use crate::infer::region_constraints::VerifyIfEq;
@@ -81,8 +82,12 @@ pub(super) fn can_match_erased_ty<'tcx>(
8182
if outlives_ty == erased_ty {
8283
// pointless micro-optimization
8384
true
84-
} else {
85+
} else if fast_reject::simplify_type(tcx, erased_ty, TreatParams::AsRigid)
86+
== fast_reject::simplify_type(tcx, outlives_ty, TreatParams::AsRigid)
87+
{
8588
MatchAgainstHigherRankedOutlives::new(tcx).relate(outlives_ty, erased_ty).is_ok()
89+
} else {
90+
false
8691
}
8792
}
8893

compiler/rustc_infer/src/infer/outlives/verify.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::assert_matches::assert_matches;
22

3+
use rustc_data_structures::fx::FxIndexSet;
34
use rustc_middle::ty::{self, OutlivesPredicate, Ty, TyCtxt};
45
use rustc_type_ir::outlives::{Component, compute_alias_components_recursive};
56
use smallvec::smallvec;
@@ -22,14 +23,14 @@ pub(crate) struct VerifyBoundCx<'cx, 'tcx> {
2223
/// Outside of borrowck the only way to prove `T: '?0` is by
2324
/// setting `'?0` to `'empty`.
2425
implicit_region_bound: Option<ty::Region<'tcx>>,
25-
known_type_outlives: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
26+
known_type_outlives: &'cx FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
2627
}
2728

2829
impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
2930
pub(crate) fn new(
3031
tcx: TyCtxt<'tcx>,
3132
implicit_region_bound: Option<ty::Region<'tcx>>,
32-
known_type_outlives: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
33+
known_type_outlives: &'cx FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
3334
) -> Self {
3435
Self { tcx, implicit_region_bound, known_type_outlives }
3536
}

0 commit comments

Comments
 (0)