Skip to content

Commit 6195749

Browse files
committed
Prevent re-allocation in CallableSig::from_params_and_return
1 parent 54f3f56 commit 6195749

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,16 @@ pub type PolyFnSig = Binders<CallableSig>;
513513

514514
impl CallableSig {
515515
pub fn from_params_and_return(
516-
mut params: Vec<Ty>,
516+
params: impl ExactSizeIterator<Item = Ty>,
517517
ret: Ty,
518518
is_varargs: bool,
519519
safety: Safety,
520520
abi: FnAbi,
521521
) -> CallableSig {
522-
params.push(ret);
523-
CallableSig { params_and_return: params.into(), is_varargs, safety, abi }
522+
let mut params_and_return = Vec::with_capacity(params.len() + 1);
523+
params_and_return.extend(params);
524+
params_and_return.push(ret);
525+
CallableSig { params_and_return: params_and_return.into(), is_varargs, safety, abi }
524526
}
525527

526528
pub fn from_def(db: &dyn HirDatabase, def: FnDefId, substs: &Substitution) -> CallableSig {
@@ -935,8 +937,7 @@ pub fn callable_sig_from_fn_trait(
935937
.as_tuple()?
936938
.iter(Interner)
937939
.map(|it| it.assert_ty_ref(Interner))
938-
.cloned()
939-
.collect();
940+
.cloned();
940941

941942
return Some((
942943
fn_x,

src/tools/rust-analyzer/crates/hir-ty/src/lower.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
18551855
let ctx_params = TyLoweringContext::new(db, &resolver, def.into())
18561856
.with_impl_trait_mode(ImplTraitLoweringMode::Variable)
18571857
.with_type_param_mode(ParamLoweringMode::Variable);
1858-
let params = data.params.iter().map(|tr| ctx_params.lower_ty(tr)).collect::<Vec<_>>();
1858+
let params = data.params.iter().map(|tr| ctx_params.lower_ty(tr));
18591859
let ctx_ret = TyLoweringContext::new(db, &resolver, def.into())
18601860
.with_impl_trait_mode(ImplTraitLoweringMode::Opaque)
18611861
.with_type_param_mode(ParamLoweringMode::Variable);
@@ -1909,7 +1909,7 @@ fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnS
19091909
let resolver = def.resolver(db.upcast());
19101910
let ctx = TyLoweringContext::new(db, &resolver, AdtId::from(def).into())
19111911
.with_type_param_mode(ParamLoweringMode::Variable);
1912-
let params = fields.iter().map(|(_, field)| ctx.lower_ty(&field.type_ref)).collect::<Vec<_>>();
1912+
let params = fields.iter().map(|(_, field)| ctx.lower_ty(&field.type_ref));
19131913
let (ret, binders) = type_for_adt(db, def.into()).into_value_and_skipped_binders();
19141914
Binders::new(
19151915
binders,
@@ -1941,7 +1941,7 @@ fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId)
19411941
let resolver = def.resolver(db.upcast());
19421942
let ctx = TyLoweringContext::new(db, &resolver, DefWithBodyId::VariantId(def).into())
19431943
.with_type_param_mode(ParamLoweringMode::Variable);
1944-
let params = fields.iter().map(|(_, field)| ctx.lower_ty(&field.type_ref)).collect::<Vec<_>>();
1944+
let params = fields.iter().map(|(_, field)| ctx.lower_ty(&field.type_ref));
19451945
let (ret, binders) =
19461946
type_for_adt(db, def.lookup(db.upcast()).parent.into()).into_value_and_skipped_binders();
19471947
Binders::new(

0 commit comments

Comments
 (0)