aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-10-14 14:14:18 +0100
committerGitHub <[email protected]>2019-10-14 14:14:18 +0100
commite182825170e346abb84240b84458b49b73783dca (patch)
tree899e18326f89571f835a0d9eb2f42a71f714f2df /crates
parent1f4fbc0035a6a9ee1b599a3aa9f236989633a9f7 (diff)
parent3a55b5bf01ddc581a3f00fa56db725db93a131c6 (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')
-rw-r--r--crates/ra_hir/src/lib.rs1
-rw-r--r--crates/ra_hir/src/ty.rs24
-rw-r--r--crates/ra_hir/src/ty/infer/unify.rs19
-rw-r--r--crates/ra_hir/src/ty/lower.rs38
-rw-r--r--crates/ra_hir/src/ty/traits.rs2
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs3
-rw-r--r--crates/ra_hir/src/util.rs12
7 files changed, 49 insertions, 50 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 9cbd9a8ae..ca261e8f5 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -51,6 +51,7 @@ mod lang_item;
51mod generics; 51mod generics;
52mod resolve; 52mod resolve;
53pub mod diagnostics; 53pub mod diagnostics;
54mod util;
54 55
55mod code_model; 56mod code_model;
56 57
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index d161735e8..cc9746f6d 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -17,8 +17,8 @@ use std::sync::Arc;
17use std::{fmt, iter, mem}; 17use std::{fmt, iter, mem};
18 18
19use crate::{ 19use crate::{
20 db::HirDatabase, expr::ExprId, type_ref::Mutability, Adt, Crate, DefWithBody, GenericParams, 20 db::HirDatabase, expr::ExprId, type_ref::Mutability, util::make_mut_slice, Adt, Crate,
21 HasGenericParams, Name, Trait, TypeAlias, 21 DefWithBody, GenericParams, HasGenericParams, Name, Trait, TypeAlias,
22}; 22};
23use display::{HirDisplay, HirFormatter}; 23use display::{HirDisplay, HirFormatter};
24 24
@@ -308,12 +308,9 @@ impl Substs {
308 } 308 }
309 309
310 pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { 310 pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
311 // Without an Arc::make_mut_slice, we can't avoid the clone here: 311 for t in make_mut_slice(&mut self.0) {
312 let mut v: Vec<_> = self.0.iter().cloned().collect();
313 for t in &mut v {
314 t.walk_mut(f); 312 t.walk_mut(f);
315 } 313 }
316 self.0 = v.into();
317 } 314 }
318 315
319 pub fn as_single(&self) -> &Ty { 316 pub fn as_single(&self) -> &Ty {
@@ -330,8 +327,7 @@ impl Substs {
330 .params_including_parent() 327 .params_including_parent()
331 .into_iter() 328 .into_iter()
332 .map(|p| Ty::Param { idx: p.idx, name: p.name.clone() }) 329 .map(|p| Ty::Param { idx: p.idx, name: p.name.clone() })
333 .collect::<Vec<_>>() 330 .collect(),
334 .into(),
335 ) 331 )
336 } 332 }
337 333
@@ -342,8 +338,7 @@ impl Substs {
342 .params_including_parent() 338 .params_including_parent()
343 .into_iter() 339 .into_iter()
344 .map(|p| Ty::Bound(p.idx)) 340 .map(|p| Ty::Bound(p.idx))
345 .collect::<Vec<_>>() 341 .collect(),
346 .into(),
347 ) 342 )
348 } 343 }
349 344
@@ -541,12 +536,9 @@ impl TypeWalk for FnSig {
541 } 536 }
542 537
543 fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { 538 fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
544 // Without an Arc::make_mut_slice, we can't avoid the clone here: 539 for t in make_mut_slice(&mut self.params_and_return) {
545 let mut v: Vec<_> = self.params_and_return.iter().cloned().collect();
546 for t in &mut v {
547 t.walk_mut(f); 540 t.walk_mut(f);
548 } 541 }
549 self.params_and_return = v.into();
550 } 542 }
551} 543}
552 544
@@ -756,11 +748,9 @@ impl TypeWalk for Ty {
756 p_ty.parameters.walk_mut(f); 748 p_ty.parameters.walk_mut(f);
757 } 749 }
758 Ty::Dyn(predicates) | Ty::Opaque(predicates) => { 750 Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
759 let mut v: Vec<_> = predicates.iter().cloned().collect(); 751 for p in make_mut_slice(predicates) {
760 for p in &mut v {
761 p.walk_mut(f); 752 p.walk_mut(f);
762 } 753 }
763 *predicates = v.into();
764 } 754 }
765 Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {} 755 Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {}
766 } 756 }
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};
9use crate::util::make_mut_slice;
9 10
10impl<'a, D: HirDatabase> InferenceContext<'a, D> { 11impl<'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
570pub(crate) fn trait_env( 565pub(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
611fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { 605fn 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}
diff --git a/crates/ra_hir/src/util.rs b/crates/ra_hir/src/util.rs
new file mode 100644
index 000000000..0095ee45d
--- /dev/null
+++ b/crates/ra_hir/src/util.rs
@@ -0,0 +1,12 @@
1//! Internal utility functions.
2
3use std::sync::Arc;
4
5/// Helper for mutating `Arc<[T]>` (i.e. `Arc::make_mut` for Arc slices).
6/// The underlying values are cloned if there are other strong references.
7pub(crate) fn make_mut_slice<T: Clone>(a: &mut Arc<[T]>) -> &mut [T] {
8 if Arc::get_mut(a).is_none() {
9 *a = a.iter().cloned().collect();
10 }
11 Arc::get_mut(a).unwrap()
12}