diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-14 14:14:18 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-14 14:14:18 +0100 |
commit | e182825170e346abb84240b84458b49b73783dca (patch) | |
tree | 899e18326f89571f835a0d9eb2f42a71f714f2df /crates/ra_hir/src/ty | |
parent | 1f4fbc0035a6a9ee1b599a3aa9f236989633a9f7 (diff) | |
parent | 3a55b5bf01ddc581a3f00fa56db725db93a131c6 (diff) |
Merge #2006
2006: Improvements around `Arc<[T]>` r=matklad a=sinkuu
First commit tries to avoid cloning `Arc<[T]>` to a temporary `Vec` for mutating it, if there are no other strong references. Second commit utilizes [`FromIterator for Arc<[T]>`](https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-FromIterator%3CT%3E) instead of `.collect::<Vec<_>>().into()` to avoid allocation in `From<Vec<T>> for Arc<[T]>`.
Co-authored-by: Shotaro Yamada <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/infer/unify.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 38 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/traits.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/traits/chalk.rs | 3 |
4 files changed, 29 insertions, 33 deletions
diff --git a/crates/ra_hir/src/ty/infer/unify.rs b/crates/ra_hir/src/ty/infer/unify.rs index d161aa6b3..014c7981f 100644 --- a/crates/ra_hir/src/ty/infer/unify.rs +++ b/crates/ra_hir/src/ty/infer/unify.rs | |||
@@ -6,6 +6,7 @@ use crate::ty::{ | |||
6 | Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, | 6 | Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, |
7 | TypeWalk, | 7 | TypeWalk, |
8 | }; | 8 | }; |
9 | use crate::util::make_mut_slice; | ||
9 | 10 | ||
10 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { | 11 | impl<'a, D: HirDatabase> InferenceContext<'a, D> { |
11 | pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D> | 12 | pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D> |
@@ -74,10 +75,11 @@ where | |||
74 | }) | 75 | }) |
75 | } | 76 | } |
76 | 77 | ||
77 | fn do_canonicalize_trait_ref(&mut self, trait_ref: TraitRef) -> TraitRef { | 78 | fn do_canonicalize_trait_ref(&mut self, mut trait_ref: TraitRef) -> TraitRef { |
78 | let substs = | 79 | for ty in make_mut_slice(&mut trait_ref.substs.0) { |
79 | trait_ref.substs.iter().map(|ty| self.do_canonicalize_ty(ty.clone())).collect(); | 80 | *ty = self.do_canonicalize_ty(ty.clone()); |
80 | TraitRef { trait_: trait_ref.trait_, substs: Substs(substs) } | 81 | } |
82 | trait_ref | ||
81 | } | 83 | } |
82 | 84 | ||
83 | fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> { | 85 | fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> { |
@@ -87,10 +89,11 @@ where | |||
87 | } | 89 | } |
88 | } | 90 | } |
89 | 91 | ||
90 | fn do_canonicalize_projection_ty(&mut self, projection_ty: ProjectionTy) -> ProjectionTy { | 92 | fn do_canonicalize_projection_ty(&mut self, mut projection_ty: ProjectionTy) -> ProjectionTy { |
91 | let params = | 93 | for ty in make_mut_slice(&mut projection_ty.parameters.0) { |
92 | projection_ty.parameters.iter().map(|ty| self.do_canonicalize_ty(ty.clone())).collect(); | 94 | *ty = self.do_canonicalize_ty(ty.clone()); |
93 | ProjectionTy { associated_ty: projection_ty.associated_ty, parameters: Substs(params) } | 95 | } |
96 | projection_ty | ||
94 | } | 97 | } |
95 | 98 | ||
96 | fn do_canonicalize_projection_predicate( | 99 | fn do_canonicalize_projection_predicate( |
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index a604c02e2..366556134 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -22,6 +22,7 @@ use crate::{ | |||
22 | resolve::{Resolver, TypeNs}, | 22 | resolve::{Resolver, TypeNs}, |
23 | ty::Adt, | 23 | ty::Adt, |
24 | type_ref::{TypeBound, TypeRef}, | 24 | type_ref::{TypeBound, TypeRef}, |
25 | util::make_mut_slice, | ||
25 | BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, | 26 | BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, |
26 | Trait, TypeAlias, Union, | 27 | Trait, TypeAlias, Union, |
27 | }; | 28 | }; |
@@ -31,11 +32,11 @@ impl Ty { | |||
31 | match type_ref { | 32 | match type_ref { |
32 | TypeRef::Never => Ty::simple(TypeCtor::Never), | 33 | TypeRef::Never => Ty::simple(TypeCtor::Never), |
33 | TypeRef::Tuple(inner) => { | 34 | TypeRef::Tuple(inner) => { |
34 | let inner_tys = | 35 | let inner_tys: Arc<[Ty]> = |
35 | inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); | 36 | inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect(); |
36 | Ty::apply( | 37 | Ty::apply( |
37 | TypeCtor::Tuple { cardinality: inner_tys.len() as u16 }, | 38 | TypeCtor::Tuple { cardinality: inner_tys.len() as u16 }, |
38 | Substs(inner_tys.into()), | 39 | Substs(inner_tys), |
39 | ) | 40 | ) |
40 | } | 41 | } |
41 | TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path), | 42 | TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path), |
@@ -57,9 +58,7 @@ impl Ty { | |||
57 | } | 58 | } |
58 | TypeRef::Placeholder => Ty::Unknown, | 59 | TypeRef::Placeholder => Ty::Unknown, |
59 | TypeRef::Fn(params) => { | 60 | TypeRef::Fn(params) => { |
60 | let inner_tys = | 61 | let sig = Substs(params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect()); |
61 | params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); | ||
62 | let sig = Substs(inner_tys.into()); | ||
63 | Ty::apply(TypeCtor::FnPtr { num_args: sig.len() as u16 - 1 }, sig) | 62 | Ty::apply(TypeCtor::FnPtr { num_args: sig.len() as u16 - 1 }, sig) |
64 | } | 63 | } |
65 | TypeRef::DynTrait(bounds) => { | 64 | TypeRef::DynTrait(bounds) => { |
@@ -69,8 +68,8 @@ impl Ty { | |||
69 | .flat_map(|b| { | 68 | .flat_map(|b| { |
70 | GenericPredicate::from_type_bound(db, resolver, b, self_ty.clone()) | 69 | GenericPredicate::from_type_bound(db, resolver, b, self_ty.clone()) |
71 | }) | 70 | }) |
72 | .collect::<Vec<_>>(); | 71 | .collect(); |
73 | Ty::Dyn(predicates.into()) | 72 | Ty::Dyn(predicates) |
74 | } | 73 | } |
75 | TypeRef::ImplTrait(bounds) => { | 74 | TypeRef::ImplTrait(bounds) => { |
76 | let self_ty = Ty::Bound(0); | 75 | let self_ty = Ty::Bound(0); |
@@ -79,8 +78,8 @@ impl Ty { | |||
79 | .flat_map(|b| { | 78 | .flat_map(|b| { |
80 | GenericPredicate::from_type_bound(db, resolver, b, self_ty.clone()) | 79 | GenericPredicate::from_type_bound(db, resolver, b, self_ty.clone()) |
81 | }) | 80 | }) |
82 | .collect::<Vec<_>>(); | 81 | .collect(); |
83 | Ty::Opaque(predicates.into()) | 82 | Ty::Opaque(predicates) |
84 | } | 83 | } |
85 | TypeRef::Error => Ty::Unknown, | 84 | TypeRef::Error => Ty::Unknown, |
86 | } | 85 | } |
@@ -392,10 +391,7 @@ impl TraitRef { | |||
392 | ) -> Self { | 391 | ) -> Self { |
393 | let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved); | 392 | let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved); |
394 | if let Some(self_ty) = explicit_self_ty { | 393 | if let Some(self_ty) = explicit_self_ty { |
395 | // FIXME this could be nicer | 394 | make_mut_slice(&mut substs.0)[0] = self_ty; |
396 | let mut substs_vec = substs.0.to_vec(); | ||
397 | substs_vec[0] = self_ty; | ||
398 | substs.0 = substs_vec.into(); | ||
399 | } | 395 | } |
400 | TraitRef { trait_: resolved, substs } | 396 | TraitRef { trait_: resolved, substs } |
401 | } | 397 | } |
@@ -558,13 +554,12 @@ pub(crate) fn generic_predicates_for_param_query( | |||
558 | param_idx: u32, | 554 | param_idx: u32, |
559 | ) -> Arc<[GenericPredicate]> { | 555 | ) -> Arc<[GenericPredicate]> { |
560 | let resolver = def.resolver(db); | 556 | let resolver = def.resolver(db); |
561 | let predicates = resolver | 557 | resolver |
562 | .where_predicates_in_scope() | 558 | .where_predicates_in_scope() |
563 | // we have to filter out all other predicates *first*, before attempting to lower them | 559 | // we have to filter out all other predicates *first*, before attempting to lower them |
564 | .filter(|pred| Ty::from_hir_only_param(db, &resolver, &pred.type_ref) == Some(param_idx)) | 560 | .filter(|pred| Ty::from_hir_only_param(db, &resolver, &pred.type_ref) == Some(param_idx)) |
565 | .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) | 561 | .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) |
566 | .collect::<Vec<_>>(); | 562 | .collect() |
567 | predicates.into() | ||
568 | } | 563 | } |
569 | 564 | ||
570 | pub(crate) fn trait_env( | 565 | pub(crate) fn trait_env( |
@@ -585,11 +580,10 @@ pub(crate) fn generic_predicates_query( | |||
585 | def: GenericDef, | 580 | def: GenericDef, |
586 | ) -> Arc<[GenericPredicate]> { | 581 | ) -> Arc<[GenericPredicate]> { |
587 | let resolver = def.resolver(db); | 582 | let resolver = def.resolver(db); |
588 | let predicates = resolver | 583 | resolver |
589 | .where_predicates_in_scope() | 584 | .where_predicates_in_scope() |
590 | .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) | 585 | .flat_map(|pred| GenericPredicate::from_where_predicate(db, &resolver, pred)) |
591 | .collect::<Vec<_>>(); | 586 | .collect() |
592 | predicates.into() | ||
593 | } | 587 | } |
594 | 588 | ||
595 | /// Resolve the default type params from generics | 589 | /// Resolve the default type params from generics |
@@ -603,9 +597,9 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> | |||
603 | .map(|p| { | 597 | .map(|p| { |
604 | p.default.as_ref().map_or(Ty::Unknown, |path| Ty::from_hir_path(db, &resolver, path)) | 598 | p.default.as_ref().map_or(Ty::Unknown, |path| Ty::from_hir_path(db, &resolver, path)) |
605 | }) | 599 | }) |
606 | .collect::<Vec<_>>(); | 600 | .collect(); |
607 | 601 | ||
608 | Substs(defaults.into()) | 602 | Substs(defaults) |
609 | } | 603 | } |
610 | 604 | ||
611 | fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { | 605 | fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { |
diff --git a/crates/ra_hir/src/ty/traits.rs b/crates/ra_hir/src/ty/traits.rs index b0f67ae50..0cb5c3798 100644 --- a/crates/ra_hir/src/ty/traits.rs +++ b/crates/ra_hir/src/ty/traits.rs | |||
@@ -89,7 +89,7 @@ pub(crate) fn impls_for_trait_query( | |||
89 | } | 89 | } |
90 | let crate_impl_blocks = db.impls_in_crate(krate); | 90 | let crate_impl_blocks = db.impls_in_crate(krate); |
91 | impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_)); | 91 | impls.extend(crate_impl_blocks.lookup_impl_blocks_for_trait(trait_)); |
92 | impls.into_iter().collect::<Vec<_>>().into() | 92 | impls.into_iter().collect() |
93 | } | 93 | } |
94 | 94 | ||
95 | /// A set of clauses that we assume to be true. E.g. if we are inside this function: | 95 | /// A set of clauses that we assume to be true. E.g. if we are inside this function: |
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 9168de709..00aaf65d9 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs | |||
@@ -126,8 +126,7 @@ impl ToChalk for Substs { | |||
126 | chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty), | 126 | chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty), |
127 | chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(), | 127 | chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(), |
128 | }) | 128 | }) |
129 | .collect::<Vec<_>>() | 129 | .collect(); |
130 | .into(); | ||
131 | Substs(tys) | 130 | Substs(tys) |
132 | } | 131 | } |
133 | } | 132 | } |