Skip to content

Commit d57fdcf

Browse files
committed
Allocate one vec, instead of one per invocation
1 parent a34c079 commit d57fdcf

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use crate::infer::canonical::{
1414
};
1515
use crate::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
1616
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
17-
use crate::infer::{InferCtxt, InferOk, InferResult, NllRegionVariableOrigin};
17+
use crate::infer::{InferCtxt, InferOk, InferResult, NllRegionVariableOrigin, TypeError};
1818
use crate::traits::query::{Fallible, NoSolution};
1919
use crate::traits::TraitEngine;
20-
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
20+
use crate::traits::{Obligation, ObligationCause, PredicateObligation, PredicateObligations};
2121
use rustc_data_structures::captures::Captures;
2222
use rustc_index::vec::Idx;
2323
use rustc_index::vec::IndexVec;
@@ -219,7 +219,8 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
219219
original_values: &OriginalQueryValues<'tcx>,
220220
query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
221221
output_query_region_constraints: &mut QueryRegionConstraints<'tcx>,
222-
) -> InferResult<'tcx, R>
222+
obligations: &mut PredicateObligations<'tcx>
223+
) -> Result<R, TypeError<'tcx>>
223224
where
224225
R: Debug + TypeFoldable<'tcx>,
225226
{
@@ -229,7 +230,6 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
229230
// Compute `QueryOutlivesConstraint` values that unify each of
230231
// the original values `v_o` that was canonicalized into a
231232
// variable...
232-
let mut obligations = vec![];
233233

234234
for (index, original_value) in original_values.var_values.iter().enumerate() {
235235
// ...with the value `v_r` of that variable from the query.
@@ -263,7 +263,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
263263
infcx: self,
264264
param_env,
265265
cause,
266-
obligations: &mut obligations,
266+
obligations: &mut *obligations,
267267
},
268268
ty::Variance::Invariant,
269269
)
@@ -277,7 +277,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
277277
infcx: self,
278278
param_env,
279279
cause,
280-
obligations: &mut obligations,
280+
obligations: &mut *obligations,
281281
},
282282
ty::Variance::Invariant,
283283
)
@@ -316,7 +316,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
316316
let user_result: R =
317317
query_response.substitute_projected(self.tcx, &result_subst, |q_r| q_r.value.clone());
318318

319-
Ok(InferOk { value: user_result, obligations })
319+
Ok(user_result)
320320
}
321321

322322
/// Given the original values and the (canonicalized) result from

compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::infer::canonical::{
22
Canonicalized, CanonicalizedQueryResponse, OriginalQueryValues, QueryRegionConstraints,
33
};
4-
use crate::infer::{InferCtxt, InferOk};
4+
use crate::infer::{InferCtxt};
55
use crate::traits::query::Fallible;
66
use crate::traits::ObligationCause;
77
use rustc_infer::infer::canonical::{Canonical, Certainty};
@@ -81,14 +81,14 @@ pub trait QueryTypeOp<'tcx>: fmt::Debug + Copy + TypeFoldable<'tcx> + 'tcx {
8181
query_key: ParamEnvAnd<'tcx, Self>,
8282
infcx: &InferCtxt<'_, 'tcx>,
8383
output_query_region_constraints: &mut QueryRegionConstraints<'tcx>,
84+
obligations: &mut PredicateObligations<'tcx>,
8485
) -> Fallible<(
8586
Self::QueryResponse,
8687
Option<Canonical<'tcx, ParamEnvAnd<'tcx, Self>>>,
87-
PredicateObligations<'tcx>,
8888
Certainty,
8989
)> {
9090
if let Some(result) = QueryTypeOp::try_fast_path(infcx.tcx, &query_key) {
91-
return Ok((result, None, vec![], Certainty::Proven));
91+
return Ok((result, None, Certainty::Proven));
9292
}
9393

9494
// FIXME(#33684) -- We need to use
@@ -101,16 +101,17 @@ pub trait QueryTypeOp<'tcx>: fmt::Debug + Copy + TypeFoldable<'tcx> + 'tcx {
101101
infcx.canonicalize_query_keep_static(query_key, &mut canonical_var_values);
102102
let canonical_result = Self::perform_query(infcx.tcx, canonical_self)?;
103103

104-
let InferOk { value, obligations } = infcx
104+
let value = infcx
105105
.instantiate_nll_query_response_and_region_obligations(
106106
&ObligationCause::dummy(),
107107
old_param_env,
108108
&canonical_var_values,
109109
canonical_result,
110110
output_query_region_constraints,
111+
obligations,
111112
)?;
112113

113-
Ok((value, Some(canonical_self), obligations, canonical_result.value.certainty))
114+
Ok((value, Some(canonical_self), canonical_result.value.certainty))
114115
}
115116
}
116117

@@ -122,8 +123,9 @@ where
122123

123124
fn fully_perform(self, infcx: &InferCtxt<'_, 'tcx>) -> Fallible<TypeOpOutput<'tcx, Self>> {
124125
let mut region_constraints = QueryRegionConstraints::default();
125-
let (output, canonicalized_query, mut obligations, _) =
126-
Q::fully_perform_into(self, infcx, &mut region_constraints)?;
126+
let mut obligations = Vec::new();
127+
let (output, canonicalized_query, _) =
128+
Q::fully_perform_into(self, infcx, &mut region_constraints, &mut obligations)?;
127129

128130
// Typically, instantiating NLL query results does not
129131
// create obligations. However, in some cases there
@@ -133,15 +135,15 @@ where
133135
while !obligations.is_empty() {
134136
trace!("{:#?}", obligations);
135137
let mut progress = false;
136-
for obligation in std::mem::take(&mut obligations) {
137-
let obligation = infcx.resolve_vars_if_possible(obligation);
138+
for _ in 0..obligations.len() {
139+
let obligation = infcx.resolve_vars_if_possible(obligations.swap_remove(0));
138140
match ProvePredicate::fully_perform_into(
139141
obligation.param_env.and(ProvePredicate::new(obligation.predicate)),
140142
infcx,
141143
&mut region_constraints,
144+
&mut obligations,
142145
) {
143-
Ok(((), _, new, certainty)) => {
144-
obligations.extend(new);
146+
Ok(((), _, certainty)) => {
145147
progress = true;
146148
if let Certainty::Ambiguous = certainty {
147149
obligations.push(obligation);

0 commit comments

Comments
 (0)