Skip to content

Commit 50f7520

Browse files
rustdoc: DocFS: Replace rayon with threadpool and enable it for all targets
1 parent 24c0b81 commit 50f7520

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

Cargo.lock

+10-1
Original file line numberDiff line numberDiff line change
@@ -5457,13 +5457,13 @@ dependencies = [
54575457
"itertools",
54585458
"minifier",
54595459
"once_cell",
5460-
"rayon",
54615460
"regex",
54625461
"rustdoc-json-types",
54635462
"serde",
54645463
"serde_json",
54655464
"smallvec",
54665465
"tempfile",
5466+
"threadpool",
54675467
"tracing",
54685468
"tracing-subscriber",
54695469
"tracing-tree",
@@ -6208,6 +6208,15 @@ dependencies = [
62086208
"once_cell",
62096209
]
62106210

6211+
[[package]]
6212+
name = "threadpool"
6213+
version = "1.8.1"
6214+
source = "registry+https://github.com/rust-lang/crates.io-index"
6215+
checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa"
6216+
dependencies = [
6217+
"num_cpus",
6218+
]
6219+
62116220
[[package]]
62126221
name = "tidy"
62136222
version = "0.1.0"

src/librustdoc/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ smallvec = "1.8.1"
2020
tempfile = "3"
2121
tracing = "0.1"
2222
tracing-tree = "0.2.0"
23+
threadpool = "1.8.1"
2324

2425
[dependencies.tracing-subscriber]
2526
version = "0.3.3"
2627
default-features = false
2728
features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"]
2829

29-
[target.'cfg(windows)'.dependencies]
30-
rayon = "1.5.1"
31-
3230
[dev-dependencies]
3331
expect-test = "1.4.0"
3432

src/librustdoc/docfs.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
//! needs to read-after-write from a file, then it would be added to this
1010
//! abstraction.
1111
12+
use std::cmp::max;
1213
use std::fs;
1314
use std::io;
1415
use std::path::{Path, PathBuf};
1516
use std::string::ToString;
1617
use std::sync::mpsc::Sender;
18+
use std::thread::available_parallelism;
19+
use threadpool::ThreadPool;
1720

1821
pub(crate) trait PathError {
1922
fn new<S, P: AsRef<Path>>(e: S, path: P) -> Self
@@ -24,11 +27,21 @@ pub(crate) trait PathError {
2427
pub(crate) struct DocFS {
2528
sync_only: bool,
2629
errors: Option<Sender<String>>,
30+
pool: ThreadPool,
2731
}
2832

2933
impl DocFS {
3034
pub(crate) fn new(errors: Sender<String>) -> DocFS {
31-
DocFS { sync_only: false, errors: Some(errors) }
35+
const MINIMUM_NB_THREADS: usize = 2;
36+
DocFS {
37+
sync_only: false,
38+
errors: Some(errors),
39+
pool: ThreadPool::new(
40+
available_parallelism()
41+
.map(|nb| max(nb.get(), MINIMUM_NB_THREADS))
42+
.unwrap_or(MINIMUM_NB_THREADS),
43+
),
44+
}
3245
}
3346

3447
pub(crate) fn set_sync_only(&mut self, sync_only: bool) {
@@ -54,12 +67,11 @@ impl DocFS {
5467
where
5568
E: PathError,
5669
{
57-
#[cfg(windows)]
5870
if !self.sync_only {
5971
// A possible future enhancement after more detailed profiling would
6072
// be to create the file sync so errors are reported eagerly.
6173
let sender = self.errors.clone().expect("can't write after closing");
62-
rayon::spawn(move || {
74+
self.pool.execute(move || {
6375
fs::write(&path, contents).unwrap_or_else(|e| {
6476
sender.send(format!("\"{}\": {}", path.display(), e)).unwrap_or_else(|_| {
6577
panic!("failed to send error on \"{}\"", path.display())
@@ -70,9 +82,12 @@ impl DocFS {
7082
fs::write(&path, contents).map_err(|e| E::new(e, path))?;
7183
}
7284

73-
#[cfg(not(windows))]
74-
fs::write(&path, contents).map_err(|e| E::new(e, path))?;
75-
7685
Ok(())
7786
}
7887
}
88+
89+
impl Drop for DocFS {
90+
fn drop(&mut self) {
91+
self.pool.join();
92+
}
93+
}

0 commit comments

Comments
 (0)