Skip to content

Commit 5801824

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 22606de + dc97181 commit 5801824

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

src/librustc_interface/util.rs

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

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

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

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

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)