Avoid cloning Arc<[T]>
into a vec if possible
This commit is contained in:
parent
77f2dd96a1
commit
f8d4cdc170
@ -51,6 +51,7 @@ fn from(it: $sv) -> $e {
|
||||
mod generics;
|
||||
mod resolve;
|
||||
pub mod diagnostics;
|
||||
mod util;
|
||||
|
||||
mod code_model;
|
||||
|
||||
|
@ -17,8 +17,8 @@
|
||||
use std::{fmt, iter, mem};
|
||||
|
||||
use crate::{
|
||||
db::HirDatabase, expr::ExprId, type_ref::Mutability, Adt, Crate, DefWithBody, GenericParams,
|
||||
HasGenericParams, Name, Trait, TypeAlias,
|
||||
db::HirDatabase, expr::ExprId, type_ref::Mutability, util::make_mut_arc_slice, Adt, Crate,
|
||||
DefWithBody, GenericParams, HasGenericParams, Name, Trait, TypeAlias,
|
||||
};
|
||||
use display::{HirDisplay, HirFormatter};
|
||||
|
||||
@ -308,12 +308,11 @@ pub fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
||||
}
|
||||
|
||||
pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
||||
// Without an Arc::make_mut_slice, we can't avoid the clone here:
|
||||
let mut v: Vec<_> = self.0.iter().cloned().collect();
|
||||
for t in &mut v {
|
||||
t.walk_mut(f);
|
||||
}
|
||||
self.0 = v.into();
|
||||
make_mut_arc_slice(&mut self.0, |s| {
|
||||
for t in s {
|
||||
t.walk_mut(f);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn as_single(&self) -> &Ty {
|
||||
@ -541,12 +540,11 @@ fn walk(&self, f: &mut impl FnMut(&Ty)) {
|
||||
}
|
||||
|
||||
fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
||||
// Without an Arc::make_mut_slice, we can't avoid the clone here:
|
||||
let mut v: Vec<_> = self.params_and_return.iter().cloned().collect();
|
||||
for t in &mut v {
|
||||
t.walk_mut(f);
|
||||
}
|
||||
self.params_and_return = v.into();
|
||||
make_mut_arc_slice(&mut self.params_and_return, |s| {
|
||||
for t in s {
|
||||
t.walk_mut(f);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -756,11 +754,11 @@ fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
||||
p_ty.parameters.walk_mut(f);
|
||||
}
|
||||
Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
|
||||
let mut v: Vec<_> = predicates.iter().cloned().collect();
|
||||
for p in &mut v {
|
||||
p.walk_mut(f);
|
||||
}
|
||||
*predicates = v.into();
|
||||
make_mut_arc_slice(predicates, |s| {
|
||||
for p in s {
|
||||
p.walk_mut(f);
|
||||
}
|
||||
});
|
||||
}
|
||||
Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
|
||||
TypeWalk,
|
||||
};
|
||||
use crate::util::make_mut_arc_slice;
|
||||
|
||||
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||
pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D>
|
||||
@ -74,10 +75,13 @@ fn do_canonicalize_ty(&mut self, ty: Ty) -> Ty {
|
||||
})
|
||||
}
|
||||
|
||||
fn do_canonicalize_trait_ref(&mut self, trait_ref: TraitRef) -> TraitRef {
|
||||
let substs =
|
||||
trait_ref.substs.iter().map(|ty| self.do_canonicalize_ty(ty.clone())).collect();
|
||||
TraitRef { trait_: trait_ref.trait_, substs: Substs(substs) }
|
||||
fn do_canonicalize_trait_ref(&mut self, mut trait_ref: TraitRef) -> TraitRef {
|
||||
make_mut_arc_slice(&mut trait_ref.substs.0, |tys| {
|
||||
for ty in tys {
|
||||
*ty = self.do_canonicalize_ty(ty.clone());
|
||||
}
|
||||
});
|
||||
trait_ref
|
||||
}
|
||||
|
||||
fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> {
|
||||
@ -87,10 +91,13 @@ fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> {
|
||||
}
|
||||
}
|
||||
|
||||
fn do_canonicalize_projection_ty(&mut self, projection_ty: ProjectionTy) -> ProjectionTy {
|
||||
let params =
|
||||
projection_ty.parameters.iter().map(|ty| self.do_canonicalize_ty(ty.clone())).collect();
|
||||
ProjectionTy { associated_ty: projection_ty.associated_ty, parameters: Substs(params) }
|
||||
fn do_canonicalize_projection_ty(&mut self, mut projection_ty: ProjectionTy) -> ProjectionTy {
|
||||
make_mut_arc_slice(&mut projection_ty.parameters.0, |params| {
|
||||
for ty in params {
|
||||
*ty = self.do_canonicalize_ty(ty.clone());
|
||||
}
|
||||
});
|
||||
projection_ty
|
||||
}
|
||||
|
||||
fn do_canonicalize_projection_predicate(
|
||||
|
@ -392,10 +392,9 @@ pub(super) fn from_resolved_path(
|
||||
) -> Self {
|
||||
let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved);
|
||||
if let Some(self_ty) = explicit_self_ty {
|
||||
// FIXME this could be nicer
|
||||
let mut substs_vec = substs.0.to_vec();
|
||||
substs_vec[0] = self_ty;
|
||||
substs.0 = substs_vec.into();
|
||||
crate::util::make_mut_arc_slice(&mut substs.0, |substs| {
|
||||
substs[0] = self_ty;
|
||||
});
|
||||
}
|
||||
TraitRef { trait_: resolved, substs }
|
||||
}
|
||||
|
19
crates/ra_hir/src/util.rs
Normal file
19
crates/ra_hir/src/util.rs
Normal file
@ -0,0 +1,19 @@
|
||||
//! Internal utility functions.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Helper for mutating `Arc<[T]>` (i.e. `Arc::make_mut` for Arc slices).
|
||||
/// The underlying values are cloned if there are other strong references.
|
||||
pub(crate) fn make_mut_arc_slice<T: Clone, R>(
|
||||
a: &mut Arc<[T]>,
|
||||
f: impl FnOnce(&mut [T]) -> R,
|
||||
) -> R {
|
||||
if let Some(s) = Arc::get_mut(a) {
|
||||
f(s)
|
||||
} else {
|
||||
let mut v = a.to_vec();
|
||||
let r = f(&mut v);
|
||||
*a = Arc::from(v);
|
||||
r
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user