From e51814e921aec8c89b2c75cac2c38a7111ca8a73 Mon Sep 17 00:00:00 2001 From: gbuyssens Date: Tue, 24 Nov 2026 20:35:00 +0100 Subject: [PATCH] DirectStorage: inflate zlib requests with zlib-rs Titles stream thousands of 64 KiB zlib blocks through DirectStorage at start-up (the retail title tested: ~9 800 requests in its first load). miniz_oxide decoded them at 210-270 MB/s on an i5-9600K; zlib-rs decodes the same blocks at 410-515 MB/s (400 blocks cut from a game executable and a release xcl.exe, compressed at level 6, 20 passes, 3 rounds). The behaviour is unchanged: the output is limited to the destination or intermediate size, a wrong Adler-32, a cut stream or more output than the limit fail with E_ZLIB_DATA, a shorter stream is returned as it is (BCPack's lenient case), and bytes after the stream's end are ignored. Ruled out: libdeflate was faster still (540-685 MB/s), but its Rust binding is Apache-2.0 only, which GPL-2.0 does not allow. Decoding with miniz_oxide into an exact-size buffer instead of a growing Vec gave nothing measurable. zlib-rs is pure Rust, Zlib licence, no dependencies. --- Cargo.lock | 7 +++++++ crates/xcl/Cargo.toml | 1 + crates/xcl/src/hle/dstorage.rs | 38 +++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9fcde4604a..33a7720969 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -554,6 +554,7 @@ dependencies = [ "toml", "windows", "windows-sys 0.61.2", + "zlib-rs", ] [[package]] @@ -564,6 +565,12 @@ dependencies = [ "windows", ] +[[package]] +name = "zlib-rs" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40990edd51aae2c2b6907af74ffb635029d5788228222c4bb811e9351c0caad3" + [[package]] name = "zmij" version = "1.0.23" diff --git a/crates/xcl/Cargo.toml b/crates/xcl/Cargo.toml index f3f6d604de..0684a2659a 100644 --- a/crates/xcl/Cargo.toml +++ b/crates/xcl/Cargo.toml @@ -19,6 +19,7 @@ retour = "0.3" serde = { version = "1", features = ["derive"] } serde_json = "1" toml = "1" +zlib-rs = "0.5" [dependencies.windows] version = "0.62" diff --git a/crates/xcl/src/hle/dstorage.rs b/crates/xcl/src/hle/dstorage.rs index c240ba6c5f..3e88214fb3 100644 --- a/crates/xcl/src/hle/dstorage.rs +++ b/crates/xcl/src/hle/dstorage.rs @@ -835,6 +835,19 @@ fn read_exact_at(f: &std::fs::File, offset: u64, buf: &mut [u8]) -> Result<(), H Ok(()) } +/// A zlib stream inflated into at most `limit` bytes; the bytes after the stream's end are +/// ignored. zlib-rs: about twice miniz_oxide's speed on the 64 KiB blocks titles stream. +fn inflate_zlib(src: &[u8], limit: usize) -> Result, HRESULT> { + let mut d = vec![0u8; limit]; + let (out, rc) = zlib_rs::inflate::uncompress_slice(&mut d, src, zlib_rs::inflate::InflateConfig::default()); + if rc != zlib_rs::ReturnCode::Ok { + return Err(E_ZLIB_DATA); + } + let len = out.len(); + d.truncate(len); + Ok(d) +} + static REQUESTS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); static BYTES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); static FAILED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); @@ -901,7 +914,7 @@ unsafe fn execute(r: &Request, file: Option<&FileInner>, dest_pages: Option<&[us // the stages in the console's order: zlib, then BCPack let stage1: Vec = if r.zlib() { let stage1_len = if r.intermediate_size != 0 { r.intermediate_size as usize } else { n }; - let mut d = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(&src, stage1_len).map_err(|_| E_ZLIB_DATA)?; + let mut d = inflate_zlib(&src, stage1_len)?; // zlib yields `intermediate_size` bytes; when the title names none, the destination's // size, or before BCPack a stream of any size up to it. That last case is XCL's // leniency, as is a BCPack stream no smaller than its blocks: the GDK documents @@ -1513,6 +1526,29 @@ mod tests { b } + #[test] + fn inflate_zlib_keeps_the_limit_checksum_and_trailing_bytes_rules() { + let plain: Vec = (0..65536u32).map(|i| (i * 7 % 251) as u8).collect(); + let packed = miniz_oxide::deflate::compress_to_vec_zlib(&plain, 6); + assert_eq!(inflate_zlib(&packed, plain.len()), Ok(plain.clone())); + // a limit above the stream's size: the stream's bytes (BCPack reads a shorter stream) + assert_eq!(inflate_zlib(&packed, plain.len() + 100), Ok(plain.clone())); + // bytes after the stream's end (a padded source size) are ignored + let mut padded = packed.clone(); + padded.extend_from_slice(&[0u8; 37]); + assert_eq!(inflate_zlib(&padded, plain.len()), Ok(plain.clone())); + // more output than the limit, a wrong Adler-32, a cut stream and no zlib header fail + assert_eq!(inflate_zlib(&packed, plain.len() - 1), Err(E_ZLIB_DATA)); + let mut bad_sum = packed.clone(); + let k = bad_sum.len() - 1; + bad_sum[k] ^= 1; + assert_eq!(inflate_zlib(&bad_sum, plain.len()), Err(E_ZLIB_DATA)); + assert_eq!(inflate_zlib(&packed[..packed.len() / 2], plain.len()), Err(E_ZLIB_DATA)); + assert_eq!(inflate_zlib(&plain[..1024], plain.len()), Err(E_ZLIB_DATA)); + // an empty stream into an empty destination + assert_eq!(inflate_zlib(&miniz_oxide::deflate::compress_to_vec_zlib(&[], 6), 0), Ok(Vec::new())); + } + #[test] fn a_refused_factory_iid_returns_e_nointerface_and_clears_out() { unsafe {