about summary refs log tree commit diff
path: root/src/librustdoc/html
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-10-24 05:48:56 +0000
committerbors <bors@rust-lang.org>2024-10-24 05:48:56 +0000
commit8aca4bab080b2c81065645fc070acca7a060f8a3 (patch)
treedbb6fe3dbb1618db359a11c87c615f43b78ccc28 /src/librustdoc/html
parent55b7f8e800a6a43657a8582450323d546297c950 (diff)
parent86abb5439ab71f7aa7cf6c48a84b5bfdd06ba009 (diff)
downloadrust-8aca4bab080b2c81065645fc070acca7a060f8a3.tar.gz
rust-8aca4bab080b2c81065645fc070acca7a060f8a3.zip
Auto merge of #131951 - notriddle:notriddle/sha256-compile-time, r=GuillaumeGomez
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.

Addresses https://github.com/rust-lang/rust/pull/131934#issuecomment-2424213861
Diffstat (limited to 'src/librustdoc/html')
-rw-r--r--src/librustdoc/html/static_files.rs19
1 files changed, 6 insertions, 13 deletions
diff --git a/src/librustdoc/html/static_files.rs b/src/librustdoc/html/static_files.rs
index 9e0803f5d3f..a4dc8cd1ed9 100644
--- a/src/librustdoc/html/static_files.rs
+++ b/src/librustdoc/html/static_files.rs
@@ -12,8 +12,8 @@ pub(crate) struct StaticFile {
 }
 
 impl StaticFile {
-    fn new(filename: &str, bytes: &'static [u8]) -> StaticFile {
-        Self { filename: static_filename(filename, bytes), bytes }
+    fn new(filename: &str, bytes: &'static [u8], sha256: &'static str) -> StaticFile {
+        Self { filename: static_filename(filename, sha256), bytes }
     }
 
     pub(crate) fn minified(&self) -> Vec<u8> {
@@ -55,17 +55,9 @@ pub(crate) fn suffix_path(filename: &str, suffix: &str) -> PathBuf {
     filename.into()
 }
 
-pub(crate) fn static_filename(filename: &str, contents: &[u8]) -> PathBuf {
+pub(crate) fn static_filename(filename: &str, sha256: &str) -> PathBuf {
     let filename = filename.rsplit('/').next().unwrap();
-    suffix_path(filename, &static_suffix(contents))
-}
-
-fn static_suffix(bytes: &[u8]) -> String {
-    use sha2::Digest;
-    let bytes = sha2::Sha256::digest(bytes);
-    let mut digest = format!("-{bytes:x}");
-    digest.truncate(9);
-    digest
+    suffix_path(filename, &sha256)
 }
 
 macro_rules! static_files {
@@ -74,8 +66,9 @@ macro_rules! static_files {
             $(pub $field: StaticFile,)+
         }
 
+        // sha256 files are generated in build.rs
         pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
-            $($field: StaticFile::new($file_path, include_bytes!($file_path)),)+
+            $($field: StaticFile::new($file_path, include_bytes!($file_path), include_str!(concat!(env!("OUT_DIR"), "/", $file_path, ".sha256"))),)+
         });
 
         pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> {