Skip to content

Commit c58f51c

Browse files
sypharGuillaumeGomez
authored andcommitted
don't compress rustdoc json twice
1 parent 2a68f20 commit c58f51c

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

src/docbuilder/rustwide_builder.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::docbuilder::Limits;
1313
use crate::error::Result;
1414
use crate::repositories::RepositoryStatsUpdater;
1515
use crate::storage::{
16-
CompressionAlgorithm, RustdocJsonFormatVersion, compress, get_file_list, rustdoc_archive_path,
17-
rustdoc_json_path, source_archive_path,
16+
RustdocJsonFormatVersion, get_file_list, rustdoc_archive_path, rustdoc_json_path,
17+
source_archive_path,
1818
};
1919
use crate::utils::{
2020
CargoMetadata, ConfigName, copy_dir_all, get_config, parse_rustc_version, report_error,
@@ -909,21 +909,11 @@ impl RustwideBuilder {
909909
.context("couldn't parse rustdoc json to find format version")?
910910
};
911911

912-
let compressed_json: Vec<u8> = {
913-
let _span =
914-
info_span!("compress_json", file_size = json_filename.metadata()?.len()).entered();
915-
916-
compress(
917-
BufReader::new(File::open(&json_filename)?),
918-
CompressionAlgorithm::Zstd,
919-
)?
920-
};
921-
922912
for format_version in [format_version, RustdocJsonFormatVersion::Latest] {
923913
let _span = info_span!("store_json", %format_version).entered();
924914
let path = rustdoc_json_path(name, version, target, format_version);
925915

926-
self.storage.store_one(&path, compressed_json.clone())?;
916+
self.storage.store_path(&path, &json_filename)?;
927917
self.storage.set_public_access(&path, true)?;
928918
}
929919

@@ -1495,10 +1485,10 @@ mod tests {
14951485
.collect();
14961486
json_files.sort();
14971487
assert!(json_files[0].starts_with(&format!("empty-library_1.0.0_{target}_")));
1498-
assert!(json_files[0].ends_with(".json.zst"));
1488+
assert!(json_files[0].ends_with(".json"));
14991489
assert_eq!(
15001490
json_files[1],
1501-
format!("empty-library_1.0.0_{target}_latest.json.zst")
1491+
format!("empty-library_1.0.0_{target}_latest.json")
15021492
);
15031493

15041494
if target == &default_target {

src/storage/mod.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use mime::Mime;
2424
use path_slash::PathExt;
2525
use serde_with::{DeserializeFromStr, SerializeDisplay};
2626
use std::{
27-
fmt, fs,
27+
fmt,
28+
fs::{self, File},
2829
io::{self, BufReader},
2930
num::ParseIntError,
3031
ops::RangeInclusive,
@@ -569,6 +570,33 @@ impl AsyncStorage {
569570
Ok(alg)
570571
}
571572

573+
#[instrument(skip(self))]
574+
pub(crate) async fn store_path(
575+
&self,
576+
target_path: impl Into<String> + std::fmt::Debug,
577+
source_path: impl AsRef<Path> + std::fmt::Debug,
578+
) -> Result<CompressionAlgorithm> {
579+
let target_path = target_path.into();
580+
let source_path = source_path.as_ref();
581+
582+
let alg = CompressionAlgorithm::default();
583+
let content = compress(BufReader::new(File::open(source_path)?), alg)?;
584+
585+
let mime = detect_mime(&target_path).to_owned();
586+
587+
self.store_inner(vec![Blob {
588+
path: target_path,
589+
mime,
590+
content,
591+
compression: Some(alg),
592+
// this field is ignored by the backend
593+
date_updated: Utc::now(),
594+
}])
595+
.await?;
596+
597+
Ok(alg)
598+
}
599+
572600
async fn store_inner(&self, batch: Vec<Blob>) -> Result<()> {
573601
match &self.backend {
574602
StorageBackend::Database(db) => db.store_batch(batch).await,
@@ -777,6 +805,18 @@ impl Storage {
777805
self.runtime.block_on(self.inner.store_one(path, content))
778806
}
779807

808+
// Store file into the backend at the given path (also used to detect mime type), returns the
809+
// chosen compression algorithm
810+
#[instrument(skip(self))]
811+
pub(crate) fn store_path(
812+
&self,
813+
target_path: impl Into<String> + std::fmt::Debug,
814+
source_path: impl AsRef<Path> + std::fmt::Debug,
815+
) -> Result<CompressionAlgorithm> {
816+
self.runtime
817+
.block_on(self.inner.store_path(target_path, source_path))
818+
}
819+
780820
/// sync wrapper for the list_prefix function
781821
/// purely for testing purposes since it collects all files into a Vec.
782822
#[cfg(test)]
@@ -843,7 +883,7 @@ pub(crate) fn rustdoc_json_path(
843883
format_version: RustdocJsonFormatVersion,
844884
) -> String {
845885
format!(
846-
"rustdoc-json/{name}/{version}/{target}/{name}_{version}_{target}_{format_version}.json.zst"
886+
"rustdoc-json/{name}/{version}/{target}/{name}_{version}_{target}_{format_version}.json"
847887
)
848888
}
849889

src/web/rustdoc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3155,7 +3155,7 @@ mod test {
31553155
web.assert_redirect_cached_unchecked(
31563156
&format!("/crate/dummy/{request_path_suffix}"),
31573157
&format!("https://static.docs.rs/rustdoc-json/dummy/{redirect_version}/{redirect_target}/\
3158-
dummy_{redirect_version}_{redirect_target}_{redirect_format_version}.json.zst"),
3158+
dummy_{redirect_version}_{redirect_target}_{redirect_format_version}.json"),
31593159
CachePolicy::ForeverInCdn,
31603160
&env.config(),
31613161
)

0 commit comments

Comments
 (0)