From 9e1c577b713600e6fcb46584414263993347f67e Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Sat, 23 Sep 2017 14:40:18 -0500 Subject: [PATCH 1/2] rustdoc: don't create directories more than once --- src/librustdoc/html/render.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index a3f446885f9..ad671e37e69 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -125,6 +125,21 @@ pub struct SharedContext { /// Warnings for the user if rendering would differ using different markdown /// parsers. pub markdown_warnings: RefCell)>>, + /// The directories that have already been created in this doc run. Used to reduce the number + /// of spurious `create_dir_all` calls. + pub created_dirs: RefCell>, +} + +impl SharedContext { + fn ensure_dir(&self, dst: &Path) -> io::Result<()> { + let mut dirs = self.created_dirs.borrow_mut(); + if !dirs.contains(dst) { + fs::create_dir_all(dst)?; + dirs.insert(dst.to_path_buf()); + } + + Ok(()) + } } /// Indicates where an external crate can be found. @@ -460,6 +475,7 @@ pub fn run(mut krate: clean::Crate, }, css_file_extension: css_file_extension.clone(), markdown_warnings: RefCell::new(vec![]), + created_dirs: RefCell::new(FxHashSet()), }; // If user passed in `--playground-url` arg, we fill in crate name here @@ -790,7 +806,6 @@ fn write_shared(cx: &Context, // Write out the shared files. Note that these are shared among all rustdoc // docs placed in the output directory, so this needs to be a synchronized // operation with respect to all other rustdocs running around. - try_err!(fs::create_dir_all(&cx.dst), &cx.dst); let _lock = flock::Lock::panicking_new(&cx.dst.join(".lock"), true, true, true); // Add all the static files. These may already exist, but we just @@ -1503,8 +1518,8 @@ impl Context { this.render_item(&mut buf, &item, false).unwrap(); // buf will be empty if the module is stripped and there is no redirect for it if !buf.is_empty() { + try_err!(this.shared.ensure_dir(&this.dst), &this.dst); let joint_dst = this.dst.join("index.html"); - try_err!(fs::create_dir_all(&this.dst), &this.dst); let mut dst = try_err!(File::create(&joint_dst), &joint_dst); try_err!(dst.write_all(&buf), &joint_dst); } @@ -1538,8 +1553,8 @@ impl Context { let name = item.name.as_ref().unwrap(); let item_type = item.type_(); let file_name = &item_path(item_type, name); + try_err!(self.shared.ensure_dir(&self.dst), &self.dst); let joint_dst = self.dst.join(file_name); - try_err!(fs::create_dir_all(&self.dst), &self.dst); let mut dst = try_err!(File::create(&joint_dst), &joint_dst); try_err!(dst.write_all(&buf), &joint_dst); From 2c9d452a295c192f3e4762225fc4f4feef6f769c Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Fri, 13 Oct 2017 15:12:13 -0500 Subject: [PATCH 2/2] rustdoc: wrap redirect page writers in a BufWriter --- src/librustdoc/html/render.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index ad671e37e69..81dea5121ea 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1562,9 +1562,10 @@ impl Context { // URL for the page. let redir_name = format!("{}.{}.html", name, item_type.name_space()); let redir_dst = self.dst.join(redir_name); - if let Ok(mut redirect_out) = OpenOptions::new().create_new(true) + if let Ok(redirect_out) = OpenOptions::new().create_new(true) .write(true) .open(&redir_dst) { + let mut redirect_out = BufWriter::new(redirect_out); try_err!(layout::redirect(&mut redirect_out, file_name), &redir_dst); } @@ -1574,7 +1575,8 @@ impl Context { if item_type == ItemType::Macro { let redir_name = format!("{}.{}!.html", item_type, name); let redir_dst = self.dst.join(redir_name); - let mut redirect_out = try_err!(File::create(&redir_dst), &redir_dst); + let redirect_out = try_err!(File::create(&redir_dst), &redir_dst); + let mut redirect_out = BufWriter::new(redirect_out); try_err!(layout::redirect(&mut redirect_out, file_name), &redir_dst); } }