Skip to content

Commit 39ebc37

Browse files
committed
Auto merge of rust-lang#15436 - Veykril:temp-alloc, r=Veykril
Preserve `resolve_obligations_as_possible` temporary allocation across calls This saves ~20ms in the highlihting bench on my machine ``` 284ms - highlight 150ms - infer:wait @ per_query_memory_usage 150ms - infer_query 0 - PerNs::filter_visibility (436 calls) 0 - crate_def_map:wait (336 calls) 2ms - deref_by_trait (909 calls) 0 - generic_params_query (1 calls) 0 - inherent_impls_in_block_query (1 calls) 107ms - resolve_obligations_as_possible (17013 calls) 0 - trait_solve::wait (1017 calls) 0 - PerNs::filter_visibility (13 calls) 17ms - Semantics::analyze_impl (19 calls) 0 - SourceBinder::to_module_def (30 calls) 0 - attrs_query (6 calls) 0 - classify_lifetime (1 calls) 0 - classify_lifetime_ref (4 calls) 35ms - classify_name (28 calls) 54ms - classify_name_ref (452 calls) 0 - crate_def_map:wait (375 calls) 7ms - descend_into_macros (776 calls) 0 - generic_params_query (5 calls) 0 - impl_data_with_diagnostics_query (1 calls) 17ms - infer:wait (32 calls) 0 - resolve_obligations_as_possible (1 calls) 0 - source_file_to_def (1 calls) 0 - trait_solve::wait (1 calls) ``` to ``` 256ms - highlight 121ms - infer:wait @ per_query_memory_usage 121ms - infer_query 0 - PerNs::filter_visibility (436 calls) 0 - crate_def_map:wait (336 calls) 2ms - deref_by_trait (909 calls) 0 - generic_params_query (1 calls) 0 - inherent_impls_in_block_query (1 calls) 81ms - resolve_obligations_as_possible (17013 calls) 0 - trait_solve::wait (1017 calls) 0 - PerNs::filter_visibility (13 calls) 17ms - Semantics::analyze_impl (19 calls) 0 - SourceBinder::to_module_def (30 calls) 0 - attrs_query (6 calls) 0 - classify_lifetime (1 calls) 0 - classify_lifetime_ref (4 calls) 35ms - classify_name (28 calls) 56ms - classify_name_ref (452 calls) 0 - crate_def_map:wait (375 calls) 7ms - descend_into_macros (776 calls) 0 - generic_params_query (5 calls) 0 - impl_data_with_diagnostics_query (1 calls) 16ms - infer:wait (32 calls) 0 - resolve_obligations_as_possible (1 calls) 0 - source_file_to_def (1 calls) 0 - trait_solve::wait (1 calls) ```
2 parents 1fde334 + c1c8e78 commit 39ebc37

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

crates/hir-ty/src/infer/unify.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ pub(crate) struct InferenceTable<'a> {
143143
var_unification_table: ChalkInferenceTable,
144144
type_variable_table: Vec<TypeVariableFlags>,
145145
pending_obligations: Vec<Canonicalized<InEnvironment<Goal>>>,
146+
/// Double buffer used in [`Self::resolve_obligations_as_possible`] to cut down on
147+
/// temporary allocations.
148+
resolve_obligations_buffer: Vec<Canonicalized<InEnvironment<Goal>>>,
146149
}
147150

148151
pub(crate) struct InferenceTableSnapshot {
@@ -159,6 +162,7 @@ impl<'a> InferenceTable<'a> {
159162
var_unification_table: ChalkInferenceTable::new(),
160163
type_variable_table: Vec::new(),
161164
pending_obligations: Vec::new(),
165+
resolve_obligations_buffer: Vec::new(),
162166
}
163167
}
164168

@@ -510,10 +514,10 @@ impl<'a> InferenceTable<'a> {
510514
pub(crate) fn resolve_obligations_as_possible(&mut self) {
511515
let _span = profile::span("resolve_obligations_as_possible");
512516
let mut changed = true;
513-
let mut obligations = Vec::new();
514-
while changed {
515-
changed = false;
517+
let mut obligations = mem::take(&mut self.resolve_obligations_buffer);
518+
while mem::take(&mut changed) {
516519
mem::swap(&mut self.pending_obligations, &mut obligations);
520+
517521
for canonicalized in obligations.drain(..) {
518522
if !self.check_changed(&canonicalized) {
519523
self.pending_obligations.push(canonicalized);
@@ -528,6 +532,8 @@ impl<'a> InferenceTable<'a> {
528532
self.register_obligation_in_env(uncanonical);
529533
}
530534
}
535+
self.resolve_obligations_buffer = obligations;
536+
self.resolve_obligations_buffer.clear();
531537
}
532538

533539
pub(crate) fn fudge_inference<T: TypeFoldable<Interner>>(

0 commit comments

Comments
 (0)