Skip to content

Commit 07079a7

Browse files
committed
Auto merge of #52438 - ljedrz:rustc_vec_capacity, r=<try>
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 31263f3 + 0bb54a1 commit 07079a7

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

src/librustc/infer/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,16 @@ 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();
565+
let unsolved_type_variables = self.type_variables.borrow_mut().unsolved_variables();
566+
let mut variables = Vec::with_capacity(
567+
unsolved_type_variables.len() +
568+
self.int_unification_table.borrow().len() +
569+
self.float_unification_table.borrow().len()
570+
);
566571

567572
{
568-
let mut type_variables = self.type_variables.borrow_mut();
569573
variables.extend(
570-
type_variables
571-
.unsolved_variables()
574+
unsolved_type_variables
572575
.into_iter()
573576
.map(|t| self.tcx.mk_var(t)));
574577
}

src/librustc/middle/dead.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,12 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
396396
fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
397397
access_levels: &privacy::AccessLevels,
398398
krate: &hir::Crate)
399-
-> Vec<ast::NodeId> {
400-
let mut worklist = Vec::new();
399+
-> Vec<ast::NodeId>
400+
{
401+
let mut worklist = Vec::with_capacity(access_levels.map.len() +
402+
if tcx.sess.entry_fn.borrow().is_some() { 1 } else { 0 }
403+
);
404+
401405
for (id, _) in &access_levels.map {
402406
worklist.push(*id);
403407
}

src/librustc_codegen_llvm/abi.rs

Lines changed: 7 additions & 1 deletion
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 = self.args.iter().fold(0, |acc, a| acc +
569+
if arg.pad.is_some() { 1 } else { 0 } +
570+
if let PassMode::Pair(_) = arg.mode { 2 } else { 1 }
571+
);
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_driver/driver.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,8 +1353,18 @@ fn generated_output_paths(
13531353
exact_name: bool,
13541354
crate_name: &str,
13551355
) -> Vec<PathBuf> {
1356-
let mut out_filenames = Vec::new();
1357-
for output_type in sess.opts.output_types.keys() {
1356+
let output_types = sess.opts.output_types.keys();
1357+
let mut out_filenames = Vec::with_capacity(
1358+
output_types.iter().fold(0, |acc, ot| acc +
1359+
if ot == OutputType::DepInfo && sess.opts.debugging_opts.dep_info_omit_d_target {
1360+
0
1361+
} else {
1362+
1
1363+
}
1364+
)
1365+
);
1366+
1367+
for output_type in output_types {
13581368
let file = outputs.path(*output_type);
13591369
match *output_type {
13601370
// If the filename has been overridden using `-o`, it will not be modified

src/librustc_typeck/astconv.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,12 @@ impl<'a, 'gcx, 'tcx> Bounds<'tcx> {
14641464
pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, param_ty: Ty<'tcx>)
14651465
-> Vec<ty::Predicate<'tcx>>
14661466
{
1467-
let mut vec = Vec::new();
1467+
let mut vec = Vec::with_capacity(
1468+
if self.implicitly_sized && tcx.lang_items().sized_trait().is_some() { 1 } else { 0 } +
1469+
self.region_bounds.len() +
1470+
self.trait_bounds.len() +
1471+
self.projection_bounds.len()
1472+
);
14681473

14691474
// If it could be sized, and is, add the sized predicate
14701475
if self.implicitly_sized {

0 commit comments

Comments
 (0)