Skip to content

Commit 4ca960a

Browse files
committed
add extend to paramenvs for faster additions
1 parent ef04d20 commit 4ca960a

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_attr_ir::lang_items::LangItem;
3434
use rustc_attr_ir::{self as attr, find_attr};
3535
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
3636
use rustc_data_structures::intern::Interned;
37+
use rustc_data_structures::snapshot_vec::VecLike;
3738
use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher};
3839
use rustc_errors::{Diag, ErrorGuaranteed};
3940
use rustc_hir as hir;
@@ -1016,6 +1017,27 @@ struct ClauseCompressor<'tcx> {
10161017
}
10171018

10181019
impl<'tcx> ClauseCompressor<'tcx> {
1020+
/// Construct a `ClauseCompressor` from an existing [`ParamEnv`] without decompressing its
1021+
/// contents. This is useful when adding clauses to an existing environment without
1022+
/// decompressing and re-interning compressed clauses.
1023+
fn from_param_env(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Self {
1024+
let mut compressed_clauses = Vec::new();
1025+
let mut uncompressed_clauses = Vec::new();
1026+
1027+
for clause in param_env.caller_bounds {
1028+
match clause.kind().skip_binder() {
1029+
ty::ClauseKind::CompressedTrait(c) => compressed_clauses.push(c),
1030+
_ => uncompressed_clauses.push(clause),
1031+
}
1032+
}
1033+
1034+
Self {
1035+
tcx,
1036+
compressed_clauses,
1037+
uncompressed_clauses,
1038+
}
1039+
}
1040+
10191041
fn new(tcx: TyCtxt<'tcx>) -> Self {
10201042
Self { tcx, compressed_clauses: Vec::new(), uncompressed_clauses: Vec::new() }
10211043
}
@@ -1106,6 +1128,16 @@ impl<'tcx> ParamEnv<'tcx> {
11061128
Self { caller_bounds: ListWithCachedTypeInfo::empty() }
11071129
}
11081130

1131+
#[inline]
1132+
pub fn extend(self, tcx: TyCtxt<'tcx>, new_bounds: impl Iterator<Item = ty::Clause<'tcx>>) -> Self {
1133+
let mut compressor = ClauseCompressor::from_param_env(tcx, self);
1134+
for clause in new_bounds {
1135+
compressor.add_clause(clause);
1136+
}
1137+
1138+
ParamEnv { caller_bounds: compressor.mk_clauses() }
1139+
}
1140+
11091141
#[inline]
11101142
pub fn caller_bounds(
11111143
self,

‎compiler/rustc_trait_selection/src/traits/auto_trait.rs‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
252252
})
253253
.collect::<Vec<ty::Clause<'tcx>>>();
254254

255-
let full_user_env = ty::ParamEnv::new(
256-
tcx,
257-
// FIXME(jdonszelmann): if this ends up being a perf problem for rustdoc (calling
258-
// all_clauses here) we could instead add a method to the caller_bounds iterator
259-
// that does "extending", which adds bounds to a paramenv, without decompressing
260-
// existing bounds.
261-
orig_env.caller_bounds().all_clauses(tcx).iter().chain(field_clauses),
262-
);
255+
let full_user_env = orig_env.extend(field_clauses);
263256

264257
let fresh_args = infcx.fresh_args_for_item(DUMMY_SP, adt_def.did());
265258
let fresh_ty = ty::EarlyBinder::bind(tcx, ty).instantiate(tcx, fresh_args).skip_norm_wip();
@@ -347,7 +340,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
347340
}));
348341

349342
let computed_clauses =
350-
param_env.caller_bounds().all_clauses(infcx.tcx).iter().collect::<Vec<_>>();
343+
param_env;
351344
let mut user_computed_clauses: FxIndexSet<_> =
352345
user_env.caller_bounds().all_clauses(infcx.tcx).iter().collect();
353346
let mut prev_user_computed_clauses = None::<FxIndexSet<_>>;
@@ -435,7 +428,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
435428

436429
let normalized_preds = elaborate(
437430
tcx,
438-
computed_clauses.iter().copied().chain(user_computed_clauses.iter().cloned()),
431+
computed_clauses.extend(tcx, user_computed_clauses),
439432
);
440433
new_env = ty::ParamEnv::new(tcx, normalized_preds);
441434
}

0 commit comments

Comments
 (0)