Skip to content

Commit 53c1e6f

Browse files
authored
Rollup merge of rust-lang#68409 - sinkuu:temp_path, r=Mark-Simulacrum
Micro-optimize OutputFilenames For example, its methods consume 6% of time during debug-compiling a `warp` example: ![Screenshot (debug-compiling a `warp` example)](https://user-images.githubusercontent.com/7091080/72780288-d74f1580-3c61-11ea-953b-34e59ca682f9.png) This PR optimize them a bit by using `PathBuf::set_extension` instead of `Path::with_extension`, to avoid cloning `PathBuf` excessively.
2 parents 917e683 + dc97181 commit 53c1e6f

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

Diff for: src/librustc_interface/util.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,13 @@ pub fn build_output_filenames(
551551
.or_else(|| rustc_attr::find_crate_name(attrs).map(|n| n.to_string()))
552552
.unwrap_or_else(|| input.filestem().to_owned());
553553

554-
OutputFilenames {
555-
out_directory: dirpath,
556-
out_filestem: stem,
557-
single_output_file: None,
558-
extra: sess.opts.cg.extra_filename.clone(),
559-
outputs: sess.opts.output_types.clone(),
560-
}
554+
OutputFilenames::new(
555+
dirpath,
556+
stem,
557+
None,
558+
sess.opts.cg.extra_filename.clone(),
559+
sess.opts.output_types.clone(),
560+
)
561561
}
562562

563563
Some(ref out_file) => {
@@ -579,18 +579,13 @@ pub fn build_output_filenames(
579579
sess.warn("ignoring --out-dir flag due to -o flag");
580580
}
581581

582-
OutputFilenames {
583-
out_directory: out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
584-
out_filestem: out_file
585-
.file_stem()
586-
.unwrap_or_default()
587-
.to_str()
588-
.unwrap()
589-
.to_string(),
590-
single_output_file: ofile,
591-
extra: sess.opts.cg.extra_filename.clone(),
592-
outputs: sess.opts.output_types.clone(),
593-
}
582+
OutputFilenames::new(
583+
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
584+
out_file.file_stem().unwrap_or_default().to_str().unwrap().to_string(),
585+
ofile,
586+
sess.opts.cg.extra_filename.clone(),
587+
sess.opts.output_types.clone(),
588+
)
594589
}
595590
}
596591
}

Diff for: src/librustc_session/config.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,8 @@ impl Input {
447447
#[derive(Clone, Hash)]
448448
pub struct OutputFilenames {
449449
pub out_directory: PathBuf,
450-
pub out_filestem: String,
450+
filestem: String,
451451
pub single_output_file: Option<PathBuf>,
452-
pub extra: String,
453452
pub outputs: OutputTypes,
454453
}
455454

@@ -458,6 +457,21 @@ impl_stable_hash_via_hash!(OutputFilenames);
458457
pub const RUST_CGU_EXT: &str = "rcgu";
459458

460459
impl OutputFilenames {
460+
pub fn new(
461+
out_directory: PathBuf,
462+
out_filestem: String,
463+
single_output_file: Option<PathBuf>,
464+
extra: String,
465+
outputs: OutputTypes,
466+
) -> Self {
467+
OutputFilenames {
468+
out_directory,
469+
single_output_file,
470+
outputs,
471+
filestem: format!("{}{}", out_filestem, extra),
472+
}
473+
}
474+
461475
pub fn path(&self, flavor: OutputType) -> PathBuf {
462476
self.outputs
463477
.get(&flavor)
@@ -477,8 +491,6 @@ impl OutputFilenames {
477491
/// Like temp_path, but also supports things where there is no corresponding
478492
/// OutputType, like noopt-bitcode or lto-bitcode.
479493
pub fn temp_path_ext(&self, ext: &str, codegen_unit_name: Option<&str>) -> PathBuf {
480-
let base = self.out_directory.join(&self.filestem());
481-
482494
let mut extension = String::new();
483495

484496
if let Some(codegen_unit_name) = codegen_unit_name {
@@ -495,16 +507,13 @@ impl OutputFilenames {
495507
extension.push_str(ext);
496508
}
497509

498-
let path = base.with_extension(&extension[..]);
499-
path
510+
self.with_extension(&extension)
500511
}
501512

502513
pub fn with_extension(&self, extension: &str) -> PathBuf {
503-
self.out_directory.join(&self.filestem()).with_extension(extension)
504-
}
505-
506-
pub fn filestem(&self) -> String {
507-
format!("{}{}", self.out_filestem, self.extra)
514+
let mut path = self.out_directory.join(&self.filestem);
515+
path.set_extension(extension);
516+
path
508517
}
509518
}
510519

0 commit comments

Comments
 (0)