Skip to content

Commit 97a427c

Browse files
authored
Merge pull request #21167 from Veykril/push-xksxokxxwyqw
perf: Shrink `InferenceResult` by ~40 bytes
2 parents e07aa92 + da79a56 commit 97a427c

File tree

8 files changed

+57
-28
lines changed

8 files changed

+57
-28
lines changed

src/tools/rust-analyzer/Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ dependencies = [
905905
"syntax",
906906
"test-fixture",
907907
"test-utils",
908+
"thin-vec",
908909
"tracing",
909910
"tracing-subscriber",
910911
"tracing-tree",

src/tools/rust-analyzer/crates/hir-ty/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ hir-expand.workspace = true
5353
base-db.workspace = true
5454
syntax.workspace = true
5555
span.workspace = true
56+
thin-vec = "0.2.14"
5657

5758
[dev-dependencies]
5859
expect-test = "1.5.1"

src/tools/rust-analyzer/crates/hir-ty/src/infer.rs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use rustc_type_ir::{
5656
};
5757
use span::Edition;
5858
use stdx::never;
59+
use thin_vec::ThinVec;
5960
use triomphe::Arc;
6061

6162
use crate::{
@@ -489,26 +490,31 @@ pub struct InferenceResult<'db> {
489490
/// [`InferenceContext`] and store the tuples substitution there. This map is the reverse of
490491
/// that which allows us to resolve a [`TupleFieldId`]s type.
491492
tuple_field_access_types: FxHashMap<TupleId, Tys<'db>>,
492-
/// During inference this field is empty and [`InferenceContext::diagnostics`] is filled instead.
493-
diagnostics: Vec<InferenceDiagnostic<'db>>,
493+
494494
pub(crate) type_of_expr: ArenaMap<ExprId, Ty<'db>>,
495495
/// For each pattern record the type it resolves to.
496496
///
497497
/// **Note**: When a pattern type is resolved it may still contain
498498
/// unresolved or missing subpatterns or subpatterns of mismatched types.
499499
pub(crate) type_of_pat: ArenaMap<PatId, Ty<'db>>,
500500
pub(crate) type_of_binding: ArenaMap<BindingId, Ty<'db>>,
501-
pub(crate) type_of_type_placeholder: ArenaMap<TypeRefId, Ty<'db>>,
501+
pub(crate) type_of_type_placeholder: FxHashMap<TypeRefId, Ty<'db>>,
502502
pub(crate) type_of_opaque: FxHashMap<InternedOpaqueTyId, Ty<'db>>,
503-
pub(crate) type_mismatches: FxHashMap<ExprOrPatId, TypeMismatch<'db>>,
503+
504+
pub(crate) type_mismatches: Option<Box<FxHashMap<ExprOrPatId, TypeMismatch<'db>>>>,
504505
/// Whether there are any type-mismatching errors in the result.
505506
// FIXME: This isn't as useful as initially thought due to us falling back placeholders to
506507
// `TyKind::Error`.
507508
// Which will then mark this field.
508509
pub(crate) has_errors: bool,
510+
/// During inference this field is empty and [`InferenceContext::diagnostics`] is filled instead.
511+
diagnostics: ThinVec<InferenceDiagnostic<'db>>,
512+
509513
/// Interned `Error` type to return references to.
510514
// FIXME: Remove this.
511515
error_ty: Ty<'db>,
516+
517+
pub(crate) expr_adjustments: FxHashMap<ExprId, Box<[Adjustment<'db>]>>,
512518
/// Stores the types which were implicitly dereferenced in pattern binding modes.
513519
pub(crate) pat_adjustments: FxHashMap<PatId, Vec<Ty<'db>>>,
514520
/// Stores the binding mode (`ref` in `let ref x = 2`) of bindings.
@@ -525,10 +531,11 @@ pub struct InferenceResult<'db> {
525531
/// ```
526532
/// the first `rest` has implicit `ref` binding mode, but the second `rest` binding mode is `move`.
527533
pub(crate) binding_modes: ArenaMap<PatId, BindingMode>,
528-
pub(crate) expr_adjustments: FxHashMap<ExprId, Box<[Adjustment<'db>]>>,
534+
529535
pub(crate) closure_info: FxHashMap<InternedClosureId, (Vec<CapturedItem<'db>>, FnTrait)>,
530536
// FIXME: remove this field
531537
pub mutated_bindings_in_closure: FxHashSet<BindingId>,
538+
532539
pub(crate) coercion_casts: FxHashSet<ExprId>,
533540
}
534541

@@ -595,25 +602,31 @@ impl<'db> InferenceResult<'db> {
595602
}
596603
}
597604
pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch<'db>> {
598-
self.type_mismatches.get(&expr.into())
605+
self.type_mismatches.as_deref()?.get(&expr.into())
599606
}
600607
pub fn type_mismatch_for_pat(&self, pat: PatId) -> Option<&TypeMismatch<'db>> {
601-
self.type_mismatches.get(&pat.into())
608+
self.type_mismatches.as_deref()?.get(&pat.into())
602609
}
603610
pub fn type_mismatches(&self) -> impl Iterator<Item = (ExprOrPatId, &TypeMismatch<'db>)> {
604-
self.type_mismatches.iter().map(|(expr_or_pat, mismatch)| (*expr_or_pat, mismatch))
611+
self.type_mismatches
612+
.as_deref()
613+
.into_iter()
614+
.flatten()
615+
.map(|(expr_or_pat, mismatch)| (*expr_or_pat, mismatch))
605616
}
606617
pub fn expr_type_mismatches(&self) -> impl Iterator<Item = (ExprId, &TypeMismatch<'db>)> {
607-
self.type_mismatches.iter().filter_map(|(expr_or_pat, mismatch)| match *expr_or_pat {
608-
ExprOrPatId::ExprId(expr) => Some((expr, mismatch)),
609-
_ => None,
610-
})
618+
self.type_mismatches.as_deref().into_iter().flatten().filter_map(
619+
|(expr_or_pat, mismatch)| match *expr_or_pat {
620+
ExprOrPatId::ExprId(expr) => Some((expr, mismatch)),
621+
_ => None,
622+
},
623+
)
611624
}
612625
pub fn placeholder_types(&self) -> impl Iterator<Item = (TypeRefId, &Ty<'db>)> {
613-
self.type_of_type_placeholder.iter()
626+
self.type_of_type_placeholder.iter().map(|(&type_ref, ty)| (type_ref, ty))
614627
}
615628
pub fn type_of_type_placeholder(&self, type_ref: TypeRefId) -> Option<Ty<'db>> {
616-
self.type_of_type_placeholder.get(type_ref).copied()
629+
self.type_of_type_placeholder.get(&type_ref).copied()
617630
}
618631
pub fn closure_info(&self, closure: InternedClosureId) -> &(Vec<CapturedItem<'db>>, FnTrait) {
619632
self.closure_info.get(&closure).unwrap()
@@ -1063,13 +1076,14 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
10631076
type_of_type_placeholder.shrink_to_fit();
10641077
type_of_opaque.shrink_to_fit();
10651078

1066-
*has_errors |= !type_mismatches.is_empty();
1067-
1068-
for mismatch in (*type_mismatches).values_mut() {
1069-
mismatch.expected = table.resolve_completely(mismatch.expected);
1070-
mismatch.actual = table.resolve_completely(mismatch.actual);
1079+
if let Some(type_mismatches) = type_mismatches {
1080+
*has_errors = true;
1081+
for mismatch in type_mismatches.values_mut() {
1082+
mismatch.expected = table.resolve_completely(mismatch.expected);
1083+
mismatch.actual = table.resolve_completely(mismatch.actual);
1084+
}
1085+
type_mismatches.shrink_to_fit();
10711086
}
1072-
type_mismatches.shrink_to_fit();
10731087
diagnostics.retain_mut(|diagnostic| {
10741088
use InferenceDiagnostic::*;
10751089
match diagnostic {
@@ -1520,7 +1534,10 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
15201534
) -> Result<(), ()> {
15211535
let result = self.demand_eqtype_fixme_no_diag(expected, actual);
15221536
if result.is_err() {
1523-
self.result.type_mismatches.insert(id, TypeMismatch { expected, actual });
1537+
self.result
1538+
.type_mismatches
1539+
.get_or_insert_default()
1540+
.insert(id, TypeMismatch { expected, actual });
15241541
}
15251542
result
15261543
}

src/tools/rust-analyzer/crates/hir-ty/src/infer/coerce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ impl<'db, 'exprs> CoerceMany<'db, 'exprs> {
15141514

15151515
self.final_ty = Some(icx.types.error);
15161516

1517-
icx.result.type_mismatches.insert(
1517+
icx.result.type_mismatches.get_or_insert_default().insert(
15181518
expression.into(),
15191519
if label_expression_as_expected {
15201520
TypeMismatch { expected: found, actual: expected }

src/tools/rust-analyzer/crates/hir-ty/src/infer/diagnostics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use hir_def::expr_store::ExpressionStore;
1111
use hir_def::expr_store::path::Path;
1212
use hir_def::{hir::ExprOrPatId, resolver::Resolver};
1313
use la_arena::{Idx, RawIdx};
14+
use thin_vec::ThinVec;
1415

1516
use crate::{
1617
InferenceDiagnostic, InferenceTyDiagnosticSource, TyLoweringDiagnostic,
@@ -24,7 +25,7 @@ use crate::{
2425
// to our resolver and so we cannot have mutable reference, but we really want to have
2526
// ability to dispatch diagnostics during this work otherwise the code becomes a complete mess.
2627
#[derive(Debug, Default, Clone)]
27-
pub(super) struct Diagnostics<'db>(RefCell<Vec<InferenceDiagnostic<'db>>>);
28+
pub(super) struct Diagnostics<'db>(RefCell<ThinVec<InferenceDiagnostic<'db>>>);
2829

2930
impl<'db> Diagnostics<'db> {
3031
pub(super) fn push(&self, diagnostic: InferenceDiagnostic<'db>) {
@@ -41,7 +42,7 @@ impl<'db> Diagnostics<'db> {
4142
);
4243
}
4344

44-
pub(super) fn finish(self) -> Vec<InferenceDiagnostic<'db>> {
45+
pub(super) fn finish(self) -> ThinVec<InferenceDiagnostic<'db>> {
4546
self.0.into_inner()
4647
}
4748
}

src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl<'db> InferenceContext<'_, 'db> {
7171
if !could_unify {
7272
self.result
7373
.type_mismatches
74+
.get_or_insert_default()
7475
.insert(tgt_expr.into(), TypeMismatch { expected: expected_ty, actual: ty });
7576
}
7677
}
@@ -100,6 +101,7 @@ impl<'db> InferenceContext<'_, 'db> {
100101
Err(_) => {
101102
self.result
102103
.type_mismatches
104+
.get_or_insert_default()
103105
.insert(expr.into(), TypeMismatch { expected: target, actual: ty });
104106
target
105107
}
@@ -293,6 +295,7 @@ impl<'db> InferenceContext<'_, 'db> {
293295
if !could_unify {
294296
self.result
295297
.type_mismatches
298+
.get_or_insert_default()
296299
.insert(expr.into(), TypeMismatch { expected: expected_ty, actual: ty });
297300
}
298301
}
@@ -1188,6 +1191,7 @@ impl<'db> InferenceContext<'_, 'db> {
11881191
Err(_) => {
11891192
this.result
11901193
.type_mismatches
1194+
.get_or_insert_default()
11911195
.insert(tgt_expr.into(), TypeMismatch { expected: target, actual: ty });
11921196
target
11931197
}
@@ -1556,7 +1560,7 @@ impl<'db> InferenceContext<'_, 'db> {
15561560
)
15571561
.is_err()
15581562
{
1559-
this.result.type_mismatches.insert(
1563+
this.result.type_mismatches.get_or_insert_default().insert(
15601564
expr.into(),
15611565
TypeMismatch { expected: t, actual: this.types.unit },
15621566
);
@@ -2130,6 +2134,7 @@ impl<'db> InferenceContext<'_, 'db> {
21302134
// Don't report type mismatches if there is a mismatch in args count.
21312135
self.result
21322136
.type_mismatches
2137+
.get_or_insert_default()
21332138
.insert((*arg).into(), TypeMismatch { expected, actual: found });
21342139
}
21352140
}

src/tools/rust-analyzer/crates/hir-ty/src/infer/pat.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'db> InferenceContext<'_, 'db> {
331331
return self.pat_ty_after_adjustment(pat);
332332
}
333333
Err(_) => {
334-
self.result.type_mismatches.insert(
334+
self.result.type_mismatches.get_or_insert_default().insert(
335335
pat.into(),
336336
TypeMismatch { expected, actual: ty_inserted_vars },
337337
);
@@ -415,6 +415,7 @@ impl<'db> InferenceContext<'_, 'db> {
415415
Err(_) => {
416416
self.result
417417
.type_mismatches
418+
.get_or_insert_default()
418419
.insert(pat.into(), TypeMismatch { expected, actual: lhs_ty });
419420
// `rhs_ty` is returned so no further type mismatches are
420421
// reported because of this mismatch.
@@ -431,7 +432,10 @@ impl<'db> InferenceContext<'_, 'db> {
431432
let ty = self.insert_type_vars_shallow(ty);
432433
// FIXME: This never check is odd, but required with out we do inference right now
433434
if !expected.is_never() && !self.unify(ty, expected) {
434-
self.result.type_mismatches.insert(pat.into(), TypeMismatch { expected, actual: ty });
435+
self.result
436+
.type_mismatches
437+
.get_or_insert_default()
438+
.insert(pat.into(), TypeMismatch { expected, actual: ty });
435439
}
436440
self.write_pat_ty(pat, ty);
437441
self.pat_ty_after_adjustment(pat)

src/tools/rust-analyzer/crates/hir-ty/src/method_resolution/confirm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ impl<'a, 'b, 'db> ConfirmContext<'a, 'b, 'db> {
481481
}
482482
Err(_) => {
483483
if self.ctx.unstable_features.arbitrary_self_types {
484-
self.ctx.result.type_mismatches.insert(
484+
self.ctx.result.type_mismatches.get_or_insert_default().insert(
485485
self.expr.into(),
486486
TypeMismatch { expected: method_self_ty, actual: self_ty },
487487
);

0 commit comments

Comments
 (0)