Skip to content

Commit 9e1c577

Browse files
rustdoc: don't create directories more than once
1 parent 1db1144 commit 9e1c577

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/librustdoc/html/render.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ pub struct SharedContext {
125125
/// Warnings for the user if rendering would differ using different markdown
126126
/// parsers.
127127
pub markdown_warnings: RefCell<Vec<(Span, String, Vec<html_diff::Difference>)>>,
128+
/// The directories that have already been created in this doc run. Used to reduce the number
129+
/// of spurious `create_dir_all` calls.
130+
pub created_dirs: RefCell<FxHashSet<PathBuf>>,
131+
}
132+
133+
impl SharedContext {
134+
fn ensure_dir(&self, dst: &Path) -> io::Result<()> {
135+
let mut dirs = self.created_dirs.borrow_mut();
136+
if !dirs.contains(dst) {
137+
fs::create_dir_all(dst)?;
138+
dirs.insert(dst.to_path_buf());
139+
}
140+
141+
Ok(())
142+
}
128143
}
129144

130145
/// Indicates where an external crate can be found.
@@ -460,6 +475,7 @@ pub fn run(mut krate: clean::Crate,
460475
},
461476
css_file_extension: css_file_extension.clone(),
462477
markdown_warnings: RefCell::new(vec![]),
478+
created_dirs: RefCell::new(FxHashSet()),
463479
};
464480

465481
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -790,7 +806,6 @@ fn write_shared(cx: &Context,
790806
// Write out the shared files. Note that these are shared among all rustdoc
791807
// docs placed in the output directory, so this needs to be a synchronized
792808
// operation with respect to all other rustdocs running around.
793-
try_err!(fs::create_dir_all(&cx.dst), &cx.dst);
794809
let _lock = flock::Lock::panicking_new(&cx.dst.join(".lock"), true, true, true);
795810

796811
// Add all the static files. These may already exist, but we just
@@ -1503,8 +1518,8 @@ impl Context {
15031518
this.render_item(&mut buf, &item, false).unwrap();
15041519
// buf will be empty if the module is stripped and there is no redirect for it
15051520
if !buf.is_empty() {
1521+
try_err!(this.shared.ensure_dir(&this.dst), &this.dst);
15061522
let joint_dst = this.dst.join("index.html");
1507-
try_err!(fs::create_dir_all(&this.dst), &this.dst);
15081523
let mut dst = try_err!(File::create(&joint_dst), &joint_dst);
15091524
try_err!(dst.write_all(&buf), &joint_dst);
15101525
}
@@ -1538,8 +1553,8 @@ impl Context {
15381553
let name = item.name.as_ref().unwrap();
15391554
let item_type = item.type_();
15401555
let file_name = &item_path(item_type, name);
1556+
try_err!(self.shared.ensure_dir(&self.dst), &self.dst);
15411557
let joint_dst = self.dst.join(file_name);
1542-
try_err!(fs::create_dir_all(&self.dst), &self.dst);
15431558
let mut dst = try_err!(File::create(&joint_dst), &joint_dst);
15441559
try_err!(dst.write_all(&buf), &joint_dst);
15451560

0 commit comments

Comments
 (0)