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 generics;
|
||||||
mod resolve;
|
mod resolve;
|
||||||
pub mod diagnostics;
|
pub mod diagnostics;
|
||||||
|
mod util;
|
||||||
|
|
||||||
mod code_model;
|
mod code_model;
|
||||||
|
|
||||||
|
@ -17,8 +17,8 @@
|
|||||||
use std::{fmt, iter, mem};
|
use std::{fmt, iter, mem};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::HirDatabase, expr::ExprId, type_ref::Mutability, Adt, Crate, DefWithBody, GenericParams,
|
db::HirDatabase, expr::ExprId, type_ref::Mutability, util::make_mut_arc_slice, Adt, Crate,
|
||||||
HasGenericParams, Name, Trait, TypeAlias,
|
DefWithBody, GenericParams, HasGenericParams, Name, Trait, TypeAlias,
|
||||||
};
|
};
|
||||||
use display::{HirDisplay, HirFormatter};
|
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)) {
|
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:
|
make_mut_arc_slice(&mut self.0, |s| {
|
||||||
let mut v: Vec<_> = self.0.iter().cloned().collect();
|
for t in s {
|
||||||
for t in &mut v {
|
|
||||||
t.walk_mut(f);
|
t.walk_mut(f);
|
||||||
}
|
}
|
||||||
self.0 = v.into();
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_single(&self) -> &Ty {
|
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)) {
|
fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
||||||
// Without an Arc::make_mut_slice, we can't avoid the clone here:
|
make_mut_arc_slice(&mut self.params_and_return, |s| {
|
||||||
let mut v: Vec<_> = self.params_and_return.iter().cloned().collect();
|
for t in s {
|
||||||
for t in &mut v {
|
|
||||||
t.walk_mut(f);
|
t.walk_mut(f);
|
||||||
}
|
}
|
||||||
self.params_and_return = v.into();
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -756,11 +754,11 @@ fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) {
|
|||||||
p_ty.parameters.walk_mut(f);
|
p_ty.parameters.walk_mut(f);
|
||||||
}
|
}
|
||||||
Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
|
Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
|
||||||
let mut v: Vec<_> = predicates.iter().cloned().collect();
|
make_mut_arc_slice(predicates, |s| {
|
||||||
for p in &mut v {
|
for p in s {
|
||||||
p.walk_mut(f);
|
p.walk_mut(f);
|
||||||
}
|
}
|
||||||
*predicates = v.into();
|
});
|
||||||
}
|
}
|
||||||
Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {}
|
Ty::Param { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
|
Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
|
||||||
TypeWalk,
|
TypeWalk,
|
||||||
};
|
};
|
||||||
|
use crate::util::make_mut_arc_slice;
|
||||||
|
|
||||||
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, 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 {
|
fn do_canonicalize_trait_ref(&mut self, mut trait_ref: TraitRef) -> TraitRef {
|
||||||
let substs =
|
make_mut_arc_slice(&mut trait_ref.substs.0, |tys| {
|
||||||
trait_ref.substs.iter().map(|ty| self.do_canonicalize_ty(ty.clone())).collect();
|
for ty in tys {
|
||||||
TraitRef { trait_: trait_ref.trait_, substs: Substs(substs) }
|
*ty = self.do_canonicalize_ty(ty.clone());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
trait_ref
|
||||||
}
|
}
|
||||||
|
|
||||||
fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> {
|
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 {
|
fn do_canonicalize_projection_ty(&mut self, mut projection_ty: ProjectionTy) -> ProjectionTy {
|
||||||
let params =
|
make_mut_arc_slice(&mut projection_ty.parameters.0, |params| {
|
||||||
projection_ty.parameters.iter().map(|ty| self.do_canonicalize_ty(ty.clone())).collect();
|
for ty in params {
|
||||||
ProjectionTy { associated_ty: projection_ty.associated_ty, parameters: Substs(params) }
|
*ty = self.do_canonicalize_ty(ty.clone());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
projection_ty
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_canonicalize_projection_predicate(
|
fn do_canonicalize_projection_predicate(
|
||||||
|
@ -392,10 +392,9 @@ pub(super) fn from_resolved_path(
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved);
|
let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved);
|
||||||
if let Some(self_ty) = explicit_self_ty {
|
if let Some(self_ty) = explicit_self_ty {
|
||||||
// FIXME this could be nicer
|
crate::util::make_mut_arc_slice(&mut substs.0, |substs| {
|
||||||
let mut substs_vec = substs.0.to_vec();
|
substs[0] = self_ty;
|
||||||
substs_vec[0] = self_ty;
|
});
|
||||||
substs.0 = substs_vec.into();
|
|
||||||
}
|
}
|
||||||
TraitRef { trait_: resolved, substs }
|
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