Skip to content

Commit 8df8a1b

Browse files
committed
Swap PredicateObligations to SmallVec
1 parent 6e022b4 commit 8df8a1b

File tree

9 files changed

+63
-41
lines changed

9 files changed

+63
-41
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/obligation_forest/tests.rs

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::fmt;
22

3+
use smallvec::smallvec;
4+
35
use super::*;
46

57
impl<'a> super::ForestObligation for &'a str {
@@ -101,9 +103,9 @@ fn push_pop() {
101103
// |-> A.3
102104
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
103105
|obligation| match *obligation {
104-
"A" => ProcessResult::Changed(vec!["A.1", "A.2", "A.3"]),
106+
"A" => ProcessResult::Changed(smallvec!["A.1", "A.2", "A.3"]),
105107
"B" => ProcessResult::Error("B is for broken"),
106-
"C" => ProcessResult::Changed(vec![]),
108+
"C" => ProcessResult::Changed(smallvec![]),
107109
"A.1" | "A.2" | "A.3" => ProcessResult::Unchanged,
108110
_ => unreachable!(),
109111
},
@@ -123,8 +125,8 @@ fn push_pop() {
123125
|obligation| match *obligation {
124126
"A.1" => ProcessResult::Unchanged,
125127
"A.2" => ProcessResult::Unchanged,
126-
"A.3" => ProcessResult::Changed(vec!["A.3.i"]),
127-
"D" => ProcessResult::Changed(vec!["D.1", "D.2"]),
128+
"A.3" => ProcessResult::Changed(smallvec!["A.3.i"]),
129+
"D" => ProcessResult::Changed(smallvec!["D.1", "D.2"]),
128130
"A.3.i" | "D.1" | "D.2" => ProcessResult::Unchanged,
129131
_ => unreachable!(),
130132
},
@@ -139,11 +141,11 @@ fn push_pop() {
139141
// |-> D.2 |-> D.2.i
140142
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
141143
|obligation| match *obligation {
142-
"A.1" => ProcessResult::Changed(vec![]),
144+
"A.1" => ProcessResult::Changed(smallvec![]),
143145
"A.2" => ProcessResult::Error("A is for apple"),
144-
"A.3.i" => ProcessResult::Changed(vec![]),
145-
"D.1" => ProcessResult::Changed(vec!["D.1.i"]),
146-
"D.2" => ProcessResult::Changed(vec!["D.2.i"]),
146+
"A.3.i" => ProcessResult::Changed(smallvec![]),
147+
"D.1" => ProcessResult::Changed(smallvec!["D.1.i"]),
148+
"D.2" => ProcessResult::Changed(smallvec!["D.2.i"]),
147149
"D.1.i" | "D.2.i" => ProcessResult::Unchanged,
148150
_ => unreachable!(),
149151
},
@@ -158,7 +160,7 @@ fn push_pop() {
158160
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
159161
|obligation| match *obligation {
160162
"D.1.i" => ProcessResult::Error("D is for dumb"),
161-
"D.2.i" => ProcessResult::Changed(vec![]),
163+
"D.2.i" => ProcessResult::Changed(smallvec![]),
162164
_ => panic!("unexpected obligation {:?}", obligation),
163165
},
164166
|_| {},
@@ -184,10 +186,10 @@ fn success_in_grandchildren() {
184186

185187
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
186188
|obligation| match *obligation {
187-
"A" => ProcessResult::Changed(vec!["A.1", "A.2", "A.3"]),
188-
"A.1" => ProcessResult::Changed(vec![]),
189-
"A.2" => ProcessResult::Changed(vec!["A.2.i", "A.2.ii"]),
190-
"A.3" => ProcessResult::Changed(vec![]),
189+
"A" => ProcessResult::Changed(smallvec!["A.1", "A.2", "A.3"]),
190+
"A.1" => ProcessResult::Changed(smallvec![]),
191+
"A.2" => ProcessResult::Changed(smallvec!["A.2.i", "A.2.ii"]),
192+
"A.3" => ProcessResult::Changed(smallvec![]),
191193
"A.2.i" | "A.2.ii" => ProcessResult::Unchanged,
192194
_ => unreachable!(),
193195
},
@@ -201,7 +203,7 @@ fn success_in_grandchildren() {
201203
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
202204
|obligation| match *obligation {
203205
"A.2.i" => ProcessResult::Unchanged,
204-
"A.2.ii" => ProcessResult::Changed(vec![]),
206+
"A.2.ii" => ProcessResult::Changed(smallvec![]),
205207
_ => unreachable!(),
206208
},
207209
|_| {},
@@ -211,7 +213,7 @@ fn success_in_grandchildren() {
211213

212214
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
213215
|obligation| match *obligation {
214-
"A.2.i" => ProcessResult::Changed(vec!["A.2.i.a"]),
216+
"A.2.i" => ProcessResult::Changed(smallvec!["A.2.i.a"]),
215217
"A.2.i.a" => ProcessResult::Unchanged,
216218
_ => unreachable!(),
217219
},
@@ -222,7 +224,7 @@ fn success_in_grandchildren() {
222224

223225
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
224226
|obligation| match *obligation {
225-
"A.2.i.a" => ProcessResult::Changed(vec![]),
227+
"A.2.i.a" => ProcessResult::Changed(smallvec![]),
226228
_ => unreachable!(),
227229
},
228230
|_| {},
@@ -247,7 +249,7 @@ fn to_errors_no_throw() {
247249
forest.register_obligation("A");
248250
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
249251
|obligation| match *obligation {
250-
"A" => ProcessResult::Changed(vec!["A.1", "A.2", "A.3"]),
252+
"A" => ProcessResult::Changed(smallvec!["A.1", "A.2", "A.3"]),
251253
"A.1" | "A.2" | "A.3" => ProcessResult::Unchanged,
252254
_ => unreachable!(),
253255
},
@@ -269,7 +271,7 @@ fn diamond() {
269271
forest.register_obligation("A");
270272
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
271273
|obligation| match *obligation {
272-
"A" => ProcessResult::Changed(vec!["A.1", "A.2"]),
274+
"A" => ProcessResult::Changed(smallvec!["A.1", "A.2"]),
273275
"A.1" | "A.2" => ProcessResult::Unchanged,
274276
_ => unreachable!(),
275277
},
@@ -280,8 +282,8 @@ fn diamond() {
280282

281283
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
282284
|obligation| match *obligation {
283-
"A.1" => ProcessResult::Changed(vec!["D"]),
284-
"A.2" => ProcessResult::Changed(vec!["D"]),
285+
"A.1" => ProcessResult::Changed(smallvec!["D"]),
286+
"A.2" => ProcessResult::Changed(smallvec!["D"]),
285287
"D" => ProcessResult::Unchanged,
286288
_ => unreachable!(),
287289
},
@@ -295,7 +297,7 @@ fn diamond() {
295297
|obligation| match *obligation {
296298
"D" => {
297299
d_count += 1;
298-
ProcessResult::Changed(vec![])
300+
ProcessResult::Changed(smallvec![])
299301
}
300302
_ => unreachable!(),
301303
},
@@ -313,7 +315,7 @@ fn diamond() {
313315
forest.register_obligation("A'");
314316
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
315317
|obligation| match *obligation {
316-
"A'" => ProcessResult::Changed(vec!["A'.1", "A'.2"]),
318+
"A'" => ProcessResult::Changed(smallvec!["A'.1", "A'.2"]),
317319
"A'.1" | "A'.2" => ProcessResult::Unchanged,
318320
_ => unreachable!(),
319321
},
@@ -324,8 +326,8 @@ fn diamond() {
324326

325327
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
326328
|obligation| match *obligation {
327-
"A'.1" => ProcessResult::Changed(vec!["D'", "A'"]),
328-
"A'.2" => ProcessResult::Changed(vec!["D'"]),
329+
"A'.1" => ProcessResult::Changed(smallvec!["D'", "A'"]),
330+
"A'.2" => ProcessResult::Changed(smallvec!["D'"]),
329331
"D'" | "A'" => ProcessResult::Unchanged,
330332
_ => unreachable!(),
331333
},
@@ -366,7 +368,7 @@ fn done_dependency() {
366368

367369
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
368370
|obligation| match *obligation {
369-
"A: Sized" | "B: Sized" | "C: Sized" => ProcessResult::Changed(vec![]),
371+
"A: Sized" | "B: Sized" | "C: Sized" => ProcessResult::Changed(smallvec![]),
370372
_ => unreachable!(),
371373
},
372374
|_| {},
@@ -379,7 +381,9 @@ fn done_dependency() {
379381
forest.register_obligation("(A,B,C): Sized");
380382
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
381383
|obligation| match *obligation {
382-
"(A,B,C): Sized" => ProcessResult::Changed(vec!["A: Sized", "B: Sized", "C: Sized"]),
384+
"(A,B,C): Sized" => {
385+
ProcessResult::Changed(smallvec!["A: Sized", "B: Sized", "C: Sized"])
386+
}
383387
_ => unreachable!(),
384388
},
385389
|_| {},
@@ -399,10 +403,10 @@ fn orphan() {
399403

400404
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
401405
|obligation| match *obligation {
402-
"A" => ProcessResult::Changed(vec!["D", "E"]),
406+
"A" => ProcessResult::Changed(smallvec!["D", "E"]),
403407
"B" => ProcessResult::Unchanged,
404-
"C1" => ProcessResult::Changed(vec![]),
405-
"C2" => ProcessResult::Changed(vec![]),
408+
"C1" => ProcessResult::Changed(smallvec![]),
409+
"C2" => ProcessResult::Changed(smallvec![]),
406410
"D" | "E" => ProcessResult::Unchanged,
407411
_ => unreachable!(),
408412
},
@@ -416,7 +420,7 @@ fn orphan() {
416420
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
417421
|obligation| match *obligation {
418422
"D" | "E" => ProcessResult::Unchanged,
419-
"B" => ProcessResult::Changed(vec!["D"]),
423+
"B" => ProcessResult::Changed(smallvec!["D"]),
420424
_ => unreachable!(),
421425
},
422426
|_| {},
@@ -459,7 +463,7 @@ fn simultaneous_register_and_error() {
459463
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
460464
|obligation| match *obligation {
461465
"A" => ProcessResult::Error("An error"),
462-
"B" => ProcessResult::Changed(vec!["A"]),
466+
"B" => ProcessResult::Changed(smallvec!["A"]),
463467
_ => unreachable!(),
464468
},
465469
|_| {},
@@ -474,7 +478,7 @@ fn simultaneous_register_and_error() {
474478
let TestOutcome { completed: ok, errors: err, .. } = forest.process_obligations(&mut C(
475479
|obligation| match *obligation {
476480
"A" => ProcessResult::Error("An error"),
477-
"B" => ProcessResult::Changed(vec!["A"]),
481+
"B" => ProcessResult::Changed(smallvec!["A"]),
478482
_ => unreachable!(),
479483
},
480484
|_| {},

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/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ rustc_span = { path = "../rustc_span" }
2525
rustc_target = { path = "../rustc_target" }
2626
rustc_transmute = { path = "../rustc_transmute", features = ["rustc"] }
2727
rustc_type_ir = { path = "../rustc_type_ir" }
28-
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
28+
smallvec = { version = "1.8.1", features = ["union", "may_dangle", "drain_filter"] }
2929
tracing = "0.1"
3030
# tidy-alphabetical-end

compiler/rustc_trait_selection/src/solve/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'tcx> ObligationStorage<'tcx> {
8181
// we get all obligations involved in the overflow. We pretty much check: if
8282
// we were to do another step of `select_where_possible`, which goals would
8383
// change.
84-
self.overflowed.extend(self.pending.extract_if(|o| {
84+
self.overflowed.extend(self.pending.drain_filter(|o| {
8585
let goal = o.clone().into();
8686
let result = <&SolverDelegate<'tcx>>::from(infcx)
8787
.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)