perf(libkernel): resolve an APR path's directory once and find the file in its listing #10633

Closed
noir39 wants to merge 1 commit from upstream-pr/10633 into main
noir39 commented 2026-10-24 13:44:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by noir39 on 2026-10-24T13:44:58Z.


sceKernelAprResolveFilepathsToIds* already takes each file's size from a listing of its directory (one directory read instead of one size query per path), but it still resolved every path through ResolvePath, which on Windows asks the file system for each file's attributes (GetFileAttributesW in ResolveHostPath). An absolute path now resolves its directory once (cached by the guest directory), and the file is looked up in that directory's listing; a path whose file is not in the listing (created since, or an unusual name), a relative path and a path ending in ., .. or a separator take the full resolve as before.

Measured

Demon's Souls PPSA01342 v1.005 resolves its whole package at start-up (ResourcePool::GatherResourceFileInfo, about 243,000 entries in 10,059 directories), Windows 11, Samsung 980 NVMe:

  • GetFileAttributesW on each of the 223,311 files: 37.4 s (167 us each); listing all 10,059 directories: 2.1 s.
  • ResourcePool::GatherResourceFileInfo() took 49.07 s on main, 6.5 to 9.3 s with this change.

Tests

New case guest_apr_read_resolve: absolute paths in one directory and a subdirectory, a name in other case, the same id for a path resolved again, a file created after its directory was listed, and a missing file (error_index, size 0). guest_apr_read_* and guest_ampr* pass on Windows (WinLibs GCC 15.2.0).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4995903272 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10633). Originally posted by [noir39](https://github.com/noir39) on 2026-10-24T13:44:58Z. --- `sceKernelAprResolveFilepathsToIds*` already takes each file's size from a listing of its directory (one directory read instead of one size query per path), but it still resolved every path through `ResolvePath`, which on Windows asks the file system for each file's attributes (`GetFileAttributesW` in `ResolveHostPath`). An absolute path now resolves its directory once (cached by the guest directory), and the file is looked up in that directory's listing; a path whose file is not in the listing (created since, or an unusual name), a relative path and a path ending in `.`, `..` or a separator take the full resolve as before. ### Measured Demon's Souls PPSA01342 v1.005 resolves its whole package at start-up (`ResourcePool::GatherResourceFileInfo`, about 243,000 entries in 10,059 directories), Windows 11, Samsung 980 NVMe: - `GetFileAttributesW` on each of the 223,311 files: 37.4 s (167 us each); listing all 10,059 directories: 2.1 s. - `ResourcePool::GatherResourceFileInfo() took` 49.07 s on main, 6.5 to 9.3 s with this change. ### Tests New case `guest_apr_read_resolve`: absolute paths in one directory and a subdirectory, a name in other case, the same id for a path resolved again, a file created after its directory was listed, and a missing file (`error_index`, size 0). `guest_apr_read_*` and `guest_ampr*` pass on Windows (WinLibs GCC 15.2.0). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-24 13:46:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:46:41Z.

Review: CHANGES_REQUESTED


The new guest-to-host directory cache can keep resolving a reused mount point to its old host directory. g_hostDirectories is keyed only by the guest directory string and has no invalidation when aliases change. For example: resolve an APR file under /savedata0 while it is mounted to save A; unmount it (sceSaveDataUmount2 calls RemovePathAlias_nid_no_patch); mount save B into the now-free slot (mount3 calls AddPathAlias_nid_no_patch with the same /savedata0 prefix); then resolve the same guest directory again. _listedPath hits the cached path for save A and may return A's file/size or fail to find B's file, despite the current alias pointing to B. SaveData slots are explicitly reused, and AddPathAlias replaces a same-prefix mapping.

Please invalidate or version this cache when aliases change, or avoid caching across mutable alias mappings. A focused regression can resolve an APR path in one alias target, rebind that prefix to a second directory with a different file, then verify a second resolution/read uses the second target. I have not run this regression locally; this finding is from tracing the exact cache and alias lifecycle in the current source.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638465936 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10633#pullrequestreview-5638465936). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T13:46:41Z. **Review:** CHANGES_REQUESTED --- The new guest-to-host directory cache can keep resolving a reused mount point to its old host directory. `g_hostDirectories` is keyed only by the guest directory string and has no invalidation when aliases change. For example: resolve an APR file under `/savedata0` while it is mounted to save A; unmount it (`sceSaveDataUmount2` calls `RemovePathAlias_nid_no_patch`); mount save B into the now-free slot (`mount3` calls `AddPathAlias_nid_no_patch` with the same `/savedata0` prefix); then resolve the same guest directory again. `_listedPath` hits the cached path for save A and may return A's file/size or fail to find B's file, despite the current alias pointing to B. SaveData slots are explicitly reused, and AddPathAlias replaces a same-prefix mapping. Please invalidate or version this cache when aliases change, or avoid caching across mutable alias mappings. A focused regression can resolve an APR path in one alias target, rebind that prefix to a second directory with a different file, then verify a second resolution/read uses the second target. I have not run this regression locally; this finding is from tracing the exact cache and alias lifecycle in the current source.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10633
No description provided.