aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/infer/expr.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-24 21:45:26 +0000
committerGitHub <[email protected]>2019-11-24 21:45:26 +0000
commitf7f9757b6b144385ab8ce57b15764473b1f57331 (patch)
treedfcb584a03bd4e049291a05912508690cd8f9247 /crates/ra_hir/src/ty/infer/expr.rs
parent6a5dea778b16a35d765a3a98f2ac303c5cc3df25 (diff)
parentd06904e90cdc1603ffcb714e70dab83905221f72 (diff)
Merge #2396
2396: Switch to variant-granularity field type inference r=flodiebold a=matklad r? @flodiebold Previously, we had a `ty` query for each field. This PR switcthes to a query per struct, which returns an `ArenaMap` with `Ty`s. I don't know which approach is better. What is bugging me about the original approach is that, if we do all queries on the "leaf" defs, in practice we get a ton of queries which repeatedly reach into the parent definition to compute module, resolver, etc. This *seems* wasteful (but I don't think this is really what causes any perf problems for us). At the same time, I've been looking at Kotlin, and they seem to use the general pattern of analyzing the *parent* definition, and storing info about children into a `BindingContext`. I don't really which way is preferable. I think I want to try this approach, where query granularity generally mirrors the data granularity. The primary motivation for me here is probably just hope that we can avoid adding a ton of helpers to a `StructField`, and maybe in general avoid the need to switch to a global `StructField`, using `LocalStructFieldId` most of the time internally. For external API (ie, for `ra_ide_api`), I think we should continue with fine-grained `StructField::ty` approach, which internally fetches the table for the whole struct and indexes into it. In terms of actual memory savings, the results are as follows: ``` This PR: 142kb FieldTypesQuery (deps) 38kb FieldTypesQuery Status Quo: 208kb TypeForFieldQuery (deps) 18kb TypeForFieldQuery ``` Note how the table itself occupies more than twice as much space! I don't have an explanation for this: a plausible hypothesis is that single-field structs are very common and for them the table is a pessimisation. THere's noticiable wallclock time difference. Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty/infer/expr.rs')
-rw-r--r--crates/ra_hir/src/ty/infer/expr.rs11
1 files changed, 8 insertions, 3 deletions
diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs
index 2996920c6..663ff9435 100644
--- a/crates/ra_hir/src/ty/infer/expr.rs
+++ b/crates/ra_hir/src/ty/infer/expr.rs
@@ -214,6 +214,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
214 self.unify(&ty, &expected.ty); 214 self.unify(&ty, &expected.ty);
215 215
216 let substs = ty.substs().unwrap_or_else(Substs::empty); 216 let substs = ty.substs().unwrap_or_else(Substs::empty);
217 let field_types =
218 def_id.map(|it| self.db.field_types(it.into())).unwrap_or_default();
217 for (field_idx, field) in fields.iter().enumerate() { 219 for (field_idx, field) in fields.iter().enumerate() {
218 let field_def = def_id.and_then(|it| match it.field(self.db, &field.name) { 220 let field_def = def_id.and_then(|it| match it.field(self.db, &field.name) {
219 Some(field) => Some(field), 221 Some(field) => Some(field),
@@ -228,8 +230,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
228 if let Some(field_def) = field_def { 230 if let Some(field_def) = field_def {
229 self.result.record_field_resolutions.insert(field.expr, field_def); 231 self.result.record_field_resolutions.insert(field.expr, field_def);
230 } 232 }
231 let field_ty = 233 let field_ty = field_def
232 field_def.map_or(Ty::Unknown, |field| field.ty(self.db)).subst(&substs); 234 .map_or(Ty::Unknown, |it| field_types[it.id].clone())
235 .subst(&substs);
233 self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty)); 236 self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty));
234 } 237 }
235 if let Some(expr) = spread { 238 if let Some(expr) = spread {
@@ -252,7 +255,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
252 .and_then(|idx| a_ty.parameters.0.get(idx).cloned()), 255 .and_then(|idx| a_ty.parameters.0.get(idx).cloned()),
253 TypeCtor::Adt(Adt::Struct(s)) => s.field(self.db, name).map(|field| { 256 TypeCtor::Adt(Adt::Struct(s)) => s.field(self.db, name).map(|field| {
254 self.write_field_resolution(tgt_expr, field); 257 self.write_field_resolution(tgt_expr, field);
255 field.ty(self.db).subst(&a_ty.parameters) 258 self.db.field_types(s.id.into())[field.id]
259 .clone()
260 .subst(&a_ty.parameters)
256 }), 261 }),
257 _ => None, 262 _ => None,
258 }, 263 },