Skip to content

Commit 6392bc9

Browse files
rbtcollinsGuillaumeGomez
authored andcommitted
Add DocFS layer to rustdoc
* Move fs::create_dir_all calls into DocFS to provide a clean extension point if async extension there is needed. * Convert callsites of create_dir_all to ensure_dir to reduce syscalls. * Convert fs::write usage to DocFS.write (which also removes a lot of try_err! usage for easier reading) * Convert File::create calls to use Vec buffers and then DocFS.write in order to consistently reduce syscalls as well, make deferring to threads cleaner and avoid leaving dangling content if writing to existing files.... * Convert OpenOptions usage similarly - I could find no discussion on the use of create_new for that one output file vs all the other files render creates, if link redirection attacks are a concern DocFS will provide a good central point to introduce systematic create_new usage. (fs::write/File::create is vulnerable to link redirection attacks). * DocFS::write defers to rayon for IO on Windows producing a modest speedup: before this patch on my development workstation: $ time cargo +mystg1 doc -p winapi:0.3.7 Documenting winapi v0.3.7 Finished dev [unoptimized + debuginfo] target(s) in 6m 11s real 6m11.734s Afterwards: $ time cargo +mystg1 doc -p winapi:0.3.7 Compiling winapi v0.3.7 Documenting winapi v0.3.7 Finished dev [unoptimized + debuginfo] target(s) in 49.53s real 0m49.643s I haven't measured how much time is in the compilation logic vs in the IO and outputting etc, but this takes it from frustating to tolerable for me, at least for now.
1 parent 56a12b2 commit 6392bc9

File tree

4 files changed

+231
-136
lines changed

4 files changed

+231
-136
lines changed

src/librustdoc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ path = "lib.rs"
1111
[dependencies]
1212
pulldown-cmark = { version = "0.5.2", default-features = false }
1313
minifier = "0.0.30"
14+
rayon = { version = "0.2.0", package = "rustc-rayon" }
1415
tempfile = "3"
1516
parking_lot = "0.7"

src/librustdoc/docfs.rs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//! Rustdoc's FileSystem abstraction module.
2+
//!
3+
//! On Windows this indirects IO into threads to work around performance issues
4+
//! with Defender (and other similar virus scanners that do blocking operations).
5+
//! On other platforms this is a thin shim to fs.
6+
//!
7+
//! Only calls needed to permit this workaround have been abstracted: thus
8+
//! fs::read is still done directly via the fs module; if in future rustdoc
9+
//! needs to read-after-write from a file, then it would be added to this
10+
//! abstraction.
11+
12+
use std::fs;
13+
use std::io;
14+
use std::path::Path;
15+
16+
macro_rules! try_err {
17+
($e:expr, $file:expr) => {{
18+
match $e {
19+
Ok(e) => e,
20+
Err(e) => return Err(E::new(e, $file)),
21+
}
22+
}};
23+
}
24+
25+
pub trait PathError {
26+
fn new<P: AsRef<Path>>(e: io::Error, path: P) -> Self;
27+
}
28+
29+
pub struct DocFS {
30+
sync_only: bool,
31+
}
32+
33+
impl DocFS {
34+
pub fn new() -> DocFS {
35+
DocFS {
36+
sync_only: false,
37+
}
38+
}
39+
40+
pub fn set_sync_only(&mut self, sync_only: bool) {
41+
self.sync_only = sync_only;
42+
}
43+
44+
pub fn create_dir_all<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
45+
// For now, dir creation isn't a huge time consideration, do it
46+
// synchronously, which avoids needing ordering between write() actions
47+
// and directory creation.
48+
fs::create_dir_all(path)
49+
}
50+
51+
pub fn write<P, C, E>(&self, path: P, contents: C) -> Result<(), E>
52+
where
53+
P: AsRef<Path>,
54+
C: AsRef<[u8]>,
55+
E: PathError,
56+
{
57+
if !self.sync_only && cfg!(windows) {
58+
// A possible future enhancement after more detailed profiling would
59+
// be to create the file sync so errors are reported eagerly.
60+
let contents = contents.as_ref().to_vec();
61+
let path = path.as_ref().to_path_buf();
62+
rayon::spawn(move ||
63+
match fs::write(&path, &contents) {
64+
Ok(_) => (),
65+
Err(e) => {
66+
// In principle these should get displayed at the top
67+
// level, but just in case, send to stderr as well.
68+
eprintln!("\"{}\": {}", path.display(), e);
69+
panic!("\"{}\": {}", path.display(), e);
70+
}
71+
});
72+
Ok(())
73+
} else {
74+
Ok(try_err!(fs::write(&path, contents), path))
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)