Skip to content

Commit 86abb54

Browse files
committed
rustdoc: hash assets at rustdoc build time
Since sha256 is slow enough to show up on small benchmarks, we can save time by embedding the hash in the executable.
1 parent 8069f8d commit 86abb54

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

Diff for: src/librustdoc/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "rustdoc"
33
version = "0.0.0"
44
edition = "2021"
5+
build = "build.rs"
56

67
[lib]
78
path = "lib.rs"
@@ -24,13 +25,15 @@ tracing = "0.1"
2425
tracing-tree = "0.3.0"
2526
threadpool = "1.8.1"
2627
unicode-segmentation = "1.9"
27-
sha2 = "0.10.8"
2828

2929
[dependencies.tracing-subscriber]
3030
version = "0.3.3"
3131
default-features = false
3232
features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"]
3333

34+
[build-dependencies]
35+
sha2 = "0.10.8"
36+
3437
[dev-dependencies]
3538
expect-test = "1.4.0"
3639

Diff for: src/librustdoc/build.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
fn main() {
2+
// generate sha256 files
3+
// this avoids having to perform hashing at runtime
4+
let files = &[
5+
"static/css/rustdoc.css",
6+
"static/css/noscript.css",
7+
"static/css/normalize.css",
8+
"static/js/main.js",
9+
"static/js/search.js",
10+
"static/js/settings.js",
11+
"static/js/src-script.js",
12+
"static/js/storage.js",
13+
"static/js/scrape-examples.js",
14+
"static/COPYRIGHT.txt",
15+
"static/LICENSE-APACHE.txt",
16+
"static/LICENSE-MIT.txt",
17+
"static/images/rust-logo.svg",
18+
"static/images/favicon.svg",
19+
"static/images/favicon-32x32.png",
20+
"static/fonts/FiraSans-Regular.woff2",
21+
"static/fonts/FiraSans-Medium.woff2",
22+
"static/fonts/FiraSans-LICENSE.txt",
23+
"static/fonts/SourceSerif4-Regular.ttf.woff2",
24+
"static/fonts/SourceSerif4-Bold.ttf.woff2",
25+
"static/fonts/SourceSerif4-It.ttf.woff2",
26+
"static/fonts/SourceSerif4-LICENSE.md",
27+
"static/fonts/SourceCodePro-Regular.ttf.woff2",
28+
"static/fonts/SourceCodePro-Semibold.ttf.woff2",
29+
"static/fonts/SourceCodePro-It.ttf.woff2",
30+
"static/fonts/SourceCodePro-LICENSE.txt",
31+
"static/fonts/NanumBarunGothic.ttf.woff2",
32+
"static/fonts/NanumBarunGothic-LICENSE.txt",
33+
];
34+
let out_dir = std::env::var("OUT_DIR").expect("standard Cargo environment variable");
35+
for path in files {
36+
let inpath = format!("html/{path}");
37+
println!("cargo::rerun-if-changed={inpath}");
38+
let bytes = std::fs::read(inpath).expect("static path exists");
39+
use sha2::Digest;
40+
let bytes = sha2::Sha256::digest(bytes);
41+
let mut digest = format!("-{bytes:x}");
42+
digest.truncate(9);
43+
let outpath = std::path::PathBuf::from(format!("{out_dir}/{path}.sha256"));
44+
std::fs::create_dir_all(outpath.parent().expect("all file paths are in a directory"))
45+
.expect("should be able to write to out_dir");
46+
std::fs::write(&outpath, digest.as_bytes()).expect("write to out_dir");
47+
}
48+
}

Diff for: src/librustdoc/html/static_files.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub(crate) struct StaticFile {
1212
}
1313

1414
impl StaticFile {
15-
fn new(filename: &str, bytes: &'static [u8]) -> StaticFile {
16-
Self { filename: static_filename(filename, bytes), bytes }
15+
fn new(filename: &str, bytes: &'static [u8], sha256: &'static str) -> StaticFile {
16+
Self { filename: static_filename(filename, sha256), bytes }
1717
}
1818

1919
pub(crate) fn minified(&self) -> Vec<u8> {
@@ -55,17 +55,9 @@ pub(crate) fn suffix_path(filename: &str, suffix: &str) -> PathBuf {
5555
filename.into()
5656
}
5757

58-
pub(crate) fn static_filename(filename: &str, contents: &[u8]) -> PathBuf {
58+
pub(crate) fn static_filename(filename: &str, sha256: &str) -> PathBuf {
5959
let filename = filename.rsplit('/').next().unwrap();
60-
suffix_path(filename, &static_suffix(contents))
61-
}
62-
63-
fn static_suffix(bytes: &[u8]) -> String {
64-
use sha2::Digest;
65-
let bytes = sha2::Sha256::digest(bytes);
66-
let mut digest = format!("-{bytes:x}");
67-
digest.truncate(9);
68-
digest
60+
suffix_path(filename, &sha256)
6961
}
7062

7163
macro_rules! static_files {
@@ -74,8 +66,9 @@ macro_rules! static_files {
7466
$(pub $field: StaticFile,)+
7567
}
7668

69+
// sha256 files are generated in build.rs
7770
pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
78-
$($field: StaticFile::new($file_path, include_bytes!($file_path)),)+
71+
$($field: StaticFile::new($file_path, include_bytes!($file_path), include_str!(concat!(env!("OUT_DIR"), "/", $file_path, ".sha256"))),)+
7972
});
8073

8174
pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> {

0 commit comments

Comments
 (0)