@@ -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.
0 commit comments