Skip to content

Commit 474c413

Browse files
committed
implement paramenv compacting
1 parent a792fe3 commit 474c413

6 files changed

Lines changed: 341 additions & 46 deletions

File tree

‎compiler/rustc_middle/src/ty/mod.rs‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,62 @@ impl<'tcx> rustc_type_ir::Flags for Clauses<'tcx> {
10081008
}
10091009
}
10101010

1011+
/// Manages compression of clauses to form a `ParamEnv`.
1012+
struct ClauseCompressor<'tcx> {
1013+
tcx: TyCtxt<'tcx>,
1014+
compressed_clauses: Vec<CompressedTraitClause<TyCtxt<'tcx>>>,
1015+
uncompressed_clauses: Vec<Clause<'tcx>>,
1016+
}
1017+
1018+
impl<'tcx> ClauseCompressor<'tcx> {
1019+
fn new(tcx: TyCtxt<'tcx>) -> Self {
1020+
Self { tcx, compressed_clauses: Vec::new(), uncompressed_clauses: Vec::new() }
1021+
}
1022+
1023+
fn add_clause(&mut self, clause: Clause<'tcx>) {
1024+
// Try to construct a common clause, if the given clause is one of the common clauses
1025+
let Some(new_compressed_trait_clause) =
1026+
CompressedTraitClause::from_clause(self.tcx, clause)
1027+
else {
1028+
self.uncompressed_clauses.push(clause);
1029+
return;
1030+
};
1031+
1032+
// See if this new common clause can be merged into another already existing
1033+
// common clause so they can be interned together.
1034+
//
1035+
// We're *not* using a hashmap here since this list is not expected to grow
1036+
// larger than a couple of elements. Just iterating is faster.
1037+
if !self
1038+
.compressed_clauses
1039+
.iter_mut()
1040+
// finishes iteration upon first successful `try_merge`.
1041+
.any(|existing| existing.try_merge(new_compressed_trait_clause))
1042+
{
1043+
self.compressed_clauses.push(new_compressed_trait_clause)
1044+
}
1045+
}
1046+
1047+
fn mk_clauses(self) -> Clauses<'tcx> {
1048+
let mut res =
1049+
Vec::with_capacity(self.uncompressed_clauses.len() + self.compressed_clauses.len());
1050+
1051+
res.extend(self.compressed_clauses.into_iter().map(|compressed| -> Clause<'tcx> {
1052+
assert_ne!(compressed.compressed_trait_refs.bits(), 0);
1053+
// NOTE: in theory, if there's only one bit set, we could opt not to compress,
1054+
// since then we also don't need to decompress. The flip side is, that that
1055+
// it's often cheaper to ask "does ty impl trait" for a compressed clause than for an
1056+
// arbitrary other clause. Especially if we know in advance that the trait for which
1057+
// this question is asked is only ever compressed, and we don't have to look through
1058+
// other clauses.
1059+
ClauseKind::CompressedTrait(compressed).upcast(self.tcx)
1060+
}));
1061+
res.extend(self.uncompressed_clauses);
1062+
1063+
self.tcx.mk_clauses(&res)
1064+
}
1065+
}
1066+
10111067
/// When interacting with the type system we must provide information about the
10121068
/// environment. `ParamEnv` is the type that represents this information. See the
10131069
/// [dev guide chapter][param_env_guide] for more information.
@@ -1068,7 +1124,12 @@ impl<'tcx> ParamEnv<'tcx> {
10681124
tcx: TyCtxt<'tcx>,
10691125
caller_bounds: impl IntoIterator<Item = ty::Clause<'tcx>>,
10701126
) -> Self {
1071-
ParamEnv { caller_bounds: tcx.mk_clauses_from_iter(caller_bounds.into_iter()) }
1127+
let mut compressor = ClauseCompressor::new(tcx);
1128+
for clause in caller_bounds {
1129+
compressor.add_clause(clause);
1130+
}
1131+
1132+
ParamEnv { caller_bounds: compressor.mk_clauses() }
10721133
}
10731134

10741135
/// Creates a pair of param-env and value for use in queries.

‎compiler/rustc_trait_selection/src/traits/select/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
924924
Err(_) => Ok(EvaluatedToErr),
925925
}
926926
}
927-
ty::PredicateKind::Clause(ty::ClauseKind::CompressedTrait(..)) => unreachable!(),
927+
ty::PredicateKind::Clause(ty::ClauseKind::CompressedTrait(_)) => unreachable!(),
928928
}
929929
}
930930

‎compiler/rustc_type_ir/src/flags.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,12 @@ impl<I: Interner> FlagComputation<I> {
468468
ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(_sym)) => {}
469469
ty::PredicateKind::Ambiguous => {}
470470

471-
ty::PredicateKind::Clause(ty::ClauseKind::CompressedTrait(_)) => unreachable!(),
471+
ty::PredicateKind::Clause(ty::ClauseKind::CompressedTrait(c)) => {
472+
// NOTE: this is correct as long as compressed trait clauses represnt trait clauses
473+
// that don't have generic parameters, in which case only computing flags of the self_ty
474+
// is correct.
475+
self.add_args(&[c.self_ty.into()]);
476+
}
472477
}
473478
}
474479

‎compiler/rustc_type_ir/src/fold.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ pub trait TypeFolder<I: Interner>: Sized {
161161

162162
/// [Fold predicate](TypeFolder::fold_predicate) deliberately doesn't get access
163163
/// to an actual predicate. This way, we can compress lists of predicates, and hide
164-
/// this detail to folders. Instead, some type implementing this trait, [`PredicateProxy`]
165-
/// is passed, with its limited API.
164+
/// this detail to folders. See [`crate::predicate::CompressedTraitClause`].
165+
/// Instead, some type implementing this trait, [`PredicateProxy`] is passed, with its limited API.
166166
///
167167
/// Most [`TypeFolder`]s only use `fold_predicate` to inspect type flags.
168168
pub trait PredicateProxy<I: Interner>:

0 commit comments

Comments
 (0)