Skip to content

Commit

Permalink
don't store OriginalQueryValues::universe_map
Browse files Browse the repository at this point in the history
ParamEnv is canonicalized in *queries input* rather than query response.
In such case we don't "preserve universes" of canonical variable.
This means that `universe_map` always has the default value, which is
wasteful to store in the cache.
  • Loading branch information
aliemjay committed Dec 13, 2023
1 parent f38d1e9 commit 8533819
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl<'tcx> InferCtxt<'tcx> {
{
let (param_env, value) = value.into_parts();
let base = self.tcx.canonical_param_env_cache.get_or_insert(
self.tcx,
param_env,
query_state,
|query_state| {
Expand Down
15 changes: 11 additions & 4 deletions compiler/rustc_middle/src/infer/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,29 +300,36 @@ pub struct CanonicalParamEnvCache<'tcx> {
map: Lock<
FxHashMap<
ty::ParamEnv<'tcx>,
(Canonical<'tcx, ty::ParamEnv<'tcx>>, OriginalQueryValues<'tcx>),
(Canonical<'tcx, ty::ParamEnv<'tcx>>, &'tcx [GenericArg<'tcx>]),
>,
>,
}

impl<'tcx> CanonicalParamEnvCache<'tcx> {
pub fn get_or_insert(
&self,
tcx: TyCtxt<'tcx>,
key: ty::ParamEnv<'tcx>,
state: &mut OriginalQueryValues<'tcx>,
canonicalize_op: impl FnOnce(
&mut OriginalQueryValues<'tcx>,
) -> Canonical<'tcx, ty::ParamEnv<'tcx>>,
) -> Canonical<'tcx, ty::ParamEnv<'tcx>> {
assert_eq!(state.var_values.len(), 0);
assert_eq!(state.universe_map.len(), 1);
debug_assert_eq!(&*state.universe_map, &[ty::UniverseIndex::ROOT]);

match self.map.borrow().entry(key) {
Entry::Occupied(e) => {
let (canonical, state_cached) = e.get();
state.clone_from(state_cached);
let (canonical, var_values) = e.get();
state.var_values.extend_from_slice(var_values);
canonical.clone()
}
Entry::Vacant(e) => {
let canonical = canonicalize_op(state);
e.insert((canonical.clone(), state.clone()));
let OriginalQueryValues { var_values, universe_map } = state;
assert_eq!(universe_map.len(), 1);
e.insert((canonical.clone(), tcx.arena.alloc_slice(var_values)));
canonical
}
}
Expand Down

0 comments on commit 8533819

Please sign in to comment.