Skip to content

Commit bf7afee

Browse files
committed
Auto merge of #52438 - ljedrz:rustc_vec_capacity, r=eddyb
Calculate Vec capacities in librustc Calculate the required capacity of a few vectors in rustc based on the number of elements they are populated with.
2 parents ee8d23d + e3d14c4 commit bf7afee

File tree

4 files changed

+49
-59
lines changed

4 files changed

+49
-59
lines changed

src/librustc/infer/mod.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -562,36 +562,25 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
562562
}
563563

564564
pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
565-
let mut variables = Vec::new();
566-
567-
{
568-
let mut type_variables = self.type_variables.borrow_mut();
569-
variables.extend(
570-
type_variables
571-
.unsolved_variables()
572-
.into_iter()
573-
.map(|t| self.tcx.mk_var(t)));
574-
}
575-
576-
{
577-
let mut int_unification_table = self.int_unification_table.borrow_mut();
578-
variables.extend(
565+
let mut type_variables = self.type_variables.borrow_mut();
566+
let mut int_unification_table = self.int_unification_table.borrow_mut();
567+
let mut float_unification_table = self.float_unification_table.borrow_mut();
568+
569+
type_variables
570+
.unsolved_variables()
571+
.into_iter()
572+
.map(|t| self.tcx.mk_var(t))
573+
.chain(
579574
(0..int_unification_table.len())
580575
.map(|i| ty::IntVid { index: i as u32 })
581576
.filter(|&vid| int_unification_table.probe_value(vid).is_none())
582-
.map(|v| self.tcx.mk_int_var(v)));
583-
}
584-
585-
{
586-
let mut float_unification_table = self.float_unification_table.borrow_mut();
587-
variables.extend(
577+
.map(|v| self.tcx.mk_int_var(v))
578+
).chain(
588579
(0..float_unification_table.len())
589580
.map(|i| ty::FloatVid { index: i as u32 })
590581
.filter(|&vid| float_unification_table.probe_value(vid).is_none())
591-
.map(|v| self.tcx.mk_float_var(v)));
592-
}
593-
594-
return variables;
582+
.map(|v| self.tcx.mk_float_var(v))
583+
).collect()
595584
}
596585

597586
fn combine_fields(&'a self, trace: TypeTrace<'tcx>, param_env: ty::ParamEnv<'tcx>)

src/librustc/middle/dead.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,12 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
391391
fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
392392
access_levels: &privacy::AccessLevels,
393393
krate: &hir::Crate)
394-
-> Vec<ast::NodeId> {
395-
let mut worklist = Vec::new();
396-
for (id, _) in &access_levels.map {
397-
worklist.push(*id);
398-
}
399-
400-
// Seed entry point
401-
if let Some((id, _, _)) = *tcx.sess.entry_fn.borrow() {
402-
worklist.push(id);
403-
}
394+
-> Vec<ast::NodeId>
395+
{
396+
let worklist = access_levels.map.iter().map(|(&id, _)| id).chain(
397+
// Seed entry point
398+
tcx.sess.entry_fn.borrow().map(|(id, _, _)| id)
399+
).collect::<Vec<_>>();
404400

405401
// Seed implemented trait items
406402
let mut life_seeder = LifeSeeder {

src/librustc_codegen_llvm/abi.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,13 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
565565
}
566566

567567
fn llvm_type(&self, cx: &CodegenCx<'a, 'tcx>) -> Type {
568-
let mut llargument_tys = Vec::new();
568+
let args_capacity: usize = self.args.iter().map(|arg|
569+
if arg.pad.is_some() { 1 } else { 0 } +
570+
if let PassMode::Pair(_, _) = arg.mode { 2 } else { 1 }
571+
).sum();
572+
let mut llargument_tys = Vec::with_capacity(
573+
if let PassMode::Indirect(_) = self.ret.mode { 1 } else { 0 } + args_capacity
574+
);
569575

570576
let llreturn_ty = match self.ret.mode {
571577
PassMode::Ignore => Type::void(cx),

src/librustc_typeck/astconv.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -1473,35 +1473,34 @@ impl<'a, 'gcx, 'tcx> Bounds<'tcx> {
14731473
pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, param_ty: Ty<'tcx>)
14741474
-> Vec<ty::Predicate<'tcx>>
14751475
{
1476-
let mut vec = Vec::new();
1477-
14781476
// If it could be sized, and is, add the sized predicate
1479-
if self.implicitly_sized {
1480-
if let Some(sized) = tcx.lang_items().sized_trait() {
1477+
let sized_predicate = if self.implicitly_sized {
1478+
tcx.lang_items().sized_trait().map(|sized| {
14811479
let trait_ref = ty::TraitRef {
14821480
def_id: sized,
14831481
substs: tcx.mk_substs_trait(param_ty, &[])
14841482
};
1485-
vec.push(trait_ref.to_predicate());
1486-
}
1487-
}
1488-
1489-
for &region_bound in &self.region_bounds {
1490-
// account for the binder being introduced below; no need to shift `param_ty`
1491-
// because, at present at least, it can only refer to early-bound regions
1492-
let region_bound = tcx.mk_region(ty::fold::shift_region(*region_bound, 1));
1493-
vec.push(
1494-
ty::Binder::dummy(ty::OutlivesPredicate(param_ty, region_bound)).to_predicate());
1495-
}
1496-
1497-
for bound_trait_ref in &self.trait_bounds {
1498-
vec.push(bound_trait_ref.to_predicate());
1499-
}
1500-
1501-
for projection in &self.projection_bounds {
1502-
vec.push(projection.to_predicate());
1503-
}
1483+
trait_ref.to_predicate()
1484+
})
1485+
} else {
1486+
None
1487+
};
15041488

1505-
vec
1489+
sized_predicate.into_iter().chain(
1490+
self.region_bounds.iter().map(|&region_bound| {
1491+
// account for the binder being introduced below; no need to shift `param_ty`
1492+
// because, at present at least, it can only refer to early-bound regions
1493+
let region_bound = tcx.mk_region(ty::fold::shift_region(*region_bound, 1));
1494+
ty::Binder::dummy(ty::OutlivesPredicate(param_ty, region_bound)).to_predicate()
1495+
}).chain(
1496+
self.trait_bounds.iter().map(|bound_trait_ref| {
1497+
bound_trait_ref.to_predicate()
1498+
})
1499+
).chain(
1500+
self.projection_bounds.iter().map(|projection| {
1501+
projection.to_predicate()
1502+
})
1503+
)
1504+
).collect()
15061505
}
15071506
}

0 commit comments

Comments
 (0)