diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-16 16:42:58 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-16 16:42:58 +0000 |
commit | adcc89137d3feea8f19fad461bbde6f4bce048e5 (patch) | |
tree | 160af959553ce57fdfcbc0a6c79bafcc3611aeea /crates/ra_hir_ty/src/display.rs | |
parent | 648df02953a6ebf87a5876668eceba208687e8a7 (diff) | |
parent | 9faea2364dee4fbc9391ad233c570b70256ef002 (diff) |
Merge #3584
3584: Switch to dynamic dispatch r=matklad a=matklad
Benches are in https://github.com/rust-analyzer/rust-analyzer/issues/1987#issuecomment-598807185
TL;DR:
* 33% faster release build
* slightly worse/same perf
* no changes for debug build
* slightly smaller binary
cc @flodiebold I genuinely don't know if it is a good idea or not.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_ty/src/display.rs')
-rw-r--r-- | crates/ra_hir_ty/src/display.rs | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs index 14e089cf4..a6ef44a31 100644 --- a/crates/ra_hir_ty/src/display.rs +++ b/crates/ra_hir_ty/src/display.rs | |||
@@ -9,8 +9,8 @@ use crate::{ | |||
9 | use hir_def::{generics::TypeParamProvenance, AdtId, AssocContainerId, Lookup}; | 9 | use hir_def::{generics::TypeParamProvenance, AdtId, AssocContainerId, Lookup}; |
10 | use hir_expand::name::Name; | 10 | use hir_expand::name::Name; |
11 | 11 | ||
12 | pub struct HirFormatter<'a, 'b, DB> { | 12 | pub struct HirFormatter<'a, 'b> { |
13 | pub db: &'a DB, | 13 | pub db: &'a dyn HirDatabase, |
14 | fmt: &'a mut fmt::Formatter<'b>, | 14 | fmt: &'a mut fmt::Formatter<'b>, |
15 | buf: String, | 15 | buf: String, |
16 | curr_size: usize, | 16 | curr_size: usize, |
@@ -19,20 +19,20 @@ pub struct HirFormatter<'a, 'b, DB> { | |||
19 | } | 19 | } |
20 | 20 | ||
21 | pub trait HirDisplay { | 21 | pub trait HirDisplay { |
22 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result; | 22 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result; |
23 | 23 | ||
24 | fn display<'a, DB>(&'a self, db: &'a DB) -> HirDisplayWrapper<'a, DB, Self> | 24 | fn display<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self> |
25 | where | 25 | where |
26 | Self: Sized, | 26 | Self: Sized, |
27 | { | 27 | { |
28 | HirDisplayWrapper(db, self, None, false) | 28 | HirDisplayWrapper(db, self, None, false) |
29 | } | 29 | } |
30 | 30 | ||
31 | fn display_truncated<'a, DB>( | 31 | fn display_truncated<'a>( |
32 | &'a self, | 32 | &'a self, |
33 | db: &'a DB, | 33 | db: &'a dyn HirDatabase, |
34 | max_size: Option<usize>, | 34 | max_size: Option<usize>, |
35 | ) -> HirDisplayWrapper<'a, DB, Self> | 35 | ) -> HirDisplayWrapper<'a, Self> |
36 | where | 36 | where |
37 | Self: Sized, | 37 | Self: Sized, |
38 | { | 38 | { |
@@ -40,10 +40,7 @@ pub trait HirDisplay { | |||
40 | } | 40 | } |
41 | } | 41 | } |
42 | 42 | ||
43 | impl<'a, 'b, DB> HirFormatter<'a, 'b, DB> | 43 | impl<'a, 'b> HirFormatter<'a, 'b> { |
44 | where | ||
45 | DB: HirDatabase, | ||
46 | { | ||
47 | pub fn write_joined<T: HirDisplay>( | 44 | pub fn write_joined<T: HirDisplay>( |
48 | &mut self, | 45 | &mut self, |
49 | iter: impl IntoIterator<Item = T>, | 46 | iter: impl IntoIterator<Item = T>, |
@@ -84,11 +81,10 @@ where | |||
84 | } | 81 | } |
85 | } | 82 | } |
86 | 83 | ||
87 | pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>, bool); | 84 | pub struct HirDisplayWrapper<'a, T>(&'a dyn HirDatabase, &'a T, Option<usize>, bool); |
88 | 85 | ||
89 | impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> | 86 | impl<'a, T> fmt::Display for HirDisplayWrapper<'a, T> |
90 | where | 87 | where |
91 | DB: HirDatabase, | ||
92 | T: HirDisplay, | 88 | T: HirDisplay, |
93 | { | 89 | { |
94 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 90 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
@@ -106,13 +102,13 @@ where | |||
106 | const TYPE_HINT_TRUNCATION: &str = "…"; | 102 | const TYPE_HINT_TRUNCATION: &str = "…"; |
107 | 103 | ||
108 | impl HirDisplay for &Ty { | 104 | impl HirDisplay for &Ty { |
109 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { | 105 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { |
110 | HirDisplay::hir_fmt(*self, f) | 106 | HirDisplay::hir_fmt(*self, f) |
111 | } | 107 | } |
112 | } | 108 | } |
113 | 109 | ||
114 | impl HirDisplay for ApplicationTy { | 110 | impl HirDisplay for ApplicationTy { |
115 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { | 111 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { |
116 | if f.should_truncate() { | 112 | if f.should_truncate() { |
117 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | 113 | return write!(f, "{}", TYPE_HINT_TRUNCATION); |
118 | } | 114 | } |
@@ -178,7 +174,7 @@ impl HirDisplay for ApplicationTy { | |||
178 | } | 174 | } |
179 | } | 175 | } |
180 | if self.parameters.len() > 0 { | 176 | if self.parameters.len() > 0 { |
181 | let generics = generics(f.db, def.into()); | 177 | let generics = generics(f.db.upcast(), def.into()); |
182 | let (parent_params, self_param, type_params, _impl_trait_params) = | 178 | let (parent_params, self_param, type_params, _impl_trait_params) = |
183 | generics.provenance_split(); | 179 | generics.provenance_split(); |
184 | let total_len = parent_params + self_param + type_params; | 180 | let total_len = parent_params + self_param + type_params; |
@@ -238,7 +234,7 @@ impl HirDisplay for ApplicationTy { | |||
238 | } | 234 | } |
239 | } | 235 | } |
240 | TypeCtor::AssociatedType(type_alias) => { | 236 | TypeCtor::AssociatedType(type_alias) => { |
241 | let trait_ = match type_alias.lookup(f.db).container { | 237 | let trait_ = match type_alias.lookup(f.db.upcast()).container { |
242 | AssocContainerId::TraitId(it) => it, | 238 | AssocContainerId::TraitId(it) => it, |
243 | _ => panic!("not an associated type"), | 239 | _ => panic!("not an associated type"), |
244 | }; | 240 | }; |
@@ -272,7 +268,7 @@ impl HirDisplay for ApplicationTy { | |||
272 | } | 268 | } |
273 | 269 | ||
274 | impl HirDisplay for ProjectionTy { | 270 | impl HirDisplay for ProjectionTy { |
275 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { | 271 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { |
276 | if f.should_truncate() { | 272 | if f.should_truncate() { |
277 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | 273 | return write!(f, "{}", TYPE_HINT_TRUNCATION); |
278 | } | 274 | } |
@@ -290,7 +286,7 @@ impl HirDisplay for ProjectionTy { | |||
290 | } | 286 | } |
291 | 287 | ||
292 | impl HirDisplay for Ty { | 288 | impl HirDisplay for Ty { |
293 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { | 289 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { |
294 | if f.should_truncate() { | 290 | if f.should_truncate() { |
295 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | 291 | return write!(f, "{}", TYPE_HINT_TRUNCATION); |
296 | } | 292 | } |
@@ -299,7 +295,7 @@ impl HirDisplay for Ty { | |||
299 | Ty::Apply(a_ty) => a_ty.hir_fmt(f)?, | 295 | Ty::Apply(a_ty) => a_ty.hir_fmt(f)?, |
300 | Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, | 296 | Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, |
301 | Ty::Placeholder(id) => { | 297 | Ty::Placeholder(id) => { |
302 | let generics = generics(f.db, id.parent); | 298 | let generics = generics(f.db.upcast(), id.parent); |
303 | let param_data = &generics.params.types[id.local_id]; | 299 | let param_data = &generics.params.types[id.local_id]; |
304 | match param_data.provenance { | 300 | match param_data.provenance { |
305 | TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => { | 301 | TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => { |
@@ -334,7 +330,7 @@ impl HirDisplay for Ty { | |||
334 | 330 | ||
335 | fn write_bounds_like_dyn_trait( | 331 | fn write_bounds_like_dyn_trait( |
336 | predicates: &[GenericPredicate], | 332 | predicates: &[GenericPredicate], |
337 | f: &mut HirFormatter<impl HirDatabase>, | 333 | f: &mut HirFormatter, |
338 | ) -> fmt::Result { | 334 | ) -> fmt::Result { |
339 | // Note: This code is written to produce nice results (i.e. | 335 | // Note: This code is written to produce nice results (i.e. |
340 | // corresponding to surface Rust) for types that can occur in | 336 | // corresponding to surface Rust) for types that can occur in |
@@ -398,7 +394,7 @@ fn write_bounds_like_dyn_trait( | |||
398 | } | 394 | } |
399 | 395 | ||
400 | impl TraitRef { | 396 | impl TraitRef { |
401 | fn hir_fmt_ext(&self, f: &mut HirFormatter<impl HirDatabase>, use_as: bool) -> fmt::Result { | 397 | fn hir_fmt_ext(&self, f: &mut HirFormatter, use_as: bool) -> fmt::Result { |
402 | if f.should_truncate() { | 398 | if f.should_truncate() { |
403 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | 399 | return write!(f, "{}", TYPE_HINT_TRUNCATION); |
404 | } | 400 | } |
@@ -420,19 +416,19 @@ impl TraitRef { | |||
420 | } | 416 | } |
421 | 417 | ||
422 | impl HirDisplay for TraitRef { | 418 | impl HirDisplay for TraitRef { |
423 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { | 419 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { |
424 | self.hir_fmt_ext(f, false) | 420 | self.hir_fmt_ext(f, false) |
425 | } | 421 | } |
426 | } | 422 | } |
427 | 423 | ||
428 | impl HirDisplay for &GenericPredicate { | 424 | impl HirDisplay for &GenericPredicate { |
429 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { | 425 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { |
430 | HirDisplay::hir_fmt(*self, f) | 426 | HirDisplay::hir_fmt(*self, f) |
431 | } | 427 | } |
432 | } | 428 | } |
433 | 429 | ||
434 | impl HirDisplay for GenericPredicate { | 430 | impl HirDisplay for GenericPredicate { |
435 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { | 431 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { |
436 | if f.should_truncate() { | 432 | if f.should_truncate() { |
437 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | 433 | return write!(f, "{}", TYPE_HINT_TRUNCATION); |
438 | } | 434 | } |
@@ -456,7 +452,7 @@ impl HirDisplay for GenericPredicate { | |||
456 | } | 452 | } |
457 | 453 | ||
458 | impl HirDisplay for Obligation { | 454 | impl HirDisplay for Obligation { |
459 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { | 455 | fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { |
460 | match self { | 456 | match self { |
461 | Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db)), | 457 | Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db)), |
462 | Obligation::Projection(proj) => write!( | 458 | Obligation::Projection(proj) => write!( |