Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate obligations in opt_normalize_projection_type #90913

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#![feature(control_flow_enum)]
#![feature(min_specialization)]
#![feature(label_break_value)]
#![feature(hash_raw_entry)]
#![recursion_limit = "512"] // For rustdoc

#[macro_use]
Expand Down
63 changes: 63 additions & 0 deletions compiler/rustc_infer/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ mod project;
mod structural_impls;
pub mod util;

use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
use rustc_span::Span;
use std::borrow::{Borrow, Cow};
use std::collections::hash_map::RawEntryMut;
use std::hash::Hash;

pub use self::FulfillmentErrorCode::*;
pub use self::ImplSource::*;
Expand Down Expand Up @@ -95,6 +99,65 @@ pub enum FulfillmentErrorCode<'tcx> {
CodeAmbiguity,
}

pub struct ObligationsDedup<'a, 'tcx, T> {
obligations: &'a mut Vec<Obligation<'tcx, T>>,
}

impl<'a, 'tcx, T: 'tcx> ObligationsDedup<'a, 'tcx, T>
where
T: Clone + Hash + Eq,
{
pub fn from_vec(vec: &'a mut Vec<Obligation<'tcx, T>>) -> Self {
ObligationsDedup { obligations: vec }
}

pub fn extend<'b>(&mut self, iter: impl ExactSizeIterator<Item = Cow<'b, Obligation<'tcx, T>>>)
where
'tcx: 'b,
{
// obligation tracing has shown that initial batches added to an empty vec do not
// contain any duplicates, so there's no need to attempt deduplication
if self.obligations.is_empty() {
*self.obligations = iter.into_iter().map(Cow::into_owned).collect();
return;
}

let initial_size = self.obligations.len();
let current_capacity = self.obligations.capacity();
let iter = iter.into_iter();
let expected_new = iter.len();
let combined_size = initial_size + expected_new;

if combined_size <= 16 || combined_size <= current_capacity {
// small case/not crossing a power of two. don't bother with dedup
self.obligations.extend(iter.map(Cow::into_owned));
} else {
// crossing power of two threshold. this would incur a vec growth anyway if we didn't do
// anything. piggyback a dedup on that
let mut seen = FxHashMap::default();
seen.reserve(initial_size);

let mut is_duplicate = move |obligation: &Obligation<'tcx, _>| -> bool {
return match seen.raw_entry_mut().from_key(obligation) {
RawEntryMut::Occupied(..) => true,
RawEntryMut::Vacant(vacant) => {
vacant.insert(obligation.clone(), ());
false
}
};
};

self.obligations.retain(|obligation| !is_duplicate(obligation));
self.obligations.extend(iter.filter_map(|obligation| {
if is_duplicate(obligation.borrow()) {
return None;
}
Some(obligation.into_owned())
}));
}
}
}

impl<'tcx, O> Obligation<'tcx, O> {
pub fn new(
cause: ObligationCause<'tcx>,
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_infer/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ impl<'tcx> ProjectionCache<'_, 'tcx> {
Ok(())
}

pub fn try_start_borrowed<'a, T>(
&'a mut self,
key: ProjectionCacheKey<'tcx>,
with: impl FnOnce(&'_ ProjectionCacheEntry<'tcx>) -> T + 'a,
) -> Option<T> {
let mut map = self.map();
if let Some(entry) = map.get(&key) {
return Some(with(entry));
}

map.insert(key, ProjectionCacheEntry::InProgress);
None
}

/// Indicates that `key` was normalized to `value`.
pub fn insert_ty(&mut self, key: ProjectionCacheKey<'tcx>, value: NormalizedTy<'tcx>) {
debug!(
Expand Down
133 changes: 74 additions & 59 deletions compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
use rustc_span::symbol::sym;

use std::borrow::Cow;
use std::collections::BTreeMap;

use crate::traits::ObligationsDedup;
pub use rustc_middle::traits::Reveal;

pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
Expand Down Expand Up @@ -839,6 +841,8 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>(
// mode, which could lead to using incorrect cache results.
let use_cache = !selcx.is_intercrate();

let mut obligations = ObligationsDedup::from_vec(obligations);

let projection_ty = infcx.resolve_vars_if_possible(projection_ty);
let cache_key = ProjectionCacheKey::new(projection_ty);

Expand All @@ -850,65 +854,76 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>(
// bounds. It might be the case that we want two distinct caches,
// or else another kind of cache entry.

let cache_result = if use_cache {
infcx.inner.borrow_mut().projection_cache().try_start(cache_key)
} else {
Ok(())
};
match cache_result {
Ok(()) => debug!("no cache"),
Err(ProjectionCacheEntry::Ambiguous) => {
// If we found ambiguity the last time, that means we will continue
// to do so until some type in the key changes (and we know it
// hasn't, because we just fully resolved it).
debug!("found cache entry: ambiguous");
return Ok(None);
}
Err(ProjectionCacheEntry::InProgress) => {
// Under lazy normalization, this can arise when
// bootstrapping. That is, imagine an environment with a
// where-clause like `A::B == u32`. Now, if we are asked
// to normalize `A::B`, we will want to check the
// where-clauses in scope. So we will try to unify `A::B`
// with `A::B`, which can trigger a recursive
// normalization.

debug!("found cache entry: in-progress");

// Cache that normalizing this projection resulted in a cycle. This
// should ensure that, unless this happens within a snapshot that's
// rolled back, fulfillment or evaluation will notice the cycle.
if use_cache {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can change this to if !use_cache { return Ok(()) } and not have to add the additional indentation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the cache is not used (or when there's a cache miss) then the code below my changes is executed.

let result =
infcx.inner.borrow_mut().projection_cache().try_start_borrowed(cache_key, |cached| {
match cached {
ProjectionCacheEntry::NormalizedTy(ty) => {
// This is the hottest path in this function.
//
// If we find the value in the cache, then return it along
// with the obligations that went along with it. Note
// that, when using a fulfillment context, these
// obligations could in principle be ignored: they have
// already been registered when the cache entry was
// created (and hence the new ones will quickly be
// discarded as duplicated). But when doing trait
// evaluation this is not the case, and dropping the trait
// evaluations can causes ICEs (e.g., #43132).
debug!(?ty, "found normalized ty");
obligations.extend(ty.obligations.iter().map(Cow::Borrowed));
Ok(Some(ty.value))
}
cached @ _ => Err(cached.clone()),
}
});

if use_cache {
infcx.inner.borrow_mut().projection_cache().recur(cache_key);
match result {
Some(Ok(ret)) => return Ok(ret),
Some(Err(cached)) => {
return match cached {
ProjectionCacheEntry::Ambiguous => {
// If we found ambiguity the last time, that means we will continue
// to do so until some type in the key changes (and we know it
// hasn't, because we just fully resolved it).
debug!("found cache entry: ambiguous");
Ok(None)
}
ProjectionCacheEntry::InProgress => {
// Under lazy normalization, this can arise when
// bootstrapping. That is, imagine an environment with a
// where-clause like `A::B == u32`. Now, if we are asked
// to normalize `A::B`, we will want to check the
// where-clauses in scope. So we will try to unify `A::B`
// with `A::B`, which can trigger a recursive
// normalization.

debug!("found cache entry: in-progress");

// Cache that normalizing this projection resulted in a cycle. This
// should ensure that, unless this happens within a snapshot that's
// rolled back, fulfillment or evaluation will notice the cycle.

if use_cache {
infcx.inner.borrow_mut().projection_cache().recur(cache_key);
}
Err(InProgress)
}
ProjectionCacheEntry::Recur => {
debug!("recur cache");
Err(InProgress)
}
ProjectionCacheEntry::Error => {
debug!("opt_normalize_projection_type: found error");
let result =
normalize_to_error(selcx, param_env, projection_ty, cause, depth);
obligations.extend(result.obligations.into_iter().map(Cow::Owned));
Ok(Some(result.value))
}
_ => unreachable!("unexpected variant"),
};
}
return Err(InProgress);
}
Err(ProjectionCacheEntry::Recur) => {
debug!("recur cache");
return Err(InProgress);
}
Err(ProjectionCacheEntry::NormalizedTy(ty)) => {
// This is the hottest path in this function.
//
// If we find the value in the cache, then return it along
// with the obligations that went along with it. Note
// that, when using a fulfillment context, these
// obligations could in principle be ignored: they have
// already been registered when the cache entry was
// created (and hence the new ones will quickly be
// discarded as duplicated). But when doing trait
// evaluation this is not the case, and dropping the trait
// evaluations can causes ICEs (e.g., #43132).
debug!(?ty, "found normalized ty");
obligations.extend(ty.obligations);
return Ok(Some(ty.value));
}
Err(ProjectionCacheEntry::Error) => {
debug!("opt_normalize_projection_type: found error");
let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
obligations.extend(result.obligations);
return Ok(Some(result.value));
_ => {}
}
}

Expand Down Expand Up @@ -966,7 +981,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>(
if use_cache {
infcx.inner.borrow_mut().projection_cache().insert_ty(cache_key, result.clone());
}
obligations.extend(result.obligations);
obligations.extend(result.obligations.into_iter().map(Cow::Owned));
Ok(Some(result.value))
}
Ok(ProjectedTy::NoProgress(projected_ty)) => {
Expand Down Expand Up @@ -996,7 +1011,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>(
infcx.inner.borrow_mut().projection_cache().error(cache_key);
}
let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
obligations.extend(result.obligations);
obligations.extend(result.obligations.into_iter().map(Cow::Owned));
Ok(Some(result.value))
}
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// This code is hot enough that it's worth avoiding the allocation
// required for the FxHashSet when possible. Special-casing lengths 0,
// 1 and 2 covers roughly 75-80% of the cases.
//
// Ideally we would perform deduplication incrementally in the predicates
// loop above to prevent excessive Vec growth but that would require
// a Vec::range_retain or similar method.
if obligations.len() <= 1 {
// No possibility of duplicates.
} else if obligations.len() == 2 {
Expand Down