From 639397aecdf09776d76e647a274fae42c7282ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= <153442863+DTsordas@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:16:23 +0200 Subject: [PATCH] VFS: a plain read of a game file asks the file system once, not three times open_decision_for checked the overlay and the original (visible_in_overlay, visible_in_original) before deciding whether an open writes, then read_path_rel checked the overlay again. For a read (no write access, not OPEN_ALWAYS) the first two answers are never used: the decision is the same read_path_rel. Each check is a std::fs::metadata, which costs about as much as the title's open itself. Measured on 200 game files (release, median of 15 rounds, RX 9060 XT box, Windows 11 26300): open_decision 65 -> 19.5 us, open_decision plus the open 94 -> 50 us, against 23 us for a bare File::open. Fixes #999 Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/vfs.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/xcl/src/vfs.rs b/crates/xcl/src/vfs.rs index dd02bcb67a..727e951deb 100644 --- a/crates/xcl/src/vfs.rs +++ b/crates/xcl/src/vfs.rs @@ -545,6 +545,14 @@ impl Vfs { Decision::Use(p) } Target::Game { rel } => { + // a plain read: only `read_path_rel` asks the file system (titles open their files + // for reading thousands of times; each probe below costs about as much as the open) + if !Self::is_write(access, disposition, flags) && disposition != OPEN_ALWAYS { + return match self.read_path_rel(&rel) { + Some(p) => Decision::Use(p), + None => Decision::Fail(ERROR_FILE_NOT_FOUND), + }; + } let in_ov = self.visible_in_overlay(&rel); let in_orig = self.visible_in_original(&rel); let visible = in_ov || in_orig; @@ -844,6 +852,29 @@ mod tests { assert_eq!(v.redirected_writes(), vec!["data\\hello.txt".to_string(), "data\\new.bin".to_string()]); } + #[test] + fn reads_take_the_original_and_read_only_open_always_still_creates() { + let f = Fixture::new("rdonly"); + let v = &f.vfs; + let data = f.game().join("data"); + // a plain read of a missing file names the original: the open itself reports the error + assert_eq!( + v.open_decision("G:\\data\\missing.bin", 0x8000_0000, OPEN_EXISTING, 0), + Decision::Use(data.join("missing.bin").to_string_lossy().to_string()) + ); + // OPEN_ALWAYS reads an existing file where it is … + assert_eq!( + v.open_decision("G:\\data\\hello.txt", 0x8000_0000, OPEN_ALWAYS, 0), + Decision::Use(data.join("hello.txt").to_string_lossy().to_string()) + ); + // … and creates a missing one in the overlay even without write access + let d = v.open_decision("G:\\data\\created.bin", 0x8000_0000, OPEN_ALWAYS, 0); + let Decision::Use(p) = d else { panic!("{d:?}") }; + assert!(p.contains("overlay"), "{p}"); + assert!(!data.join("created.bin").exists()); + assert_eq!(v.redirected_writes(), vec!["data\\created.bin".to_string()]); + } + #[test] fn delete_is_a_persistent_whiteout() { let f = Fixture::new("wo");