Skip to content

Commit 5b0ec1e

Browse files
committed
parallelize HTML checking tool
1 parent 211637d commit 5b0ec1e

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,7 @@ dependencies = [
17881788
name = "html-checker"
17891789
version = "0.1.0"
17901790
dependencies = [
1791+
"rayon",
17911792
"walkdir",
17921793
]
17931794

src/tools/html-checker/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ path = "main.rs"
99

1010
[dependencies]
1111
walkdir = "2"
12+
rayon = "1.5"

src/tools/html-checker/main.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rayon::iter::{ParallelBridge, ParallelIterator};
12
use std::env;
23
use std::path::Path;
34
use std::process::{Command, Output};
@@ -56,27 +57,30 @@ const DOCS_TO_CHECK: &[&str] =
5657

5758
// Returns the number of files read and the number of errors.
5859
fn find_all_html_files(dir: &Path) -> (usize, usize) {
59-
let mut files_read = 0;
60-
let mut errors = 0;
61-
62-
for entry in walkdir::WalkDir::new(dir).into_iter().filter_entry(|e| {
63-
e.depth() != 1
64-
|| e.file_name()
65-
.to_str()
66-
.map(|s| DOCS_TO_CHECK.into_iter().any(|d| *d == s))
67-
.unwrap_or(false)
68-
}) {
69-
let entry = entry.expect("failed to read file");
70-
if !entry.file_type().is_file() {
71-
continue;
72-
}
73-
let entry = entry.path();
74-
if entry.extension().and_then(|s| s.to_str()) == Some("html") {
75-
errors += check_html_file(&entry);
76-
files_read += 1;
77-
}
78-
}
79-
(files_read, errors)
60+
walkdir::WalkDir::new(dir)
61+
.into_iter()
62+
.filter_entry(|e| {
63+
e.depth() != 1
64+
|| e.file_name()
65+
.to_str()
66+
.map(|s| DOCS_TO_CHECK.into_iter().any(|d| *d == s))
67+
.unwrap_or(false)
68+
})
69+
.par_bridge()
70+
.map(|entry| {
71+
let entry = entry.expect("failed to read file");
72+
if !entry.file_type().is_file() {
73+
return (0, 0);
74+
}
75+
let entry = entry.path();
76+
// (Number of files processed, number of errors)
77+
if entry.extension().and_then(|s| s.to_str()) == Some("html") {
78+
(1, check_html_file(&entry))
79+
} else {
80+
(0, 0)
81+
}
82+
})
83+
.reduce(|| (0, 0), |a, b| (a.0 + b.0, a.1 + b.1))
8084
}
8185

8286
/// Default `tidy` command for macOS is too old that it does not have `mute-id` and `mute` options.

0 commit comments

Comments
 (0)