Skip to content

Commit d72444b

Browse files
committed
Swap PredicateObligations to SmallVec
1 parent 43e2168 commit d72444b

File tree

8 files changed

+28
-8
lines changed

8 files changed

+28
-8
lines changed

compiler/rustc_data_structures/src/obligation_forest/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ use std::fmt::Debug;
7575
use std::hash;
7676
use std::marker::PhantomData;
7777

78+
use smallvec::SmallVec;
7879
use tracing::debug;
7980

8081
use crate::fx::{FxHashMap, FxHashSet};
@@ -141,7 +142,7 @@ pub trait ObligationProcessor {
141142
#[derive(Debug)]
142143
pub enum ProcessResult<O, E> {
143144
Unchanged,
144-
Changed(Vec<O>),
145+
Changed(SmallVec<[O; 4]>),
145146
Error(E),
146147
}
147148

compiler/rustc_data_structures/src/smallvec.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ where
2727
impl<'a, A, F> ExtractIf<'a, A, F>
2828
where
2929
A: Array,
30+
F: FnMut(&mut A::Item) -> bool,
3031
{
3132
pub fn new(vec: &'a mut SmallVec<A>, filter: F) -> Self {
3233
let old_len = vec.len();

compiler/rustc_infer/src/traits/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_middle::traits::solve::Certainty;
1717
pub use rustc_middle::traits::*;
1818
use rustc_middle::ty::{self, Ty, TyCtxt, Upcast};
1919
use rustc_span::Span;
20+
use smallvec::SmallVec;
2021

2122
pub use self::ImplSource::*;
2223
pub use self::SelectionError::*;
@@ -84,7 +85,7 @@ pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
8485
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::TraitPredicate<'tcx>>;
8586
pub type PolyTraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
8687

87-
pub type PredicateObligations<'tcx> = Vec<PredicateObligation<'tcx>>;
88+
pub type PredicateObligations<'tcx> = SmallVec<[PredicateObligation<'tcx>; 4]>;
8889

8990
impl<'tcx> PredicateObligation<'tcx> {
9091
/// Flips the polarity of the inner predicate.

compiler/rustc_middle/src/traits/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -625,14 +625,14 @@ pub enum ImplSource<'tcx, N> {
625625
/// for some type parameter. The `Vec<N>` represents the
626626
/// obligations incurred from normalizing the where-clause (if
627627
/// any).
628-
Param(Vec<N>),
628+
Param(SmallVec<[N; 4]>),
629629

630630
/// Successful resolution for a builtin impl.
631-
Builtin(BuiltinImplSource, Vec<N>),
631+
Builtin(BuiltinImplSource, SmallVec<[N; 4]>),
632632
}
633633

634634
impl<'tcx, N> ImplSource<'tcx, N> {
635-
pub fn nested_obligations(self) -> Vec<N> {
635+
pub fn nested_obligations(self) -> SmallVec<[N; 4]> {
636636
match self {
637637
ImplSource::UserDefined(i) => i.nested,
638638
ImplSource::Param(n) | ImplSource::Builtin(_, n) => n,
@@ -686,7 +686,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
686686
pub struct ImplSourceUserDefinedData<'tcx, N> {
687687
pub impl_def_id: DefId,
688688
pub args: GenericArgsRef<'tcx>,
689-
pub nested: Vec<N>,
689+
pub nested: SmallVec<[N; 4]>,
690690
}
691691

692692
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]

compiler/rustc_trait_selection/src/solve/fulfill.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::marker::PhantomData;
22
use std::mem;
33
use std::ops::ControlFlow;
44

5+
use rustc_data_structures::smallvec::ExtractIf;
56
use rustc_infer::infer::InferCtxt;
67
use rustc_infer::traits::query::NoSolution;
78
use rustc_infer::traits::solve::{CandidateSource, GoalSource, MaybeCause};
@@ -81,7 +82,7 @@ impl<'tcx> ObligationStorage<'tcx> {
8182
// we get all obligations involved in the overflow. We pretty much check: if
8283
// we were to do another step of `select_where_possible`, which goals would
8384
// change.
84-
self.overflowed.extend(self.pending.extract_if(|o| {
85+
self.overflowed.extend(ExtractIf::new(&mut self.pending, |o| {
8586
let goal = o.clone().into();
8687
let result = <&SolverDelegate<'tcx>>::from(infcx)
8788
.evaluate_root_goal(goal, GenerateProofTree::No)

compiler/rustc_trait_selection/src/traits/fulfill.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_middle::mir::interpret::ErrorHandled;
1414
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
1515
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1616
use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt};
17+
use smallvec::SmallVec;
1718
use tracing::{debug, debug_span, instrument};
1819

1920
use super::project::{self, ProjectAndUnifyResult};
@@ -28,7 +29,7 @@ use crate::traits::normalize::normalize_with_depth_to;
2829
use crate::traits::project::{PolyProjectionObligation, ProjectionCacheKeyExt as _};
2930
use crate::traits::query::evaluate_obligation::InferCtxtExt;
3031

31-
pub(crate) type PendingPredicateObligations<'tcx> = Vec<PendingPredicateObligation<'tcx>>;
32+
pub(crate) type PendingPredicateObligations<'tcx> = SmallVec<[PendingPredicateObligation<'tcx>; 4]>;
3233

3334
impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
3435
/// Note that we include both the `ParamEnv` and the `Predicate`,

compiler/rustc_type_ir/src/fold.rs

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use std::mem;
4949

5050
use rustc_index::{Idx, IndexVec};
51+
use smallvec::SmallVec;
5152
use tracing::instrument;
5253

5354
use crate::data_structures::Lrc;
@@ -322,6 +323,12 @@ impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Vec<T> {
322323
}
323324
}
324325

326+
impl<I: Interner, T: TypeFoldable<I>, const N: usize> TypeFoldable<I> for SmallVec<[T; N]> {
327+
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
328+
self.into_iter().map(|t| t.try_fold_with(folder)).collect()
329+
}
330+
}
331+
325332
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Box<[T]> {
326333
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
327334
Vec::from(self).try_fold_with(folder).map(Vec::into_boxed_slice)

compiler/rustc_type_ir/src/visit.rs

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use std::ops::ControlFlow;
4747
use rustc_ast_ir::visit::VisitorResult;
4848
use rustc_ast_ir::{try_visit, walk_visitable_list};
4949
use rustc_index::{Idx, IndexVec};
50+
use smallvec::SmallVec;
5051

5152
use crate::data_structures::Lrc;
5253
use crate::inherent::*;
@@ -184,6 +185,13 @@ impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Vec<T> {
184185
}
185186
}
186187

188+
impl<I: Interner, T: TypeVisitable<I>, const N: usize> TypeVisitable<I> for SmallVec<[T; N]> {
189+
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
190+
walk_visitable_list!(visitor, self.iter());
191+
V::Result::output()
192+
}
193+
}
194+
187195
// `TypeFoldable` isn't impl'd for `&[T]`. It doesn't make sense in the general
188196
// case, because we can't return a new slice. But note that there are a couple
189197
// of trivial impls of `TypeFoldable` for specific slice types elsewhere.

0 commit comments

Comments
 (0)